56

What do you use as a lightweight SMTP for outgoing e-mails only?

I am running an app that sends e-mails via SMTP when events occur once in a while, and I don't need a fully featured e-mail server.

EDIT: I am more than happy to use something like postfix (and do), if that's the best choice. I was thinking there may be something much simpler, that runs out the box.

derobert
  • 107,579
  • 20
  • 231
  • 279
FlappySocks
  • 661
  • 1
  • 6
  • 6
  • I think it is perfectly reasonable to stick with something like exim and postfix. They aren't hard to configure if you are doing simple things. – Faheem Mitha May 07 '13 at 19:16
  • What's your definition of "lightweight"? How large are you expecting the messages to be? How often is "once in a while"? – Mike B Feb 06 '15 at 19:45
  • The http://emailrelay.sourceforge.net/ is exactly for this. It also works on Windows. – Sergey Ponomarev Sep 29 '22 at 17:07

8 Answers8

25

There are several choices of minimal, relay-only mail transfer agents (MTAs, or "mail servers"), some of which have been mentioned in other answers:

For either of those to work, you need a full fledged MTA somewhere that will further relay your messages (known as "mail hub") and it is strongly recommended that you control this mail hub. Bad Things™ can happen otherwise.

  • esmtp is no longer being maintained. – Nikola Kotur Aug 06 '13 at 07:56
  • 2
    nullmailer has a serious SMTP compliance problem : it retries infinitely when given an SMTP permanent error. This can cost you real money if you pay for bandwidth/transit http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=329192 – telent Feb 06 '14 at 16:13
  • @telent: you should not use nullmailer in such a scenario. –  Feb 07 '14 at 12:09
  • Thanks! The config guide at http://www.troubleshooters.com/linux/nullmailer/ notes a security issue with nullmailer: it exposes passwords on the command line, or at least used to do that. Other conversations note that mstmp makes it a bit less clear how to enqueue mail than nullmailer. – nealmcb Jun 12 '19 at 03:35
14

IMHO, the easiest way to do this is with postfix. You can configure it as a light outgoing relay with these commands:

postconf -e relayhost=other.mailserver.com
postconf -e myorigin=my.domainname.com

As long as the SMTP server specified in relayhost doesn't require authentication, allows you to relay or is a valid destination for the recipient address this will work.

The myorigin parameter will be the default domain name. So if you send mail to foo it will go to [email protected]. Mail sent by user bar will be from [email protected].

If you do need to authenticate and want to support TLS, also run the following commands:

postconf -e smtp_tls_security_level=may
postconf -e smtp_sasl_auth_enable=yes
postconf -e smtp_sasl_password_maps=pcre:/etc/postfix/sasl_password_maps

Then in /etc/postfix/sasl_password_maps have this content:

/./ username:password

All mail will be sent to your relayhost with the specified username and password.

bahamat
  • 38,658
  • 4
  • 70
  • 103
  • Tried these settings and I get an error: "error: unsupported dictionary type: pcre" – valentt Nov 06 '15 at 11:37
  • 1
    Install pcre and/or postfix-pcre and try again. – bahamat Nov 07 '15 at 06:48
  • I figured it out, also in the mean time, there were two packages missing, first one is postfix-pcre as you said, but other one was a strange one - libsasl2-modules. I'm puzzled how libsasl2 and libsasl2-modules-db were installed but libsasl2-modules was missing... Now finally everything works. Can you please add this crucial info to your answer? – valentt Nov 09 '15 at 11:39
  • 1
    For tls, I also used: `relayhost=other.mailserver.com:465` then `smtp_tls_wrappermode=yes` and `smtp_tls_security_level=encrypt` but it's attempting to use the local user as the auth. I have to rewrite the auth user somehow. – tudor -Reinstate Monica- Jun 29 '16 at 04:13
  • "As long as the SMTP server specified in relayhost doesn't require authentication" -> ok, but that's rarely the case nowadays. Your suggestion is correct, but mostly irrelevant! – FarO Jun 12 '19 at 09:16
  • @FarO I explicitly covered both cases. Two commands if you don't need to authenticate (believe it or not, plenty of places still use an IP ACL), three additional commands if you do need to auth. – bahamat Jun 21 '19 at 00:34
9

Assuming you need a program you can run from the shell like sendmail(8), perhaps MSMTP will fit your needs? It can connect to a remote SMTP server and submit mail it gets as stdin, just like sendmail.

  • I use MSMTP for this, works great and the configuration is very simple. – dsp Sep 01 '10 at 15:16
  • I'm so glad you recommended this. I've been looking for something like that for quite a while now. sendmail's configuration is just way too differentiated for my needs. – ixtmixilix Sep 03 '10 at 20:33
4

ESMTP is a relay only mail transfer server that may work for your application.

moberley
  • 331
  • 3
  • 10
2

You may find sSMTP suitable for your needs. Read this article on it and its limitations.

Honestly though, I wouldn't be too stressed about installing postfix or exim, even if it is only for sending emails.

anonman
  • 145
  • 1
  • 1
  • 6
Evan
  • 2,041
  • 2
  • 13
  • 8
  • 1
    +1 for ssmtp, -1 for the unnecessary comment at sendmail, as well as recommending using a full mta, which is _way_ harder to do right. badly configured and maintained mail servers make the life of us mail admins hell. –  Sep 01 '10 at 11:27
  • @hop: I disagree on both counts. sendmail is awful. Also, exim for example is the Debian default, and is perfectly simple to use. So is postfix, probably, but I have never used it. – Faheem Mitha May 07 '13 at 19:14
2

If you use docker, https://hub.docker.com/r/zixia/simple-mail-forwarder/ is a good choice for easy deploy and only of 10MB size, with full smtp(tls) support.

Huan
  • 132
  • 7
1

You technically don't need an e-mail server on your host, what you need (and probably already have) is an smtp-client (or MUA) that can connect to your corporate SMTP Server (which could be anywhere you can connect to.)

Depending on the App (and the programming language it was written in) there are many different ways for your app to connect to the 'the' SMTP Server on your network.

Usually, with PHP or Python, there's a configuration file with the app to specify the "SMTP" Server (which can be on your host or elsewhere on your network.) If you already have a mail server on the network, then just point the configuration to that server.

If no other options exist, the app can generate the e-mail message to standard output that you can process with something like:

listoftargets="[email protected] [email protected]" echo "Test Content" | mail -s "Test Subject" $listoftargets

On some systems, there is already a minimal mailserver for sending only.

Sam T.

samt
  • 151
  • 3
  • I have a whole bunch of servers in the cloud, and I really wanted each to have their own outgoing SMTP servers, rather than rely on anything external. In that way, if my remote SMTP server is down, it's not effecting every server. – FlappySocks Sep 01 '10 at 10:09
  • 1) if you let PHP or Python do the smtp part, you have to do all the work (queueing, error handling) that an MTA would handle. 2) at the end you contradict what you say in the beginning –  Sep 01 '10 at 11:16
  • Some linux applications are setup to expect `mail` or `sendmail` named executables. System notifications. That's the whole point isn't it? For anything else there are no constraints. – Craig Hicks Jul 14 '18 at 22:20
0

apt install postfix (will also restart config if installed)
Edit this config file: vi /etc/postfix/main.cf

Uncomment this line or use this sed:
sed -i 's/#smtp_sasl_security_options/smtp_sasl_security_options/' /etc/postfix/main.cf

Add your smtp info and reload service:
echo '[smtp.gmail.com]:587 <your_email>:<your_password>' > /etc/postfix/sasl_passwd && chmod 600 /etc/postfix/sasl_passwd (or [smtp.sendgrid.net]:587 apikey:xxx)
postmap /etc/postfix/sasl_passwd && systemctl reload postfix

Test it:
EMAIL=<your_email_address>
mail $EMAIL <<< "message" works.. or also(sendmail -f $EMAIL $EMAIL <<< "message")

Weekly postive control test message:
echo "mail -s "email test on: $(hostname)" root \<\<\< "email_test" > /etc/cron.weekly/email_test or if using .env/file:
echo "mail -s "email test on: $(hostname)" \$(cat ~/.env/EMAIL)" \<\<\< "email_test" > /etc/cron.weekly/email_test


To use one email forwarding for all root mail:
echo "your_email_address" >> /etc/aliases && newaliases and test with:
echo "body" | mail -s "subject" root

Now all apps mailing to root will go to your email. More info here:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-as-a-send-only-smtp-server-on-ubuntu-16-04#step-3-testing-the-smtp-server

alchemy
  • 537
  • 5
  • 16