Setting up a mail agent for the command line is a great way to keep your email address and mailbox organized and easy to access. You can use an MTA like Sendmail or Mail Transfer Agent (MTA) to send and receive email, or you can use an IMAP server like Postfix. If you’re using an MTA, you need to set up a user account for your mail agent and create a mailbox for it. You can also set up aliases for your mail agent’s addresses so that you don’t have to type them every time you want to send or receive email. To set up your mail agent, follow these steps:
- Log in to your account and click on the “My Account” link in the top left corner of the screen. This will take you to the “Account Settings” page. On this page, click on the “Mail Agents” tab. This will take you to the “Mail Agent Settings” page. On this page, enter the following information into the “User Name” field:
- In the “Password” field, enter a strong password that you’ll remember when setting up your mail agent.
- In the “Domain Name” field, enter a domain name that’s associated with your email address (for example, mydomainname).
- Click on the “Create Mail Agent Account” button and provide some basic information about yourself ( such as your name and contact information).
- After creating your new mail agent account, click on the “Agent Configuration” button in order to start configuring it! On this page, you’ll need to provide some basic information about your email server(s) and mailbox(s). You can find more detailed instructions on how to configure your mail agents here: https://support.microsoftonline/kb/829191 .
- After configuring your new mail agent, click on the “Agent Status
Having your server send you emails is a simple way to get notifications from bash scripts, applications, and cron jobs. Command line email works similarly to personal email, and is easy to configure and use.
How Does Command Line Email Work?
When email is traveling down the tubes of the Internet, it’s usually being sent over the Simple Mail Transfer Protocol, or SMTP. The server that handles sending mail is called an SMTP server, and many free email providers (Gmail, Yahoo, etc.) provide SMTP servers for free. This is great for this use case, as you will only need to configure the command line app that does the sending.
This app is called a Mail Transfer Agent (MTA), and handles communicating with the SMTP server. You’ll need to authenticate the MTA with the SMTP server, which is usually as simple as giving it your password or key. Then, the MTA will be able to act as you, and send emails from your account.
How to Install and Configure Postfix
Postfix can also run its own SMTP server, but it’s it’s harder to configure, and less compatible with external recipients unless you configure domain verification.
Install Postfix and libsasl2-modules, a package for managing SMTP authentication, from your distro’s package manager. For Debian based systems like Ubuntu, that would be:
As Postfix installs, it will prompt you for configuration. At the first screen, select “Internet Site,” which will configure Postfix to use SMTP.
The next prompt will ask for your domain name. You don’t need a domain name to use Postfix, but you will need one to have your emails sent from that domain name. In this example, without specifying the domain name, your emails will come from the Gmail account you have configured for Postfix.
Next, you’ll need to authenticate Postfix. You can use your account’s Gmail password, which is fine if you create a new account just for Postfix, but if you’re using your personal account, you’ll want to create an app password. This way, the password can be revoked at any time. Note that you will need two factor authentication turned on to use app passwords.
Postfix stores authentication details in /etc/postfix/sasl/sasl_passwd. This file may not be there by default, so you may have to create it with touch. Open it up, and paste your information in:
This configures Postfix to use Google’s SMTP server and authenticate with your details.
Next, run postmap on sasl_passwd:
This will generate a sasl_passwd.db file used by Postfix. Both of these files store your app password in plaintext, so you’ll want to restrict them to root by running chown and chmod:
Postfix should now be ready to go, but you’ll need to configure Postfix’s main configuration file to use SMTP relay and your SASL credentials. Open up /etc/postfix/main.cf in your favorite text editor, and find the “relayhost” option. Change this to use Gmail’s SMTP server:
Then at the end of the file, add the following lines to configure SASL and use your password file.
Save this file, then restart Postfix with systemctl:
Postfix should now be fully configured, and set as the default mail handler on your system. You can test it using Postfix’s own sendmail command:
Check your inbox (or outbox), and you should see a new email. You can run sudo tail -f /var/log/mail.log (or mail.err) to check the mail logs.
Postfix will configure itself as your server’s default mail handler. Any app or program that needs to do emailing should now use Postfix by default, such as PHP (which uses Postfix’s sendmail). Some may need extra configuration, which is usually just telling the app to use Postfix.
If you don’t want to use sendmail (as it is a little clunky) you can install another mail client. A good client is mutt, which supports sending files as attachments, and will use Postfix by default. The syntax for simple sending is:
And for attaching files, you’ll need to seperate the -a flag values from the recipient with a double dash “–“:
Which will show up in your inbox with the file attached, presuming it doesn’t hit any file size limits imposed by the SMTP server:
Whichever mail client you choose, any of them should be usable from within shell scripts, cron jobs, and anywhere else you can configure to run Unix commands.