How to Install Mail Server on Mac OSX

In this post, I would like to share how to install mail server on Mac OSX. Having mail server is important if you need to send email from your web application in local environment. To do this, we will use Postfix.

Postfix is a Mail Transport Agent (MTA), supporting LDAP, SMTP AUTH (SASL) and TLS. In this post, We will setup Postfix to run with SMTP.

Setup

– Mac OSX (I’m using OSX Yosemite 10.10.3).
– Terminal

Postfix Installation

No need it because Postfix already exist as pre-installed on Mac OSX. Open your terminal, make sure you can see response if you type below:

$ postfix
postfix: error: to submit mail, use the Postfix sendmail command
postfix: fatal: the postfix command is reserved for the superuser

Great, then we are going to configure it to be able for sending email.

Postfix Configuration

We are going to change postfix configuration file. Run this command below:

$ sudo vi /etc/postfix/main.cf

Make sure if these lines below are exist

mail_owner = _postfix
setgid_group = _postdrop

Then, add these lines at the end of file

# Use Gmail SMTP
relayhost = smtp.gmail.com:587
smtp_sasl_mechanism_filter = plain
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
tls_random_source = dev:/dev/urandom

From codes above, you can see that we use Gmail SMTP for our postfix. Next step is add our gmail’s username and password.

$ sudo vim /etc/postfix/sasl_passwd

and add this line

smtp.gmail.com:587 your_username@gmail.com:your_password

Then run command below to create the hash db file for Postfix.

$ sudo postmap /etc/postfix/sasl_passwd

Configure the postfix daemon

$ sudo vi /System/Library/LaunchDaemons/org.postfix.master.plist

Add these lines before </dict>

<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>

Start the postfix service

$ sudo postfix start

* Note that if you make changes in postfix configuration file (/etc/postfix/main.cf), you have to run command

$ sudo postfix reload

Testing

Here is the command you need to execute to test postfix.

$ echo "Test sending email from Postfix" | mail -s "Test Postfix" youremail@domain.com

If success, you will receive email in your inbox.

postfix-testing-email

If you want to see mail queues, execute this command

$ mailq

If you want to see mail log, execute this command

$ tail -f /var/log/mail.log

Conclusion

Setting up mail server on Mac OSX can be done using pre-installed Postfix. Having mail server in local environment will give you advantage for your web application that have sending email feature.

  • payal padiya

    hey,

    as have done the configuration and when i run the mailq it show

    local data error while talking to smtp.gmail.com

    this type of error pls help

  • Andy Moyle

    Hi there, sorry to be commenting on an old post, but it should sort an issue I have
    I’ve got a terminal open and have typed in sudo vi /etc/postfix/main.cf and it has output the contents of the file.

    But I can’t edit it! What am I doing wrong?

    • deerawan

      Have you pressed “a” after “sudo vi…..”? you should see INSERT status in the terminal

      • Andy Moyle

        I need to learn how to use vim – so typed vimtutor and saw how in the end. Thanks for the best article!

  • Chirag Vij

    It took time to reach to the final destination, but it finally received. Thanks a lot!!

    • deerawan

      awesome

  • Eldwin Eldwin

    You are missing “smtp_sasl_security_options =” on # Use Gmail SMTP

  • Mikhail Lebedev

    os sierra

    $ sudo vi /System/Library/LaunchDaemons/org.postfix.master.plist
    -bash: $: command not found

    $ tail -f /var/log/mail.log
    -bash: $: command not found

  • Shubham Gupta

    even if I am not making any changes in /etc/postfix/main.cf, I am able to send mail. so it means there is some default smtp ? am I write ?

    why do we need 3rd party smtp server like (gmail or hotmail) why not go with default ?