Frequently Asked Questions

« Previous
How do I launch parallel jobs using MPI?
Next «
How does Thunderbird autoconfig work?

56. How do I send mail from a shell script in Linux and macOS?

Here is an example of sending mail on Linux and MacOS, one using sendmail and one using mailx.

The sendmail binary may be both in /usr/sbin/sendmail and the traditional /usr/lib/sendmail but using the /usr/bin/env as a wrapper should work with both location.

Please note that both the envelope header and the from-header must be set. This is done with sendmail both inside the mail and as a command line argument. There are other ways of doing this. But this is one of them.

FROM=helpdesk@bmc.uu.se TO=jerker.nyberg@bmc.uu.se SUBJECT="This is in subject" /usr/bin/env sendmail -f $FROM $TO <<EOF To: $TO From: $FROM Subject: $SUBJECT Hello darkness my old friend! EOF

FROM=helpdesk@bmc.uu.se TO=jerker.nyberg@bmc.uu.se SUBJECT="This is in subject" /usr/bin/mailx -s "$SUBJECT" -r $FROM $TO <<EOF Hello darkness my old friend! EOF

This entry sendmail was last modified 2018-06-04

This documentation is covered by GNU Free Documentation License.