Skip to content

Instantly share code, notes, and snippets.

@anandkkpr
Last active September 30, 2020 19:44
Show Gist options
  • Save anandkkpr/896cfd25fd3d4d12661b to your computer and use it in GitHub Desktop.
Save anandkkpr/896cfd25fd3d4d12661b to your computer and use it in GitHub Desktop.
Postfix+PHP+Drupal on OSX 10.11.11 (El Capitan) for Software Developers (a.k.a software development and testing).

How to get mail sending to work on OSX for development purposes ONLY.

OSX comes with Postfix pre-installed. All that needs to be done is to configure it AND PHP. There are a couple of configuration files involved. Each file that needs to be adjusted is attached to this gist along with the relevant configuration options and settings.

You will start by editing the Postfix config file located at: /private/etc/postfix/main.cf. You will then edit your PHP config file (php.ini) which, for me I installed via macports and was thus located at: /opt/local/etc/php56/php.ini.

These are the two pages that I used to get things working:

  1. How to send emails from localhost (MAC OS X El Capitan)
  2. Macports: How to set-up Postfix

After running those steps above, I was able to get Drupal to send out emails from my local environment as I was testing mail functionality in it.

One more thing to note...

I've set up SMTP for Postfix so that I could test emails from any network. Using this "sasl_passwd" method is not necessary if you know the outgoing SMTP server of your ISP and you know that they have an active and un-authenticated SMTP server (like TekSavvy's Outgoing SMTP Mail Server). This didn't work for me because I'm not always working from home - I sometimes visit client offices and work from there. As such, setting up Postfix to send email via authenticated SMTP was the next best option. I have a Gmail account with 2-factor authentication which is great - it means that when I stored the password in the sasl_passwd file, I didn't need to worry TOO much about it because, having 2-factor auth enabled for Gmail meant that I could generate an "App password" and use that. So... keep that in mind!

#================ PATH TO FILE ================
# /private/etc/postfix/main.cf
# OR, you can use OSX's aliased folder:
# /etc/postfix/main.cf
# both are the same thing.
#==============================================
# Just an identifier and doesn't need to be a real sub-domain or host.
myhostname = YOUR.DOMAIN.COM
# Just an identifier and doesn't need to be a real domain.
mydomain = p-c.io
# Again, just an identifier and doesn't need to be a real domain, this is
# why it's set to the same value as "mydomain".
myorigin = $mydomain
# THIS IS IMPORTANT - it blocks all other computers except the host from using
# the Postfix service to send mail.
mynetworks_style = host
####################################
# THIS IS THE KEY SECTION - this needs to be adjusted accordingly. None of
# the "smtp_..." values were present in my default "main.cf" file.
#
# Source: http://www.developerfiles.com/how-to-send-emails-from-localhost-mac-os-x-el-capitan/
####################################
relayhost = [smtp.gmail.com]:587
# Postfix 2.2 uses the generic(5) address mapping to replace local fantasy email
# addresses by valid Internet addresses. This mapping happens ONLY when mail
# leaves the machine; not when you send mail between users on the same machine.
#
# IMPORTANT NOTE: Though this file exists, you will need to generate it's DB file.
# This will need to be done via CLI:
# E.g.: `$ cd /etc/postfix && sudo postmap generic`
# Running the above will create a "generic.db" file in the same folder.
smtp_generic_maps = hash:/opt/local/etc/postfix/generic
# These settings (along with the relayhost setting above) will make
# postfix relay all outbound non-local email via Gmail using an
# authenticated TLS/SASL session.
smtp_tls_loglevel=1
smtp_tls_security_level=encrypt
smtp_sasl_auth_enable=yes
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain
tls_random_source = dev:/dev/urandom
# IMPORTANT NOTE: This file will not exist. You will need to create it (see the
# documentation source URL in the section note above). Once it's been created,
# you'll need to generate a "sasl_passwd.db" file from it using the "postmap"
# command from CLI.
# E.g.: `$ cd /etc/postfix && sudo postmap sasl_passwd`.
# Running the above will create a "sasl_passwd.db" file in the same folder.
smtp_sasl_password_maps=hash:/etc/postfix/sasl_passwd
# I'm not sure these were really necessary. I implemented them based upon advice
# I found for the macports Postfix. That said, Postfix is working on my local and
# as such, I'm leaving them here.
# Source: https://trac.macports.org/wiki/howto/SetupPostfix
#
# IMPORTANT NOTE: This file will also need to have a DB version generated.
# Unlike the "sasl_passwd" and "generic" files, this file will be generated using
# a different command in CLI.
# E.g.: `$ cd /etc/postfix && sudo newaliases`.
# Running the above will create an "aliases.db" file in the same folder.
alias_maps = hash:/etc/postfix/aliases
alias_database = hash:/etc/postfix/aliases
;================ PATH TO FILE ================
; I use macports so this is the path to where its PHP's php.ini is:
; /opt/local/etc/php56/php.ini
;==============================================
[mail function]
; For Win32 only.
; http://php.net/smtp
;SMTP = localhost
; http://php.net/smtp-port
;smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = [email protected]
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
sendmail_path = "/usr/sbin/sendmail -t -i"
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =
; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header = On
; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
mail.log = /var/log/mail.log
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog
#================ PATH TO FILE ================
# /private/etc/postfix/sasl_passwd
# OR, you can use OSX's aliased folder:
# /etc/postfix/sasl_passwd
# both are the same thing.
# IMPORTANT NOTE: I don't know if code comments are allowed in this file.
# DELETE ALL THESE COMMENTS if you are going to copy/paste this text.
#==============================================
[smtp.gmail.com]:587 [email protected]:your_password
<?php
// You can run this file from the CLI to test if mail is working from PHP.
// Remember to update the $from and $to values.
//
// E.g.: `$ php /path/to/sendmail-test.php`.
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$from = "[email protected]";
$to = "[email protected]";
$subject = "Testing PHP Mail: ". date("l jS \of F Y h:i:s A");;
$message = "This is a test to check the PHP Mail functionality";
$headers = "From:" . $from;
mail($to,$subject,$message, $headers);
echo "Test email sent";
?>
#!/opt/local/bin/zsh
# THIS IS A ZSH script. You will need to have ZSH installed.
# The path above is where Macports installs ZSH.
#
# IMPORTANT: Don't forget to update the following
# values in the script below:
# - HELO
# - MAIL FROM:
# - RCPT TO:
# - From:
# - To:
#
# E.g. `$ chmod u+x /path/to/sendmail-test.sh && /path/to/sendmail-test.sh`
setopt X_Trace
telnet localhost smtp <<-EOF
HELO YOUR-COMPUTER-NAME.local
MAIL FROM: [email protected]
RCPT TO: [email protected]
DATA
Subject: Test Mail
From: [email protected]
To: [email protected]
Some Text
.
QUIT
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment