sendmail Command: Tutorial & Examples

A mail transfer agent for Linux

sendmail is a very popular Mail Transfer Agent (MTA) in the Linux ecosystem. It is used to send emails from the command line or by scripts and programs on a Linux system. It works in the background, routing emails to their proper destinations, either on the local system or over the network.

How does sendmail work?

sendmail uses the Simple Mail Transfer Protocol (SMTP) to move mail from one machine to another. It listens on port 25 for incoming mail and delivers it to the correct mailbox. It can also use other protocols like POP3 or IMAP if configured to do so.

When you execute sendmail, it creates a shell process that communicates with the kernel to send the email. The command parameters and options determine where and how the email is sent.

What is sendmail used for?

sendmail is primarily used for sending emails from a Linux server. It can be invoked from the command line or from a script to send an email. It's often used in cron jobs to send notifications or reports. It can also be used by applications to send email notifications.

Why is sendmail important?

Email is one of the primary modes of communication in the professional world. Having a tool like sendmail allows us to automate this communication. For example, if a backup script fails, sendmail can email the system administrator a notification of the failure.

In addition, sendmail is an essential component of the Internet email architecture. It's responsible for transferring emails from one server to another.

How to use sendmail

To use sendmail, you need to provide the email addresses and the message to send. Here is a basic example:

echo "This is the body of the email" | /usr/sbin/sendmail someone@example.com

This will send an email to someone@example.com with the text "This is the body of the email".

To add a subject, you can use the -s option:

echo "This is the body of the email" | /usr/sbin/sendmail -s "This is the subject" someone@example.com

Common sendmail parameters

  • -t: Read message for recipients
  • -s: Specify the subject of the email
  • -f: Set the sender's address
  • -c: Add a CC address
  • -b: Add a BCC address

Potential problems and pitfalls with sendmail

While sendmail is widely used, it's not without its potential issues. Here are a few things to watch out for:

  • Configuration: sendmail can be tricky to configure properly, especially for beginners. Misconfiguration can lead to emails not being sent or received.
  • Spam: Since sendmail is used to send emails, it can be exploited by spammers if not properly secured.
  • Performance: sendmail can be resource-intensive, especially on servers with a high volume of email.

If you're facing high load issues or network issues, you might need to investigate if sendmail is the culprit.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA