Can I set up system mail to use an external SMTP server?

I found sSMTP very simple to use.

In Debian based systems:

apt-get install ssmtp

Then edit the configuration file in /etc/ssmtp/ssmtp.conf

A sample configuration to use your gmail for sending e-mails:

# root is the person who gets all mail for userids < 1000
[email protected]

# Here is the gmail configuration (or change it to your private smtp server)
mailhub=smtp.gmail.com:587
[email protected]
AuthPass=yourGmailPass
UseTLS=YES
UseSTARTTLS=YES

Note: Make sure the "mail" command is present in your system. mailutils package should provide this one in Debian based systems.

Update: There are people (and bug reports for different Linux distributions) reporting that sSMTP will not accept passwords with a 'space' or '#' character. If sSMTP is not working for you, this may be the case.


For postfix:

  • Add the IP for your external mail-relay to /etc/hosts and add an alias mailrelay to it.
  • Modify the postfix configuration:

    relayhost = [mailrelay]
    smtp_sasl_auth_enable = yes
    smtp_sasl_password_maps = hash:/etc/postfix/smtp_auth
    smtp_sasl_security_options = noanonymous
    
  • Edit /etc/postfix/smtp_auth

    mailrelay login:password
    
  • Convert into hash-format

postmap /etc/postfix/smtp_auth

No need to say that only root should be able to read this... chmod u=r,og=-


mailx supports setting the smtp server on the CLI...

echo "message" | mailx -S smtp=$smtphost:$smtpport -s "subject line" -v [email protected]

Nothing needs to be installed, provided your smtp server lets you send un-authenticated mail.

There is no one answer that sets the smtp server for all the bits of software you might have on your Linux box. Each email client can configure a SMTP server.

Tags:

Linux

Email

Smtp