DNS 323 & 325 - Funplug : Send mail from shell scripts using your Gmail account

Contents[Hide]

dropcap-dns325-gmail

Once you have prepared your DNS-323 or your  DNS-325 with Fun plug and adds-on, a lot of possibilities are opened, especially in terms of automated batch processing.

For example, if you automate some processes like backup, database dump, ... you may be interested in sending reports by email straight from your batchs.

This article will explain how to configure your NAS to be able to send emails from command line.

The sendmail client used will be the one provided by the NAS firmware : msmtp.

Configuration will be done to be able to use a Gmail account, but it can obviously be adapted to any other smtp provider.

This article has been tested on DNS-323 running Fun plug 0.5 and on DNS-325 running Fun plug 0.7. It should also be applicable to a DNS-320 as well.

1. Configuration file

Email account configuration and log files will be held under /ffp/etc/msmtp.

The configuration file will hold all the needed information about the account used to send mails.

Important : It has to be accessible by the root user only (600), otherwise msmtp will refuse to use it.

So, here are she steps to create the environment files & directories :

# mkdir -p /ffp/etc/msmtp
# touch /ffp/etc/msmtp/msmtp.conf
# chmod 600 /ffp/etc/msmtp/msmtp.conf
# nano /ffp/etc/msmtp/msmtp.conf

The configuration file has to follow the msmtp specifications.

If you are using a gmail account, your /ffp/etc/msmtp/msmtp.conf configuration file should be like that :

/ffp/etc/msmtp/msmtp.conf

# Set gmail default values
defaults
tls on
tls_certcheck off

# Gmail account parameters
account default
host smtp.gmail.com
port 587
from This email address is being protected from spambots. You need JavaScript enabled to view it.
auth on
user This email address is being protected from spambots. You need JavaScript enabled to view it.
password yourgmailpassword

# set log file
logfile /ffp/etc/msmtp/msmtp.log

2. Send a mail

When the configuration file is ready, you should be able to send a mail.

First thing to do is to prepare a mail body file.

It should be in a text file, for example under /ffp/tmp/mail.txt, and it should have the following structure :

/ffp/tmp/mail.txt

To:This email address is being protected from spambots. You need JavaScript enabled to view it.
Subject:Subject of the mail

Your email body starts here ...
...

Please note that it is very important to have one empty separation line between the Subject and the body.
If not, your mail will be sent, but without body.

Once the email file is prepared, to send it you just need to throw the following command line :

# cat /ffp/tmp/mail.txt | /usr/bin/msmtp --file=/ffp/etc/msmtp/msmtp.conf --read-recipients

Your mail has been sent … that's as simple as that !

You can check that it has been successfuly sent in /ffp/etc/msmtp/msmtp.log.

/ffp/etc/msmtp/msmtp.log

...
Dec 04 16:17:01 host=smtp.gmail.com tls=on auth=on user=xxx from=xxx recipients=xxx mailsize=102 smtpstatus=250 smtpmsg='250 2.0.0 OK 1323012020 x12sm48350372eef.9' exitcode=EX_OK

You are now ready to automatise a lot of tasks on your DNS-32x and to get some feedback & reporting by email.

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus