Tony Lea

Send Email from Ubuntu with PostFix

Created April 17, 2020

Sending email from a single line of code inside your Ubuntu server is actually pretty simple. Make sure that you've installed PostFix on your server and then you can simply send out emails with the following command:

echo "This is the body of the email" | mail -s "This is the subject line" [email protected]

And sure enough, if you run that line of code [email protected] should receive an email.

Next, what if we wanted to send from a specific email address. Well you could add the -a flag to include additional headers in the email, like so:

echo "This is the body of the email" | mail -aFrom:[email protected] -s "This is the subject line" [email protected]

And now that email will be sent from your desired email address. Of course, this email will probably be sent to spam until you've verified a DNS entry for a specific sender domain. To learn more about preventing the email from going to spam you can check out this article: https://www.linuxbabe.com/mail-server/block-email-spam-postfix

That's a simple way to send an email from your Ubuntu server in a single line of code. It's kind of cool to see how easy it is to send emails and when it comes down to it, sending emails is really nothing more than passing strings with messages to another server. The DNS and SMTP servers receiving that email are responsible for parsing and routing them appropriately.