104

Is it possible to set up system mail on a linux box to be sent via a different smtp server - maybe even with authentication? If so, how do I do this?

If that's unclear, let give an example. If I'm at the command line and type:

cat body.txt | mail -s "just a test" myfriend@hisdomain.com

is it possible to have that be sent via an external SMTP server, like G-mail ?

I'm not looking for "a way to send mail from gmail from the command line" but rather an option to configure the entire system to use a specific SMTP server, or possibly one account on an SMTP server (maybe overriding the from address).

cwd
  • 45,389

6 Answers6

87

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
root=your@email.com

# Here is the gmail configuration (or change it to your private smtp server)
mailhub=smtp.gmail.com:587
AuthUser=your@gmail.com
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.

  • 3
    as a note to others you may have to install ssmtp and then mailutils. i think mailutils sets up the mail command and symlinks to ssmtp so you can use the regular mail syntax :) – cwd Aug 23 '12 at 14:24
  • 1
    @cwd Just tested on Ubuntu 13.04. I didn't have to install mailutils package (and it's not installed automaticaly). ssmtp worked out-of-the box. – Pijusn May 24 '13 at 05:42
  • I just successfully used this in Debian on a BeagleBone Black and did have to install mailutils first. – Dave Nelson Mar 29 '14 at 23:40
  • On Kubuntu 14.04, had to install mailutils after ssmtp. However it worked at first try. – MariusMatutiae May 22 '14 at 03:58
  • 15
    ssmtp does NOT verify the SSL/TLS certificate of the remote server on the current debian, ubuntu and redhat releases and also does NOT verify the hostname of the certificate. This is a major issue, as this effectively renders the encryption useless and your password is being transmitted alike to being plaintext and anyone can sniff it. ssmtp has had no active development since atleast 2009. So, if you care about the security of the email account you use for your servers outgoing emails, do NOT use ssmtp, but postfix (or something else) instead: unix.stackexchange.com/a/118101/72087 – Zulakis Feb 24 '16 at 10:44
  • Can you please provide some more details about your claims that sSMTP is not under active development anymore, as well as the security issue? Some link where it is discussed further maybe? – Vangelis Tasoulas Feb 25 '16 at 11:44
  • I had to activate FromLineOverride=YES in order to send mails successfully – Abdull Dec 21 '16 at 16:01
  • 1
    @VangelisTasoulas That is already linked to from the discussion. You could try to do the minimal work yourself next time. Following the links I found the Debian GIT repo for SSMTP. It has seen no active development for the last decade: https://anonscm.debian.org/gitweb/?p=ssmtp/ssmtp.git – oligofren Feb 08 '18 at 11:29
  • @oligofren very kind of you to try to do the minimal work for me and failing to provide any value to this discussion. sSMTP is unmaintained with regards to adding new features, but that doesn't make a program useless as long as there are proper security updates in place. Debian and Red Hat take security very seriously and patch outstanding issues. Is there any outstanding security issue that hasn't been patched yet? If yes, please point me to it and do not be arrogant. And BTW, the latest version of SSMTP has been shipped in 2014. Someone probably forgot to push their changes in the git repo. – Vangelis Tasoulas Feb 08 '18 at 21:42
  • @VangelisTasoulas You are right, I was a bit condescending. Sorry for that. – oligofren Feb 13 '18 at 14:13
  • 1
    There seems there is some activity here: https://salsa.debian.org/debian/ssmtp/commits/master. So while it may not be an active development, the package doesn't look abandoned either. – ᴍᴇʜᴏᴠ Jan 07 '20 at 08:15
28

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=-

Nils
  • 18,492
20

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

echo "message" | mailx -S smtp=$smtphost:$smtpport -s "subject line" -v foo@baa.com

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.

teknopaul
  • 703
8

My favorite is the email (github) client. It's really simple, no complex configuration needed, no dependencies needed. And you can specify smtp-server argument via command line, which means it's more suitable for scripting. The only pity is that most linux distributions does not contains this tool, so you need to compile it yourself.

Quotes from email project on github

Q: What is 'email' ?

A: 'email' is a program I designed that will send email via the command line to remote smtp servers or use 'sendmail' internally, and fully interact with GNUPG to encrypt and sign your e-mails, so you decide to do so... You can get GNUPG at: http://www.gnupg.org

Thank cygwin for let me known this useful email client.

Compile and install

./configure
make
./install.sh --version 3.1.3 --prefix /usr --mandir /usr/share/man --sysconfdir /etc

Command line options of email

$ email --help
Options information is as follows
email [options] recipient1,recipient2,...

    -h, -help module          Print this message or specify one of the below options
    -V, -verbose              Display mailing progress.
    -f, -from-addr            Senders mail address
    -n, -from-name            Senders name
    -b, -blank-mail           Allows you to send a blank email
    -e, -encrypt              Encrypt the e-mail for first recipient before sending
    -s, -subject subject      Subject of message
    -r, -smtp-server server   Specify a temporary SMTP server for sending
    -p, -smtp-port port       Specify the SMTP port to connect to
    -a, -attach file          Attach file and base64 encode
    -c, -conf-file file       Path to non-default configuration file
    -t, -check-config         Simply parse the email.conf file for errors
    -x, -timeout              Set socket timeout.
        -cc email,email,...   Copy recipients
        -bcc email,email,...  Blind Copy recipients
        -sign                 Sign the email with GPG
        -html                 Send message in HTML format ( Make your own HTML! )
        -tls                  Use TLS/SSL
    -m, -smtp-auth type       Set the SMTP AUTH type (plain or login)
    -u, -smtp-user username   Specify your username for SMTP AUTH
    -i, -smtp-pass password   Specify your password for SMTP AUTH
    -g, -gpg-pass             Specify your password for GPG
    -H, -header string        Add header (can be used multiple times)
        -high-priority        Send the email with high priority
        -no-encoding          Don't use UTF-8 encoding

Sample usage

Simple mail with SMTP server specified

echo "mail body" | email -subject "unix.stackexchange.com Q36982" -from-name LiuYan刘研 -from-addr liuyan@domain.com -smtp-server smtp.domain.com -smtp-port 25 cwd@your-domain.com your-friends@his-domain.com

HTML mail

echo "<h1>header</h1><p>paragraph</p>" | email -html -subject "unix.stackexchange.com Q36982 HTML mail" cwd@your-domain.com

Attachment(s)

echo "see the attachment(s)" | email -subject "This is my email.conf file" -attach /etc/email/email.conf -attach cwd@your-domain.com
2

Set up a local exim SMTP server by following the instructions on this page, but choose the "mail sent by smarthost; no local mail" option from the first screen of the Exim configuration. This will set up an SMTP server on your box that can receive mail from the "mail" or "mailx" commands and will forward all messages to the smarthost (a Gmail server in your case) for delivery.

To send outgoing email through a Gmail server you will need to set up TLS support for Exim, which is not trivial. This Howto gives some explanation and here's a link with further explanation specific to Gmail. You should allow yourself several hours to get this working. I would suggest starting by sending email from your box to a TLS enabled SMTP server to which you have root access so that you can debug your Exim TLS configuration from both sides before trying to send though a Gmail server. Since the communication is encrypted, you wont be able to use tools like tcpdump to analyze the protocol on the line.

Eli Rosencruft
  • 540
  • 2
  • 6
1

If you have external smtp server connectivity with your host then you can configure smtp on your host and send emails.

Smtp configuration in linux steps can be found here : http://kerneltalks.com/config/guide-smtp-configuration-linux/

Email comand examples can be found here : http://kerneltalks.com/commands/examples-send-email-through-terminal/

  • Answers should include enough detail to stand on their own. Having links with more comprehensive information is fine, but at least a portion of the linked material should be quoted in the answer so it can be useful after link rot sets in. – user4556274 Dec 26 '16 at 15:49