Bootstrap FreeKB - Linux Commands - Send and retrieve email using the sendmail command
Linux Commands - Send and retrieve email using the sendmail command

Updated:   |  Linux Commands articles

The which command can be used to determine if the sendmail command is installed on your system. In this example, the sendmail command is installed.

~]# which sendmail
/sbin/sendmail

 

In this example, sendmail is used to send an email to john.doe@example.com. This assumes there is a user account for john.doe on the example.com email server.

sendmail john.doe@example.com

 

The -t flag can be used to send the email to the "to" recipient.

sendmail -t

 

You will then be prompted to provide the sender (from) email, recipient (to) email, message, and a single dot to send the email.

From: no-reply@example.com
To: john.doe@example.com
This is a test
.

 

The -r option can be used to set the from (sender) email.

sendmail -r john.doe@example.com -t

 

echo can be used to set the sender (from) email, recipient (to) email, subject, and message body.

echo -e "From: john-doe@example.com\nTo: jane.doe@example.com\nSubject: Example Subject \n\n email body goes here" | sendmail -t

 

Or you can store the information in a file.

echo "From: no-reply@example.com" > foo.txt
echo "To: john.doe@example.com"  >> foo.txt
echo "Subject: Testing"          >> foo.txt
echo "Example message"           >> foo.txt

 

And then send the email by reading the file.

cat foo.txt | sendmail -t

 

To set additional options, such as the target SMTP server or to enable STARTTLS, the sendmail-cf package will need to be installed

dnf install sendmail-cf

 

Create a backup copy of the sendmail.mc (macros) file.

sudo cp /etc/mail/sendmail.mc /etc/mail/sendmail.mc.original

 

Here is an example of how you would set the SMTP server in sendmail.mc.

define(`SMART_HOST', `smtp.example.com')dnl

 

Use make to rebuild sendmail-cf. If the make is successful, no output should be returned.

/etc/mail/make

 

Restart the sendmail service. Now, sendmail will use smtp.example.com (in this example) when sending emails.

systemctl restart sendmail

 

The -v or -vv or -vvv options can be used for verbose output.

echo -e "From: john-doe@example.com\nTo: jane.doe@example.com\nSubject: Example Subject \n\n email body goes here" | sendmail -t -vvv

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter ae6208 in the box below so that we can be sure you are a human.