PERSONAL WEBSITE OF DESIGNER + DEVELOPER STEVE MCDONALD
  • running shoes that fit
  • iPod repair replacement upgrade
  • Klout.com
  • Flitter.com
  • Tweetdeck
  • ham antenna tight connection not so tight
  • ham antenna loose connection
  • apartment antenna
  • Canon T2i HDSLR
  • Depth of Field
  • Canon XH-A1 video camera
  • Letus on an XH-A1

Send eMail In Drupal Development


So you want to test out a local drupal workflow/use-case which culminates in the sending of an email… but by default you aren’t running an SMTP service.  Rather than rushing out your dev product to a live host, here is a little write up on how to setup SMTP under OSX while developing under a localhost LAMP stack.

Setting Up Your Environment
First, this only works on OSX (not sure which versions exactly… I am running Snow Leopard but I would imagine this to work under Leopard as well). If you are looking for an SMTP solution for a Windows OS I have no advice for you at the moment.

Next, you need to get some kind of localhost LAMP stack working. This could be MAMP or XAMP.  I am using Drupal under the Acquia Dev Desktop LAMP stack.

Acquia Dev Desktop Control Panel
I am going to assume at this point that you know how to work with a LAMP stack so no further testing here.  Lets imagine that you are ready to send an email message using PHP (either you are writing that yourself or in my case I am wanting to use a Drupal Contributed Module called “Rules” to send an email in certain situations). What we need to do is:

  1. Get your SMTP service up and running.
  2. Configure the SMTP service to talk with your localhost.

 

Get your SMTP service up and running
You may have googled your way around the web looking for a local SMTP service to add to your computer, or to reroute email messages.  You shouldn’t need any of that.  You have everything you need, already.

Within OSX there is a service called postfix which will handle sending emails. Here is what we want to do:

First, lets test to be certain that it is not already running. To do this we need to open the application Terminal (it should be in your Applications > Utilities folder).
Terminal Login

Once you have the terminal window open you need to run the command:

telnet 127.0.0.1 25

This should return the message “telnet: Unable to connect to remote host” if postfix is not running.
Terminal Telnet to SMTP Service fail

Configure the SMTP service to talk with your localhost

Now, before we get postfix running, we might want to configure postfix.  So lets find the config file and get postfix configured to work with localhost.  To do this, go to the Finder application and from the system menu select… Go > Connect to Folder … and now enter… /etc …and press “GO”. This gets us into a hidden folder on your computer.  Locate the file… main.cf …under… /etc/postfix/
Finder finding /etc/postfix/main.cf

Before we edit the file we need to grant ourselves permission to edit it. Rick click the file and select Get Info.
Get Info to update permissions for editing main.cf under Postfix

From the Sharing and Permissions section hit the lock button to unlock the permissions property (you will have to enter your system password). Next, add your user name to the permissions list by hitting the “+” button in the lower left and then grant yourself Read and Write permissions.
GetInfo permissions changes

Lock it back up again and close up the Info window. And now right click on the main.cf file and open it in TextEdit.
TextEdit Application editing Main.cf for PostFix SMTP service configuration

There are a few lines you need to find and then add within the file:

Find: #myhostname =
After this line add the new line: myhostname = localhost

Find: #inet_interfaces =
After this line add the new line: inet_interfaces = localhost

Find: #mydestination =
After this line add the new line: mydestination = $myhostname, localhost

Next up we want to start up the postfix service. Enter the following command into the Terminal window…

sudo launchctl start org.postfix.master

..and press enter. It should prompt you for your password. Enter it and hit return.
Terminal starting postfix as super user

Nothing will appear to have happened at this point.  We need to test our connection again. To do that, again we enter…

telnet 127.0.0.1 25

…and this time we should see that we successfully connected to localhost.
Terminal Telnet to SMTP Service success

Good.  You should be able to send mail using your own PHP code OR in the case of Drupal, your contact forms should work now (I just sent a message from my contact form to my inbox successfully).

You need to know one more command at this point.

How To Shut Down The Postfix service
You don’t want to leave that running and spamming people or just taking away cycles from your CPU.  Here is how you turn it off again. Just type the command…

sudo launchctl stop org.postfix.master

..and hit enter. If you test again you will see that you are no longer connected. Good luck!

(You can read more about sending email from within a drupal dev environment on drupal.org!)

Blog: Development

Tags