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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a VPS running Ubuntu 13.10 on Digital Ocean.

I'd like to install postfix on the server, but I want it to be able to send e-mails only to my e-mail address whenever a system message wants to be sent to root.

Now I have my postfix installed as local only. This sends the messages to /var/mail/root.

Instead, I'd like my messages to go to my real e-mail (example@gmail.com), but I don't want to allow other users/sites to send e-mail (like for example from PHP's mail()).

Is this possible?

share|improve this question
up vote 4 down vote accepted

There are several ways to do this

Using SSMTP:

You can find a detailed article here. (Please consider Zulakis' comment below regarding security: I let ssmtp solution here for your knowledge, but prefer postfix solution)

Install ssmtp

sudo aptitude install ssmtp

Edit the configuration file:

sudo vim /etc/ssmtp/ssmtp.conf

And configure it with your gmail account:


Using Postfix

If you want to use your postfix install, you can configure it with your gmail account. You can found a detailed article here.

Check that you have all the needed dependencies

mailutils libsasl2-2 ca-certificates libsasl2-modules

Edit the configuration of postfix:

sudo vim /etc/postfix/main.cf

And configure it with your gmail account:

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

Create the file with your password:

vim /etc/postfix/sasl_passwd

And add the following lines

[smtp.gmail.com]:587    USERNAME@gmail.com:PASSWORD

Allowing root only

I'm not exactly sure what you mean with

I don't want to allow other users/sites to send e-mail (like for example from PHP's mail())

But for blocking mail access per user or per domain, you can edit the following file:

vim /etc/mail/access

And add rules such as:

To:a1@server.vikas.com        REJECT            # Reject a1 user from recieving mails
From:a1@server.vikas.com     REJECT           # Reject a1 user from sending mails

I hope this helps.

share|improve this answer
For everyone reading this answer: 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 use the postfix solution proposed here! – Zulakis Feb 24 at 10:07

Add an alias entry in your alias_maps file like

root:       example@gmail.com

This will send all mails of root to example@gmail.com

share|improve this answer

i think what you are asking for is only partly possible, since the two requirements (accepting (and forwarding) administrative emails and rejecting mails from other users) are not orthogonal.

e.g. what happens, if a service wants to send an email to root via PHP's mail() interface? should it be allowed? forbidden? what happens (as is usually the case) if a service runs as an unprivileged user instead of root?

so i guess, what you really want is to only use the MTA for sending administrative emails from your local machine.

this can be accomplished by configuring your MTA to do the following (these are generic instructions to configure any MTA, not specific to postfix):

  • only accept emails from localhost (thus it will not accept emails from outside); do this by configuring your MTA to listen on and/or by setting up a firewall that blocks all incoming traffic on port 25.

  • only accept emails for administrative accounts (root, webmaster, postmaster, abuse,...) and setup an alias to forward these emails to me@example.com

given that you control your own server, there is little use in blocking certain applications (e.g. php) from sending emails via your own mail-server. simply configure these applications to use a different mailserver (if need be), or re-evaluate your requirement.

share|improve this answer

Your Answer


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.