2026-07-15

Using curl to send email

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

Using curl to send email

The Developer's Dilemma: Using curl to Send Email from Gmail

As a senior developer, we often find ourselves needing to interact with low-level protocols like SMTP directly using command-line tools like curl. While raw protocol interaction offers deep insight into networking, trying to use it to send emails—especially from secured services like Gmail—often hits significant roadblocks related to modern security and authentication.

This post will dissect why your attempt to use a simple curl command failed and explore the more practical, secure methods developers use today to achieve automated email delivery.

Why Raw curl for SMTP is Tricky

Your initial attempt demonstrated an understanding of wanting to automate an action via the command line:

curl -n --ssl-reqd --mail-from "<sender@gmail.com>" --mail-rcpt "<receiver@server.tld>" --url smtps://smtp.gmail.com:465 -T file.txt

The reason this command fails and returns the Access denied: 530 error is twofold:

  1. Protocol Complexity: Sending an email via SMTP (Simple Mail Transfer Protocol) is not a simple file upload operation. It requires a multi-step conversation: connecting, greeting (HELO/EHLO), authenticating (LOGIN/PLAIN), specifying the sender (MAIL FROM), specifying the recipient (RCPT TO), and finally sending the message body (DATA). curl alone cannot magically assemble this complex sequence without knowing the exact SMTP commands required by the server.
  2. Authentication Hurdles: The 530 error is a clear indicator of an authentication failure. Modern email providers like Google (Gmail) enforce strict security protocols. When using standard account credentials, these systems often block direct programmatic access unless specific application-level permissions are granted.

Solving the Authentication Problem: Gmail Security

The core issue here is not the use of curl, but the security layer imposed by Google. To successfully connect to an SMTP server like Gmail programmatically, you must provide valid credentials that the server trusts for automated access.

The Solution: App Passwords

For accounts protected by Two-Factor Authentication (2FA), standard account passwords are often insufficient for programmatic access. Google strongly recommends using App Passwords instead of your main account password when setting up third-party applications or scripts.

  1. Enable 2FA: Ensure Two-Factor Authentication is enabled on your Gmail account.
  2. Generate App Password: Go to your Google Account Security settings and generate an App Password specifically for the application you are connecting to (in this case, a generic email client or script).
  3. Use the App Password in curl (or better yet): When interacting with SMTP via curl, you must use these generated credentials in the appropriate authentication headers or command sequence. Simply passing the password in the URL is generally insecure and often rejected by modern servers.

The Modern Developer Approach: APIs Over Raw Protocols

While understanding raw protocols is valuable, relying on manually crafting complex curl commands for email delivery is brittle and error-prone. For robust, maintainable, and secure applications—especially those built on frameworks like Laravel, where we focus on clean application logic—the preferred method is to use dedicated APIs or established libraries.

Instead of wrestling with SMTP commands via shell scripting, developers should leverage:

  1. SMTP Libraries: Use PHP's built-in PHPMailer or similar libraries if you are working within a PHP environment. These libraries handle the complex TLS negotiation, authentication flow, and error handling for you, making the code significantly more reliable.
  2. Third-Party APIs: For maximum security and ease of integration, services like SendGrid, Mailgun, or AWS SES offer RESTful APIs specifically designed for sending transactional emails. This abstracts away all the complexity of managing SMTP servers and certificates entirely.

This architectural shift aligns perfectly with modern development principles; we focus on solving business problems efficiently rather than reinventing low-level networking protocols. For more insight into structuring secure application logic, understanding how robust system design works is key, much like in building resilient systems within frameworks like those promoted by the Laravel ecosystem.

Conclusion

In summary, while curl is a powerful tool for network interaction, attempting to use it directly to send secure emails from services like Gmail is an overly complicated and insecure path. The Access denied: 530 error stems from missing proper application-level authentication. For reliable email automation in a development context, developers should pivot away from raw SMTP scripting and embrace established libraries or dedicated third-party APIs. This approach ensures security, maintainability, and scalability for any project.

Tags:

Enhance your marketing setup with your own email marketing platform.

Join the growing number of SaaS platforms using Laravel Mail to offer email marketing solutions to their customers.