Using sSMTP to send emails from a Linux system?
To send mail messages from a Linux server, you need to install an MTA to take care of this task.
If you don’t want to use a full MTA like Postfix or Sendmail, sSMTP is a good lightweight alternative that is easy to install and configure. However, you do need a relay SMTP server to forward mails. sSMTP only sends mail and therefore cannot receive mail.
So let’s start by installing the package.
# apt install ssmtp
Next we need to configure sSMTP by editing ssmtp.conf file.
# # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=you@yourdomain.com # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=mail.yourdomain.com:<port> # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=yourdomain.com # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES
Make sure that your relay mail server will accept mail from server on which sSMTP was installed by adding the IP address of this server to the list of allowed networks. In this example we also added a port to the url of the “mailhub” which is only required when using an alternative port (not port 25).
if your relay mail server requires authentication you will need add login and password credentials and possibly enable TLS and StartTLS.
AuthUser=<your_mail_user> AuthPass=<your_strong_password> UseTLS=YES UseSTARTTLS=YES
Add these lines after the “mailhub” part.
# # Config file for sSMTP sendmail # # The person who gets all mail for userids < 1000 # Make this empty to disable rewriting. root=you@yourdomain.com # The place where the mail goes. The actual machine name is required no # MX records are consulted. Commonly mailhosts are named mail.domain.com mailhub=mail.yourdomain.com:<port> AuthUser=<your_mail_user> AuthPass=<your_strong_password> UseTLS=YES UseSTARTTLS=YES # Where will the mail seem to come from? #rewriteDomain= # The full hostname hostname=yourdomain.com # Are users allowed to set their own From: address? # YES - Allow the user to specify their own From: address # NO - Use the system generated From: address FromLineOverride=YES
In order to send mail from the CLI we need a mail user agent. On Ubuntu we have the choice of several of these agents but we will go for bsd-mailx.
# apt install bsd-mailx
Now we can test our configuration.
# echo Hello | mail you@yourdomain.com
If something goes wrong, you can view the log file to debug the problem.
# less /var/log/mail.log