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.
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.
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
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 email@example.com
This will send an email to
firstname.lastname@example.org with the text "This is the body of the email".
To add a subject, you can use the
echo "This is the body of the email" | /usr/sbin/sendmail -s "This is the subject" email@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
sendmail is widely used, it's not without its potential issues. Here are a few things to watch out for:
sendmailcan be tricky to configure properly, especially for beginners. Misconfiguration can lead to emails not being sent or received.
- Spam: Since
sendmailis used to send emails, it can be exploited by spammers if not properly secured.
sendmailcan be resource-intensive, especially on servers with a high volume of email.