XMail configuring in openwrt

I had to learn how to send emails from the command line, simply I needed a script which would send emails to specific email addresses upon the occurrence of certain events.

Of course the easiest way is to create a mailbox on any mail service like gmaile with further sending messages from it to other addresses. But probably much better will be when the box itself (without creating any email address on third party hosting) will be able to send the messages.

The easiest way of realisation was using Xmail. I don't know maybe there are more simple solutions of this problem, but they are unknown to me and so here below I briefly describe how to send emails from the command line in openWrt using Xmail.

Install the package XMail using make menuconfig.

Don't know why, but after the openwrt building there will be some archiv files MailRoot.tar.bz2 in the folder /etc, which probably would be needed for xmail working. Probably it should be unpacked, but where? Let it be the folder named www for this purpose.

In other words we create in www the directory MailRoot and put there the contents of the archiv files.

Further we open the file /etc/config/xmail and see the following lines there:

config xmail
        option  mail_root       '/var/MailRoot'

This lines tell us that the directory www where we unpacked the archiv files is not the right choiсe. But we have already choosen the directory www so we should change this line as follows:

config xmail
        option  mail_root       '/www/MailRoot'

Let's try to send an email using the utility sendmail which is a part of xmail. sendmail needs a file in a specific format. For example, let's create a file with a message in /home/email.example:

From: box1@openwrt
To: myemail@gmail.com
Subject: test email from openwrt

this is openwrt xmail.

Let's send the email from the command line as follows:

# sendmail myemail@gmail.com  < /home/email.example

We get the following message:

cannot find environment variable: MAIL_ROOT

Let's set MAIL_ROOT as follows:

export MAIL_ROOT=/www/MailRoot

Send again:

# sendmail myemail@gmail.com  < /home/email.example

Check myemail@gmail.com. There is no email. Check the contents of this directory:

# ls -l /www/MailRoot/spool/local/
-rw-r--r--    1 root     root           200 Jan 28 23:47 949092438000.3555.TR628M1
-rw-r--r--    1 root     root           200 Jan 28 23:47 949092445000.3576.TR628M1

The emails have been placed in the directory for sending but not sent. Because the Xmail service have not been run. It can be done as follows:

/etc/init.d/xmail start

or restart the system.

Check the output ps:

# ps | grep Xmail
1941 root      5020 S    /usr/bin/XMail -Ms /www/MailRoot -PX 4 -B- -SX 4 -X-

It seems like ok.

Let's try to send the email:

# sendmail myemail@gmail.com  < /home/email.example

The letter should be in your Inbox of myemail@gmail.com. But if your ip address is blacklisted, the email will be discarded by the server and will not fall into Inbox.


Email sending using a third party mail service in openWrt.

Another way of email sending from the console in openwrt using a third party mail service is using the SMTP.

In this case we use the service gmail.com

Install/build the package ssmtp in openwrt.

Let's edit the configuration file /etc/ssmtp/ssmtp.conf as follows:

# /etc/ssmtp.conf -- a config file for sSMTP sendmail.

# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.

# The place where the mail goes. The actual machine name is required
# no MX records are consulted. Commonly mailhosts are named mail.domain.com
# The example will fit if you are in domain.com and your mailhub is so named.

# Where will the mail seem to come from?

# The full hostname

# Set this to never rewrite the "From:" line (unless not given) and to
# use that address in the "from line" of the envelope.

# Use SSL/TLS to send secure messages to server.

# Use SSL/TLS certificate to authenticate against smtp host.

# Use this RSA certificate.

# Get enhanced (*really* enhanced) debugging information in the logs
# If you want to have debugging of the config file parsing, move this option
# to the top of the config file and uncomment



Let's create the file /tmp/msg.txt with the following text:

Subject: ahaha

Send the email to ourself using the following command:

ssmtp myemail@gmail.com < /tmp/msg.txt

After sending we can get the error message:

ssmtp: Authorization failed (534 5.7.14  https://support.google.com/mail/answer/78754 q4sm582746lfq.45 - gsmtp)

To fix the error we need to follow the link: https://www.google.com/settings/security/lesssecureapps

and choose "Activate" in the menu "Account access" from unverified applications.

If it is nessesary to generate an email dynamically and without using of any files then the command line can looke like this:

local email_cmd="ssmtp -t <<EOF\ 
From: <%s>\
To: <%s>\
Date: info\
Subject: сообщение ПУИД\