2026-07-15

How to send an email using sendmail command in linux

Stefan Bogdanescu

Stefan Bogdanescu

Founder & Senior Architect

How to send an email using sendmail command in linux

Sending Emails in Linux: Moving Beyond Basic sendmail for External SMTP Delivery

As developers, we often dive into the command line to manage system services. Understanding how mail delivery works—the difference between a local Mail Transfer Agent (MTA) and an external Simple Mail Transfer Protocol (SMTP) server—is crucial when building robust applications or system scripts.

You encountered a very common roadblock: attempting to use the raw sendmail command for email delivery often fails when the actual delivery mechanism relies on an external, authenticated SMTP server. This is because sendmail primarily handles the routing and local delivery, whereas sending mail over the internet requires specific protocol handling (SMTP).

This post will explain why your initial attempts failed and provide the correct, practical methods for sending emails from a Linux environment when you have an external SMTP server available.


The Misconception: sendmail vs. SMTP

The confusion often arises from conflating two distinct layers of email functionality:

  1. MTA (Mail Transfer Agent) - sendmail: This is the system process responsible for accepting mail, sorting it, and handing it off to a delivery agent. In many Linux setups, this handles local delivery or routing between local mailboxes.
  2. SMTP (Simple Mail Transfer Protocol): This is the application-layer protocol used specifically for sending email across the network—the actual handshake required to communicate with an external mail server (like Gmail, SendGrid, or your dedicated corporate SMTP server).

Your original command:

echo "My message" | sendmail -s subject Recipient_mail_id

This command relies on the local sendmail configuration to handle the entire delivery chain. If your system is not configured as a full-fledged MTA capable of authenticating and connecting directly to an external SMTP server, this pipe will fail silently or result in undelivered mail.

The Developer's Solution: Using Dedicated Tools for External Delivery

When you need to push mail through a dedicated external SMTP server, the most reliable method is to bypass relying solely on the local sendmail configuration and use tools explicitly designed to speak the SMTP protocol.

Method 1: Using mailx or mail with SMTP Configuration

While still related to the mail system, utilities like mailx (or the more modern mail) often provide a better abstraction layer when interacting with external servers if they are properly configured to use an external relay. However, for direct control over SMTP authentication, dedicated tools are superior.

Method 2: The Robust Approach – Using ssmtp or msmtp

For connecting directly to an external SMTP server, the most practical approach in a Linux environment is to utilize lightweight mail transfer agents configured specifically for relaying. Tools like ssmtp or msmtp act as simple SMTP clients, handling the connection, authentication, and encryption required by your remote server, rather than trying to force the entire complex routing through the local sendmail setup.

Here is a conceptual example using a hypothetical scenario where you are relaying via an external server defined in your environment variables:

# Set up the message content
MESSAGE="Hi, this is my message, and I'm sending it to you!"
SUBJECT="Test Email via External SMTP"
RECIPIENT="recipient@example.com"

# Use a tool like msmtp (or another client) to push the mail via the configured SMTP server
echo "$MESSAGE" | msmtp -t $RECIPIENT -s "$SUBJECT" -S smtp.your-external-server:587 -u your_username:your_password

Explanation: This approach separates the concerns. You are no longer relying on sendmail to manage the external connection; you are using a specialized tool (msmtp) that is explicitly designed to handle the SMTP handshake with your remote server, providing necessary authentication credentials directly.

Best Practices for System Integration

When integrating external services into a Linux environment, it’s vital to treat system configuration as code. Just as modern frameworks like Laravel rely on robust service configurations (like .env files) to manage dependencies, your mail delivery setup should be equally explicit.

Always focus on authentication and security. Never hardcode passwords into scripts. Use secure methods, such as environment variables or configuration files managed by the system, to store credentials for your external SMTP server. This ensures that if you are managing complex deployments, like those found in a Laravel application running via a Linux server, the mail functionality is reliable and secure.

Conclusion

While sendmail is the foundational tool for local mail handling on Linux, it often falls short when dealing with modern requirements involving external SMTP relaying. For sending emails through an established external SMTP server, shift your focus from forcing sendmail to handle the entire process to using specialized clients like msmtp. This separation of concerns leads to more reliable, secure, and maintainable system scripts, which is the hallmark of good software development practice.

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.