Send mail from command line in Linux or OpenWrt

You might want to send mail from command line in Linux for logging or sending mails automatically for some purpose, obviously not for spamming.

In this tutorial we’re not installing or configuring a SMTP server, instead using Gmail’s already configured and secure SMTP server.

This tutorial is for almost every UNIX like OS to send mail command line, including OpenWrt, let’s get started …

 

 1. Configure Gmail, or other email providers

As we’re using Gmail for forwarding mails through it’s SMTP server, obviously we need a Gmail account. But it’s up to you, you could whatever mail service, as example Yandex Mail, if you like.

There’s nothing much to configure with gmail, just enable access to some apps, which Google considers as “Less Secure”.

After logging in to Gmail, go to this URL and turn on Access to less secure apps.

http://www.google.com/settings/security/lesssecureapps , unnecessary screenshot below,gmail_enable_access_to_lesssecureapp

I think other mail services also require something similar, to increase their user’s security. You’ve to configure some more if you’re using two factor authentication.

 

2. Installing and configuring sSMTP

The next step is to install a MTA(message transfer agent), and here we’re going to use sSMTP , it’s for only sending, it could not receive mails.

Install sSMTP in Debian or Ubuntu,

 sudo apt-get install ssmtp 

Install sSMTP in OpenWrt

opkg update && opkg install ssmtp ca-certificates 

Note: In OpenWrt prbabbly you’ve to configure extroot, as sSMTP’s dependency  libopenssl consumes around 1.7 MB.

The sSMTP configuration file is /etc/ssmtp/ssmtp.conf, you’ve to customize it according to your credentials.

An example configuration below,


mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
hostname=smtp.gmail.com:587

[email protected]
AuthPass=sexample_password

FromLineOverride=YES
UseTLS=YES
USESTARTTLS=YES

Use the man ssmtp.conf command to know more about configurations.

 

3. Test, sending mails from command line

There are many command line utilities to send mails, and in many flavor too.

  • Using the mail command in any Linux distro, it’s available as mailutils in Debian or Ubuntu.
     mail -s "System Log" -A /var/log/syslog [email protected] < "test_body" 

    Modify the command above according to your need.

  • Using the mpack command, it’s good for scripting.
      mpack -s "Syslog @ $(date)" /var/log/syslog [email protected] 

    This will send mail with the current timestamp in the mail subect, the /var/log/syslog file will be sent as attachment.

  • You can also use the ssmtp command too, if you want to type a bit long text.
     ssmtp [email protected] 

 

4. Sending mails with mailsend command, OpenWrt specific

This command is extremely useful for scripting, fits well with crontab entries, and this time you don’t have to configure a MTA like previous commands.

  1. Install mailsend in OpenWrt, probably you’ve to configure extroot this time too.
     opkg update && opkg install mailsend ca-certificates 
  2. An example, using gmail’s SMTP server with port 465
    mailsend -smtp smtp.gmail.com  -port 465 -t [email protected] -f [email protected] -sub 'nothing'  -ssl -auth -user [email protected] -pass "Secret_pass"  -msg-body /tmp/resolv.conf 
  3. Another example with gmail’s SMTP server, but this time port number 587 with TLS authentication.
    mailsend -smtp smtp.gmail.com  -port 587 -t [email protected] -f [email protected] -sub 'nothing2'  -starttls -auth -user [email protected] -pass "Secret_pass"  -msg-body /tmp/resolv.conf 

To know more about mailsend, use mailsend -help and mailsend -example.

 

Hope you’ve found this tutorial useful, though it’s not much clarified a bit short, anyway leave comments if you have suggestions, questions or opinions. Have a great day 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *