Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
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? –  Gilles Apr 20 '12 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 '12 at 0:55
add comment

4 Answers 4

up vote 15 down vote accepted

I found much simpler the use of sSMTP

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.

share|improve this answer
1  
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. –  Pius May 24 '13 at 5:42
    
I just successfully used this in Debian on a BeagleBone Black and did have to install mailutils first. –  Dave Nelson Mar 29 at 23:40
    
On Kubuntu 14.04, had to install mailutils after ssmtp. However it worked at first try. –  MariusMatutiae May 22 at 3:58
add comment

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

share|improve this answer
add comment

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 projet 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
share|improve this answer
    
Thanks I'll take a look and maybe give it a try –  cwd Aug 23 '12 at 14:23
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.