Sending emails from console using ssmtp

This article shows simple examples (and disscusses most important side-effects) of using ssmtp for sending emails from console windows or from Bash script. I’m not a very experienced Linux user (actually, I’m very beginner), so consider possible mistakes in this text.

Configuration

ssmtp configuration is kept in /etc/config/ssmtp/ssmtp.conf file (at least on QNAP).

Here is an example configuration for Gmail:

mailhub=smtp.gmail.com:587
rewriteDomain=
UseSTARTTLS=yes
AuthUser=account_name@gmail.com
AuthPass=password

And here is another example:

mailhub=poczta.someserver.com:587
rewriteDomain=someserver.com
UseSTARTTLS=yes
AuthUser=username
AuthPass=password

Sending emails

Now, I’ll show you an example of sending emails directly from console:

(echo "Subject: Topic"; echo "From: sender@domain.com"; echo "To: Me <me@mail.com&>"; echo ""; echo "Message content") | ssmtp me@mail.com

And here comes Bash script:

#!/bin/sh

MAILFILE=/share/email_test.txt

echo "Subject: E-mail test" > $MAILFILE
echo "To: to_whom@to_where.com" >> $MAILFILE
echo "From: from_who@from_where.com" >> $MAILFILE
echo "" >> $MAILFILE
echo "Test sent on $(date '+%Y/%m/%d at %H:%M:%S')." >> $MAILFILE
echo "" >> $MAILFILE
echo "Have a nice day!" >> $MAILFILE

cat $MAILFILE | ssmtp to_whom@to_where.com

rm $MAILFILE

Both examples of ssmtp.conf configuration assumes that you have to authenticate yourself on SMTP server with login and password before sending any e-mail. Most nowadays server requires this as a part of SPAM prevention practice. However, if your server requires authentication and you fail to do so, you’ll most likely get ssmtp: 554 5.7.1 Sender address rejected error.

It is up to your mail server configuration, whether it requires line:

echo "From: from_who@from_where.com" &gt;&gt; $MAILFILE

in mail header or not. If it does not requires it (like GMail), then don’t bother setting it. QNAP will overwrite that and send e-mails from some different address.

But, if your mail server requires it to be set, make sure, you set it correctly (most often, the same e-mail address as main account, you’re using for sending e-mails through that server). Because, if you miss that one, you may again get ssmtp: 554 5.7.1 Sender address rejected or similar error.

If you want to send HTML-formatted e-mails, add these two lines between: “Subject:” and “To:”

echo "Mime-Version: 1.0" > $MAILFILE
echo "Content-type: text/html; charset=”utf-8”" > $MAILFILE

Change encoding, if you need different. And, of course, you must add some HTML tags to your message body. For example, you must end each line with, as normal line-breaks (added by echo) works only, if message is in plain-text. On the other hand, you can now format your message: change styles, add some external images etc.

Problems

Keep in mind, that due to some unknown (for me) bug in ssmtp such messages will be sent with incorrect title (subject), ie. with “(unknown subject)” as title.

I don’t know any workaround for this problem. I tried to move these two lines before “Subject:”, which restored subject, but message was again in plain-text. I also tried to move them down, below “To:” and “From:”, but in this case message was not delivered and my mail server reported that From: field is malformed and mail relying is forbidden to avoid spam.

Conclusion

Using AuthMethod=plain in ssmtp configuration may help if anything else fails. Although it is strictly related to mail server configuration and may not work on most of them.

SSMTP has problems with passwords containing “=” (equals sign) and those with national, not-Latin characters. Try to avoid that, if you can.

2 comments on “Sending emails from console using ssmtp

  1. Nemesis

    Finally, somebody that explained how it worked, thx!

    Btw, when browsing from a phone, your website shows a sign informing about you importing posts,but not on the computer version..

    1. admin

      Thank you for a kind words. It is pleasure to serve others. The post import is still in process (no time to complete it), so there must be some kind of error that you see this message only on mobile. Have a great day.

Leave a Reply to adminCancel reply