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.

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

3 Answers 3

up vote 2 down vote accepted
+100

There are several ways to do this

Using SSMTP:

You can find a detailed article here.

Install ssmtp

sudo aptitude install ssmtp

Edit the configuration file:

sudo vim /etc/ssmtp/ssmtp.conf

And configure it with your gmail account:

root=username@gmail.com
mailhub=smtp.gmail.com:587
hostname=username@gmail.com
UseSTARTTLS=YES
AuthUser=username
AuthPass=password
FromLineOverride=yes

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

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

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

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.