Send Email From Command Line

Occasionally during development, you'll need to send email to test out certain feature or cron job. Moreover, is nice to have this feature from the command line or console so you can debug and trace any issues.

$ echo "foobar" | mail -s "testing" [email protected]
The program 'mail' can be found in the following packages:
* heirloom-mailx
* mailutils
Try: apt-get install 

So, I was given two choices. Which one? I ended up picking GNU Mailutils because of dependency of Exim, which is known to be lightweight and easy to configure.
$ sudo -s
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install mailutils exim4-daemon-light
$ dpkg-reconfigure exim4-config

Exim mailer was configured using following settings.
  • General type of mail configuration: internet site
  • System mail name: foobar
  • IP-addresses to listen on for incoming SMTP connections: ; ::1
  • Other destinations for which mail is accepted: foobar; localhost.localdomain; localhost
  • Domains to relay mail for:
  • Machines to relay mail for:
  • Keep number of DNS-queries minimal (Dial-on-Demand) : 
  • Delivery method for local mail: mbox format in /var/mail
  • Split configuration into smaller files: no

To check whether we've configure Exim correctly.
$ exim -bt [email protected]

R: dnslookup for [email protected]
[email protected]
router = dnslookup, transport = remote_smtp
host [] MX=10

To send out a sample test email.
$ echo "foobar" | mail -s "testing" [email protected]
$ sudo tail -f /var/log/exim4/mainlog

However, GNU Mailutils does not support neither Bind Carbon Copy (BCC) nor Carbon Copy (CC). Hence, we've to install the bsd-mailx package.
$ sudo install apt-get install bsd-mailx

To send a Carbon Copy (CC) email.
$ echo "testing cc email" | mail -s "testing email cc" -c [email protected] [email protected]

To send a test Blind Carbon Copy (BCC) email.
$ echo "testing bcc email" | mail -s "testing email bcc" -b [email protected] [email protected]

No comments:

Post a Comment