How to send emails from localhost (MAC OS X El Capitan)

Sometimes we may need to send mails from localhost for testing purposes. That can be easily done using an external Mail client (Gmail, Hotmail, Yahoo… ETC) and Postfix, which is the standard email server installed by default in Mac OS X (At least in the latest versions). As I said, this is a quick workaround for testing purposes but if security is a requirement, you must buy yourself a certificate at Thawte or Verisign, or create your own one.

This tutorial is about how to configure Postfix for Mac OS X. If you are using windows, you may want to try this approach.

Step 1. Edit Postfix config file

Open a terminal and edit the file main.cf

sudo vi /etc/postfix/main.cf

First check Postfix is configured correctly, look for the following lines (They are probably separated):

mydomain_fallback = localhost
mail_owner = _postfix
setgid_group = _postdrop

Now add the following lines at the very end of the file:

#Gmail SMTP
relayhost=smtp.gmail.com:587
# Enable SASL authentication in the Postfix SMTP client.
smtp_sasl_auth_enable=yes
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options=noanonymous
smtp_sasl_mechanism_filter=plain
# Enable Transport Layer Security (TLS), i.e. SSL.
smtp_use_tls=yes
smtp_tls_security_level=encrypt
tls_random_source=dev:/dev/urandom

This is telling Postfix to use a GMAIL SMTP server with Simple Authentication and Security Layer (SASL). Which will be stored in the path “/etc/postfix/sasl_passwd“. You can use any other SMTP provider (Hotmail, Yahoo, ETC…). You only need to know the SMTP host and port. For example for hotmail you should replace the relayhost for the following:

#Hotmail SMTP
relayhost=smtp.live.com:587

Or for Yahoo:

#Yahoo SMTP
relayhost=smtp.mail.yahoo.com:465

Step 2. Create the sasl_passwd file

We need to create the sasl_passwd file with the SMTP credentials

sudo vi /etc/postfix/sasl_passwd

Write the following content and save:

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

Create the Postfix lookup table from the sasl_passwd file.

sudo postmap /etc/postfix/sasl_passwd

This will create the file sasl_passwd.db

Step 3. Restart Postfix

To apply all new changes we have to restart Postfix:

sudo postfix reload

Step 4. Turn on less secure apps (Only Gmail)

In Gmail we must switch on the option “Access for less secure apps“, otherwise we will get the error:
SASL authentication failed

Step 5. Test it!

Let’s send a mail to our own account to be sure everything is working fine:

date | mail -s testing your_email@gmail.com

You can check the mail queue and the posible delivery errors using “mailq

mailq

Use the postfix logs to ensure everything is working as expected:

tail -f /var/log/mail.log 

Other useful commands

To clear the mail queue:

sudo postsuper -d ALL

