Published

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:

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].”

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

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…