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" [email protected]

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).

2
  • It's the way most machines used to be set up, in fact. Do you have any particular MTA in mind? If not (this includes “what is an MTA?”), what distribution are you running? Apr 20, 2012 at 23:52
  • I'm on Ubuntu 10.04, found this article on setting up ssmtp, seems like it might work. can you elaborate more on how most machines are set up?
    – cwd
    Apr 21, 2012 at 0:55

6 Answers 6

85

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
AuthUser=y[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.

11
  • 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, 2012 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, 2013 at 5:42
  • 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, 2016 at 10:44
  • 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: anonscm.debian.org/gitweb/?p=ssmtp/ssmtp.git
    – oligofren
    Feb 8, 2018 at 11:29
  • 1
    There seems there is some activity here: salsa.debian.org/debian/ssmtp/commits/master. So while it may not be an active development, the package doesn't look abandoned either. Jan 7, 2020 at 8: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=-

0
20

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.

5
  • Be sure to put -S var=val before your recipient. Oct 1, 2015 at 13:28
  • 2
    What to do if un-authenticated mail is not allowed? I got Client was not authenticated to send anonymous mail during MAIL,
    – Mian Ahmad
    Dec 10, 2018 at 13:56
  • 1
    My Debian version of mailx doesn't work as above, instead I have to used heirloom-mailx or s-nail Sep 25, 2020 at 15:41
  • 1
    where can i find info on that -S option? i can't find that in any man page anywhere. Jun 25, 2022 at 5:26
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 [email protected] -smtp-server smtp.domain.com -smtp-port 25 [email protected] [email protected]

HTML mail

echo "<h1>header</h1><p>paragraph</p>" | email -html -subject "unix.stackexchange.com Q36982 HTML mail" [email protected]

Attachment(s)

echo "see the attachment(s)" | email -subject "This is my email.conf file" -attach /etc/email/email.conf -attach [email protected]
2
  • Hello, the first link ("cleancode.rg") is dead (and spam).
    – Ehvince
    Apr 10, 2020 at 14:46
  • @Ehvince Thanks, links are updated now. Apr 13, 2020 at 6:40
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.

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/

1
  • 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. Dec 26, 2016 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .