Send mail() using PHP on Mac OS X

Thanks to Benjamin Rojas, Andy Stratton, and a tip from Jasper, I was able to successfully send email from my home-brewed MAMP environment. Here’s the summary.

  1. Add the following to your /etc/postfix/sasl_passwd file:
    smtp.gmail.com:587 username@gmail.com:password

    (Of course, you don’t have to use GMail or port 587, but you get the idea.)

  2. Configure postfix:
    sudo postmap /etc/postfix/sasl_passwd
  3. Backup and edit your postfix configuration:
    sudo cp /etc/postfix/main.cf /etc/postfix/main.cf.orig
    sudo vim /etc/postfix/main.cf

    If you use TLS, then you will need to add the TLS settings but the other settings should already be there as a result of running the postmap command. You should have these options set in /etc/postfix/main.cf:

    mydomain_fallback = localhost
    mail_owner = _postfix
    setgid_group = _postdrop
    relayhost=smtp.gmail.com:587
    smtp_sasl_auth_enable=yes
    smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
    smtp_sasl_security_options=
    smtp_use_tls=yes
    smtp_tls_security_level=encrypt
    tls_random_source=dev:/dev/urandom
  4. Start postfix:
    sudo postfix start

    If there are errors, you may need to edit your /etc/postfix/main.cf and restart postfix:

    sudo postfix reload
  5. Send a test message:
    date | mail -s test youremailaddress@yourdomain.com
  6. Make postfix start automatically on boot by opening your /System/Library/LaunchDaemons/org.postfix.master.plist file and adding:
    <key>RunAtLoad</key>
    <true/>

    Add this at the bottom just before the closing </dict> tag.

  7. Edit your /etc/php.ini file and configure the sendmail_path option:
    sendmail_path = "sendmail -t -i"

You should now be able to send email using PHP’s mail() function. If you continue to have issues, watch the contents of your postfix mail log:

tail -f /var/log/mail.log

4 thoughts on “Send mail() using PHP on Mac OS X”

  1. Thank you, but for me there was a very important thing:

    I have to change the sender address
    >sudo nano /etc/postfix/sender_canonical

    myaccount meineemail@meiserver.com

    save this, then
    >postmap /etc/postfix/sender_canonical

    this must in the main.cf
    sender_canonical_maps=hash:/etc/postfix/sender_canonical

    then
    >postfix reload

  2. Thanks a lot, but………
    for me it’s sounds ( and is) to complicated. Isn’t there just a way of entering ‘somewhere’ my smtp credentials?
    Php mail on Windows just works. Never problems there.
    Regards,
    Ab

Leave a Reply

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