2026-07-15

How to send email using simple SMTP commands via Gmail?

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How to send email using simple SMTP commands via Gmail?

Sending Email via SMTP: From Raw Commands to Modern Application Design

Sending emails is a fundamental task in software development, and understanding the Simple Mail Transfer Protocol (SMTP) is crucial. While manually sending commands via telnet can be educational, modern applications rarely rely on raw socket manipulation for this purpose. As senior developers, our focus shifts from how to type the command to how to securely and reliably implement the protocol using established libraries.

This post will address the challenge of using SMTP with services like Gmail, focusing on why raw command-line methods are often insufficient for modern security requirements, and what the best practices are today.

The Challenge of Raw SMTP Commands (The telnet Approach)

You correctly identified that connecting to an SMTP server, such as smtp.gmail.com, requires more than just sending simple commands. As you discovered with the example involving the STARTTLS command, modern mail servers enforce security protocols.

When you connect to port 587 (the standard port for submission) and issue a command like STARTTLS, you are initiating a negotiation process where the client and server agree on a secure encryption channel (TLS). This negotiation requires complex handling of certificates and authentication credentials. Trying to manage the handshake, encryption, and authentication purely through raw terminal commands is brittle, error-prone, and bypasses the robust security mechanisms provided by dedicated cryptographic libraries.

For instance, while tools like telnet show the underlying plumbing, they abstract away the complexity of managing TLS sessions, certificate validation, and complex authentication methods required by services like Gmail. This is why relying on raw command-line interaction for production email sending is strongly discouraged.

The Developer's Path: Using Libraries and APIs

The professional approach involves leveraging established programming language libraries or Application Programming Interfaces (APIs) designed specifically to handle these complexities. Instead of manually managing the SMTP handshake, we configure our application with credentials and let the library manage the secure communication channel.

For applications built on frameworks like Laravel, for example, developers rarely interact directly with raw SMTP sockets. Instead, they utilize built-in mail features or external services that handle the heavy lifting:

// Conceptual example using a framework's mail feature (e.g., Laravel)
$mail = new \Illuminate\Mail\Mail();
$mail->send(new MyEmail(), function ($message) {
    $message->to('recipient@example.com')->subject('Test Email');
});

This abstraction is powerful because it allows us to focus on the business logic (what email to send) rather than the networking details (how to secure the connection). This reliance on well-tested components ensures that security protocols like TLS are handled correctly and securely, which is a core principle in building robust systems—a concept central to modern development philosophies seen across platforms like laravelcompany.com.

Implementing Secure SMTP with Gmail

To successfully send mail via Gmail's SMTP server programmatically, you need three key pieces of information:

  1. SMTP Host and Port: smtp.gmail.com on port 587 (using STARTTLS).
  2. Authentication: You must use an App Password generated by your Google account settings, as standard account passwords often require Two-Factor Authentication (2FA) restrictions for external application access.
  3. Encryption: The connection must be secured using TLS/STARTTLS.

When using a library, you simply provide these credentials. The library then handles the complex process of establishing the TLS tunnel, presenting the correct authentication headers, and encrypting the data before transmission. This shifts the responsibility for security from brittle command-line scripting to robust, audited code.

Conclusion

While understanding the raw mechanics of SMTP commands is valuable for debugging network issues or deep protocol analysis, it is not the recommended path for application development. For sending emails securely and reliably in production environments, developers should always rely on high-level libraries and APIs. These tools abstract away the complexities of TLS negotiation and authentication, allowing you to build secure, maintainable, and scalable applications quickly. Focus on using established patterns rather than reinventing low-level network protocols.

Note: Blog content is currently available in English.

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.