PCsuggest

  • Quick tip
  • SECURITY
  • NETWORKING
  • OpenWrt
  • HARDWARE

Send mail from command line in Linux or OpenWrt

Updated - September 4, 2018 by Arnab Satapathi

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 ...

Contents

  • 1. Configure Gmail, or other email providers
  • 2. Installing and configuring sSMTP
  • 3. Test, sending mails from command line
  • 4. Sending mails with mailsend command, OpenWrt specific
  • Conclusion

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,

amazon prime logo
Try AmazonPrime for free
Enjoy free shipping and One-Day delivery, cancel any time.
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

AuthUser=example_user@gmail.com
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 receiver_address@gmail.com < "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 receiver_address@gmail.com

    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 receiver_address@gmail.com

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 receiver@gmail.com -f root@openwrt -sub 'nothing' -ssl -auth -user sender@gmail.com -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 receiver@gmail.com -f root@openwrt -sub 'nothing2' -starttls -auth -user sender@gmail.com -pass "Secret_pass" -msg-body /tmp/resolv.conf

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

Conclusion

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 🙂

Filed Under: networking, openwrt Tagged With: command line, dns, mail

Your comments
  1. Serviciotsn says

    September 14, 2018

    Thanks for this tutorial. I'm trying install mail, mailx, sendmail in, opensuse server and fedora server but not working. Now trying with ssmtp, but in opensuse not is functionally.

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © PCsuggest.com · All rights reserved.

  • Home
  • About
  • Contact
  • Privacy Policy
  • Sitemap