Sending Emails with curl Command-Line Tool: A Comprehensive Guide from Gmail to Custom Servers

Dec 05, 2025 · Programming · 22 views · 7.8

Keywords: curl | email | SMTP | Gmail | command-line

Abstract: This article explores in detail how to use the curl command-line tool to send emails via the SMTP protocol, focusing on configuring Gmail accounts, including SSL connections, authentication mechanisms, and email content formatting. It also discusses security best practices, such as avoiding direct password passing in the command line, and how to simplify authentication for custom mail servers. By step-by-step analysis of core command parameters and common error solutions, this paper provides practical technical references for system administrators and developers.

Introduction

In modern system administration and automation tasks, sending emails using command-line tools is a common requirement. curl, as a powerful data transfer tool, supports sending emails via the SMTP (Simple Mail Transfer Protocol) protocol, making it ideal for scripts and batch jobs. This article delves into how to use curl to send emails, with a focus on configuring Gmail accounts and analyzing related technical details.

Basic Principles of Sending Emails with curl

curl communicates with mail servers via SMTP or SMTPS (SMTP over SSL/TLS) protocols to send emails. Core command parameters include: --url to specify the server address and port, --mail-from and --mail-rcpt to set the sender and recipient, and --upload-file to upload the email content file. For connections requiring SSL encryption, use the --ssl-reqd parameter to enforce SSL.

Configuration and Common Issues with Gmail Accounts

When using curl to send emails via Gmail, several key points must be noted. First, authentication is achieved through the --user parameter in the format username:password. However, Gmail's security policies require enabling "Less secure app access" or using "App passwords"; otherwise, errors like "Access denied: 530" may occur. For example, the following command demonstrates correct configuration:

curl --ssl-reqd \
  --url 'smtps://smtp.gmail.com:465' \
  --user 'username@gmail.com:password' \
  --mail-from 'username@gmail.com' \
  --mail-rcpt 'john@example.com' \
  --upload-file mail.txt

The email content file (e.g., mail.txt) must follow standard email format, including headers like From, To, Subject, and the body. For example:

From: "User Name" <username@gmail.com>
To: "John Smith" <john@example.com>
Subject: This is a test

Hi John,
I’m sending this mail with curl thru my gmail account.
Bye!

Security Best Practices

Passing passwords directly in the command line poses security risks, as they may be exposed in system logs or to other users. It is recommended to use the --netrc-file parameter to read credentials from a secure file or pass them via environment variables. Additionally, avoid using the --insecure parameter to skip SSL verification, ensuring connection security.

Simplified Authentication for Custom Mail Servers

For personal or internal mail servers, the authentication process may be simpler due to typically more lenient security policies. For instance, if the server uses basic authentication without SSL, the command can be simplified to:

curl --url 'smtp://server.tld:25' \
  --user 'user:pass' \
  --mail-from 'sender@server.tld' \
  --mail-rcpt 'receiver@example.com' \
  --upload-file email.txt

This avoids the additional security steps required by Gmail, but it is essential to ensure the server configuration permits such access.

Error Handling and Debugging

Common errors like "curl: (67) Access denied: 530" often stem from authentication failures. Check if the password is correct, if less secure app access is enabled, or try using app-specific passwords. Using the curl -v parameter to enable verbose output can aid in debugging connection issues.

Conclusion

curl is a versatile tool for sending emails via SMTP, particularly useful in automation and scripting scenarios. By correctly configuring parameters and adhering to security best practices, users can efficiently send emails from Gmail or custom servers. The examples and analysis provided in this article aim to help readers gain a deep understanding of the technology and apply it in practical projects.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.