Comments 63

    1. Post
      Author
  1. With havin so much content do you ever run into any problems of
    plagorism or copyright infringement? My website has a lot of unique
    content I’ve either created myself or outsourced but it appears a lot of it is popping it up all over the web without my permission. Do you know any techniques to help reduce content from being stolen? I’d truly
    appreciate it.

    1. Post
      Author

      To be honest I’m not really worried about that. This blog is mainly a reminder for me and I’m not a pioneer in many of the approaches I’m posting.
      If someone is taking advantage and copying literally my content.. What can I say? I just hope they include a link to me website ๐Ÿ˜‰

  2. I went through your instructions & I then sent a test email to myself and I got the following error: send-mail: cannot load trust file ~/Programs/Thawte Root Certificates/thawte Premium Server CA/Thawte Premium Server CA.cer: error:00000000:lib(0):func(0):reason(0)
    send-mail: could not send mail (account default from /usr/local/etc/msmtprc). Can you please help me in fixing it so that I won’t encounter this error any longer? Feel free to email me if you wish. Thanks.

  3. This works great, thank you!

    Took me a while to find the correct settings to work with Rackspace, sharing here in case anyone else is having the same issue.

    #Rackspace SMTP
    relayhost = smtp.emailsrvr.com:587
    smtp_sasl_auth_enable=yes
    smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
    smtp_sasl_mechanism_filter = AUTH LOGIN
    smtp_sasl_security_options =
    smtp_use_tls=yes
    smtp_tls_security_level=encrypt

    1. Post
      Author
  4. This is very interesting, You’re a very skilled blogger. I’ve joined your feed and look forward
    to seeking more of your excellent post. Also, I have shared your website in my
    social networks!

  5. I’m having a few problems with this. First of all my main.cf file did not have the following lines:
    mail_owner = _postfix
    setgid_group = _postdrop

    It only had the “mydomain_fallback = localhost” and then some other code under that. So I went ahead and added the two missing lines directly under “mydomain_fallback = localhost”. Then when I restart postfix I get the following:

    postfix: fatal: chdir(/Library/Server/Mail/Data/spool): No such file or directory

    I’ve checked that the sasl_passwd and sasl_password.db files have been created and they are there but I’m betting that I can’t just simply add the two lines to main.cf as I did? Can you please help me out? Thanks in advance!

  6. This worked very nice. Thanks so much for the tutorial. I found it surprisingly difficult to locate good information on postfix.

  7. Admiring the dedication you put into your blog and detailed information you offer.
    It’s great to come across a blog every once in a while that isn’t
    the same out of date rehashed information. Fantastic read!
    I’ve saved your site and I’m including your RSS feeds to my Google account.

  8. When I try to create the Postfix lookup table it says “postmap: fatal: open database /etc/postfix/sasl_passwd.db: Permission denied”

  9. p.s. The problem is that I used BBEdit to create the sasl_passwd file. Permissions on that file must not have been right. When I learned how to use vi and created the sasl_passwd with vi… it all worked.

  10. I had this problem:

    # sudo postfix reload
    postfix/postfix-script: fatal: the Postfix mail system is not running

    # sudo launchctl
    launchd% start org.postfix.master
    launchd% quit

    # sudo postfix reload
    postfix/postfix-script: refreshing the Postfix mail system

    # sudo mailq
    Mail queue is empty

    HTH

  11. If you have Google 2-steps authentication you will also need to create an application specific password for postfix. Then repeat from step 2 of this tutorial using your application specific password instead of you usual Gmail password. Enjoy.
    Thanks a lot for your tutorial. This is THE solution that should pop up from the many solutions about this subject on the Web.

  12. hey you are awesome! I was struggling for about 5 hours, and finally did it with your tutorial ๐Ÿ™‚
    Thank you so much !

  13. Hi! Thanks for the very helpful information. I’m running into this issue, any ideas appreciated:

    postfix: warning: /etc/postfix/main.cf, line 692: overriding earlier entry: config_directory=/etc/postfix
    /usr/sbin/postconf: warning: /etc/postfix/main.cf, line 692: overriding earlier entry: config_directory=/etc/postfix
    /usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: use_sacl_cache=yes
    /usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: imap_submit_cred_file=/Library/Server/Mail/Config/postfix/submit.cred
    /usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: mydomain_fallback=localhost
    postfix/postlog: warning: /etc/postfix/main.cf, line 692: overriding earlier entry: config_directory=/etc/postfix
    postfix/postfix-script: fatal: the Postfix mail system is not running

    line 692 is the start of the #Hotmail SMTP

    I’m afraid that I may have tried to do this a year ago and perhaps tried using a comcast email account, but I don’t see any reference to comcast in the main.cf. I wish I remember what I did!!!! ๐Ÿ™‚

  14. I configured /etc/postfix/main.cf
    I get the error message:
    /usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: mydomain_fallback=localhost

    I work with OS X Yosemite.

    What do I have to change?

    1. Hi…
      Just comment (#) that line and add “noanonymous” (without quote) in this option

      smtp_sasl_security_options = noanonymous

      It’s work perfectly for me ๐Ÿ™‚ ::cheers

  15. Hello,
    I have followed the steps in this great article and all went fine until I noticed that my queued mail were not sent. I peek on the log file and noticed these following error message:
    “EF049737495: to=, relay=smtp.gmail.com[74.125.68.109]:587, delay=4.7, delays=0.06/0.07/4.6/0, dsn=4.7.0, status=deferred (SASL authentication failed; cannot authenticate to server smtp.gmail.com[74.125.68.109]: generic failure)”
    Are there any steps that I missed ?
    Looking forward for your kind responses.

    Cheers.

  16. On Mac OS X 10.10.2

    1. need comment line ‘mydomain_fallback’ and add a value of smtp_sasl_security_options

    sudo vi /etc/postfix/main.cf
    #mydomain_fallback = localhost
    smtp_sasl_security_options=noanonymous

    2. start server first before reload
    sudo postfix start

  17. Hello,
    When I opened a terminal and edit the file main.cf, i couldn’t see the following lines.
    mydomain_fallback = localhost
    mail_owner = _postfix
    setgid_group = _postdrop

    So, what should I do next? Should I add these lines? If so, where should I place them?

    Looking forward to your kind reply.

    Cheers!

  18. Thank you. These instructions were very helpful. Note that gmail will likely complain about less secure apps trying to access account if using gmail (smtp.gmail.com). Even after changing permissions on gmail to allow less secure apps, still failures. i switched to hotmail (smtp.live.com) keeping the above – plus adding “smtp_sasl_mechanism_filter = plain” to main.cf and everything worked.

  19. Post
    Author
  20. Thanks so much. This is the first time I’ve been able to get mail working with the free version of mamp and osx mavericks!

  21. EN-AshutoshT:pid 11114$ sudo postfix start
    postfix/postfix-script: starting the Postfix mail system
    postfix/postfix-script: fatal: mail system startup failed

    please help..

  22. fatal: open lock file /var/lib/postfix/master.lock: unable to set exclusive lock: Resource temporarily unavailable

    in the logs seeing above error

  23. Hi,
    I tried this tutorial, because I am having issues with the mail command under OS X yosemite. This was not working. First, when starting the postfix I got a warning:
    /usr/sbin/postconf: warning: /etc/postfix/main.cf: unused parameter: mydomain_fallback=localhost

    Perhaps not very important, but possible issue.
    Later when running a mail command as:
    echo “coucou” | mailx -s “subject” myemail@orange.fr
    I did not received any mail, and in the /var/mail/mail.log I got:
    Jan 22 00:22:09 pc1.home postfix/smtp[9897]: CLIENT wrappermode (port smtps/465) is unimplemented
    Jan 22 00:22:09 pc1.home postfix/smtp[9897]: instead, send to (port submission/587) with STARTTLS

    I have to use the port 465 to working with orange, and it seems postfix, is fixed on port 587, hence orange reject the submission.

    Do you have any idea how to fix this ?
    Is this coming from a difference between yosemite and el capitan ?

  24. It seems you also need to set “myhostname=smtp.gmail.com” in /etc/postfix/main.cf

    Also, you can review configuration settings using postfix -n

  25. NOTE:

    In OS X 10.11.1 (El Capitan) the command “mydomain_fallback” doesn’t exist.

    So is just did not use this command line and continue with the tutorial and everything will work perfectly.

    Thank you.

    ๐Ÿ™‚

  26. el capitan -I’m getting this error…

    F3C3B244064F 341 Sat Apr 23 13:47:46 hsumo@yoda-2.local
    (delivery temporarily suspended: Host or domain name not found. Name service error for name=smtp.gmail.com type=MX: Host not found, try again)

    How can I fix it?

  27. Hello
    I’ve edited files as required, and granted access to less secure apps, but I got this error from smtp.gmail.com:

    said: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1

    Any help ?
    Tnx
    Roberto

  28. All, right it is working thanks.
    I had trouble that postfix was enabled in MAMP too so couldn’t start it from cmd.
    So need disable it in MAMP before doing this manual tweaks.

    And one more useful command: postfix flush
    Which will clear the mailq, it will resend all the emails so be warned

    Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *