in Apple, iPhone, Mac

PHP sendmail on OS X Server (Yosemite)

Setting up Postfix

The org.postfix.master.plist file located at /System/Library/LaunchDaemons/ is used to start or stop postfix on demand, as and when any email is submitted to the mail system for processing. Open up the file in TextWrangler and make it match the following

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.postfix.master</string>
<key>Program</key>
<string>/usr/libexec/postfix/master</string>
<key>ProgramArguments</key>
<array>
<string>master</string>
<string>-e</string>
<string>60</string>
</array>
<key>QueueDirectories</key>
<array>
<string>/var/spool/postfix/maildrop</string>
</array>
<key>AbandonProcessGroup</key>
<true/>
<key>OnDemand</key>
<true/>
</dict>
</plist>

Note that the location of ‘maildrop’ may differ for you (for example, above is what I already had on my computer, which is different from what you’d find in the original article.  For example, in Mountain Lion, it used to be ‘/Library/Server/Mail/Data/spool/maildrop’.

Step 2: Editing the ‘main.cf’ file

Type the following in Terminal

1 cd /etc/postfix
2 sudo cp main.cf main.cf.orig

The first line ‘cd’ stands for ‘change directory’. ‘sudo’ tells it to run the following command with superuser permission. ‘cp’ stands for ‘copy’. So all in all, above says ‘change directory to /etc/postfix, then copy main.cf and paste it as main.cf.orig’. You might have figured it out already but basically this saves the original file as a backup.

Now open main.cf in TextWrangler, and copy and paste the following in at the end of the file:

# Set the relayhost to the Gmail Server. Replace with your SMTP server as needed
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.
smtp_generic_maps = hash:/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_password_maps=hash:/etc/postfix/sasl/passwd
smtp_sasl_security_options = noanonymous
smtp_sasl_mechanism_filter = plain

 

Step 3: Editing the ‘aliases’ file

“We… need to allow mails sent to the root ID to your local user mailbox.” Fire up the Terminal, then type in the following commands.

 

cd /etc/postfix
whoami                # This will provide your local user name
sudo cp aliases aliases.orig

Open up ‘aliases’ (it has no extension) in TextWrangler and find the line that says

#root:              you

and replace the “you” with the username provided by the whoami command above.  Also, remove the “#” from beginning of the line. After saving the changes, go back to the Terminal, then type

sudo newaliases

 

Step 4: Editing the ‘generic’ file

“This file maps the local user address (usually of the form yourid@machine.local) to a valid Internet email address you would like to use when sending mails to the outside world. In our case, it would basically map your Unix user name to the Gmail [username].”

Fire up the Terminal, and type in

cd /etc/postfix
whoami                # This will provide your local user name, &lt;yourusername&gt;
hostname              # This will provide your machine name, &lt;machinename&gt;
sudo cp generic generic.orig

Note that <gmailusername> should include the @gmail.com; i.e. example@gmail.com. Save the changes and go back to the terminal and run the following command

sudo postmap generic

 

Step 5: Editing the ‘passwd’ file

“In this step, we store the SMTP authentication (user ID and password) for Gmail, so that postfix can connect as any other SMTP client to Gmail via an authenticated session.”

Back to the Terminal, then

sudo mkdir -p /etc/postfix/sasl    # In case the directory does not exist
cd /etc/postfix/sasl
sudo vim passwd

The last command opens up the dreaded vim. Afterwards, type the following in

smtp.gmail.com:587    &lt;gmailusername&gt;:&lt;gmailpassword&gt;

Replace <gmailusername> with the ID you use for Gmail (with the “@gmail.com” added at the end), and <gmailpassword> with the password you use to login to Gmail. After making the edit, press Escape (to make sure that you’re not in ‘INSERT’ mode) and then type ‘:w’ and press Enter to write the file, and then type ‘:q’ and press Enter to close the VIM editor.

[Alternatively, if you want to use TextWrangler to create the passwd file, make sure to set the permission to 0600 by running

chmod 0600 /etc/postfix/sasl/*

Not doing this will mean that you’ll run into ‘postmap: fatal: open database passwd.db: Permission denied’ error when you run the command below.]
Save the changes and go back to the terminal and run the following command

sudo postmap passwd

Setting up your web server

This is the last step. You need to edit your php.ini Find the following

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

and change it to

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "sendmail -t -i"

Make sure you restart your web server after you make the changes to php.ini; i.e. go into the Server.app and then flick the On/Off switch for the Web server.

Testing

So to test, we can run the following commands in the Terminal:

# first to load things up
cd /System/Library/LaunchDaemons
sudo launchctl load -w org.postfix.master.plist
cd ~ # as recommended in the original article, just to be safe

# Let's try sending email out
mail 
# It'll first ask you to type in the Subject
# Pressing Enter would let you enter the body of the email
# Pressing Ctrl + D at a blank line (press Enter after your last line of text)
# to send the email

 

If it did work, you should see the email in your Gmail. Note that it often gets put into the Spam box so be sure to check there.

One important thing which puzzled me at this stage is that, when you send a test message, make sure you give the test email a proper title and a body text. Otherwise, Google’s SMTP server will consider your test message to be a spam and not send it… If you’re unsure if it’s sending it or not just open up ‘/var/log/mail.log’ and have a look. It’s also worth running the ‘mail’ command in Terminal again to see the inbox. In my case, I had a message titled ”Undelivered Mail Return” which told me that something was going wrong…