May 12, 2012

f Comment

How to Send Emails on a Unix machine with PHP?

Amazon It is unbelievably difficult to find a comprehensive tutorial on sending emails with PHP or any programming language. They only talk about what PHP functions to use and that's it. Why they neglect THE MOST important step is mystery to me. Lucky you found this tutorial that will teach you exactly what you need to do to send emails. Read on.

How does Email Work?
First you need a basic understanding of how email works. Email uses the client-server architecture to send and receive emails over the internet.

When you provide information such as From, To, Subject, Body, you use an email client (e.g. Outlook Express, or any programming API that supports this function) to properly encode them to a format that an email server would understand. Then your email client relays the data to an email server, known as the SMTP server.

SMTP is a series of protocols that define how emails are relayed from one server to another. They include mail transfer agent (MTA), mail submission agent (MSA), mail user agent (MUA), and message delivery agent (MDA). Each of them has their specific job. You don't need to bother yourself with the details. Just know that once your email client passes the data to your SMTP server (which is what we commonly refer to MTA as), it sends the data to a set of agents, also called SMTP clients, before it finally reaches the destination SMTP server.

The destination's SMPT server relays the message to its POP3 or IMAP server, which defines how the messages are stored. In a POP3 server the message is deleted as soon as it has been downloaded into the recipient's email client (e.g. Outlook, Gmail, Yahoo). In an IMAP server the message is kept until it's told to be deleted. My recipient then uses the email client to download the emails from the POP3 or IMAP server.

How does programmatically sending emails work?

1. First, you run an SMTP server such as Postfix on local port 25.

2. You call PHP's email sending function such as mail() to put the email together and send the email's content to the SMTP server following the standard SMTP protocols.

That's it! However, your emails are likely to end up in the recipient's spam or junk folder. If you don't want that to happen, follow the following the steps.

3. Sign up for Amazon SES and validate the ownership of your email address.

4. Edit the SMTP server's configuration file, which is the main.cf file in the /etc/postfix folder, so that it uses the Amazon host as the relay host, which many major email servers such as Yahoo and Gmail accept as a trusted host.

That's it. Now whenever you send an email from a PHP program, Amazon will handle the email on your behalf and make sure th email ends up in the Inbox folder of the recipient's email box.

Read on to see more detail of step 1 and step 2. Read Step-by-Step Guide on Sending Emails PROGRAMMATICALLY and RELIABLY with Amazon SES, Postfix, Zoho! to see more detail of step 3 and step 4.

Solution
Now it's easy to see you need an email client and an SMTP server to send emails. Here are the steps to send emails from a PHP program on a Unix or Linux machine. If you are using Windows read How to Install SMTP Server on Windows!

Step 1: Install an SMTP Server Locally
If you use a modern version of Unix most likely you can install Postfix, which is a free SMTP server. I am using Ubuntu 10. Run the following command to install Postfix.

sudo apt-get install postfix
Once you are done you should have the SMTP server running on port 25. You can easily check if that's the case by issuing this command:

sudo lsof -i:25
If it is then you should see something like this:

ubuntu@ip-10-116-41-207:~$ sudo lsof -i:25
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
master 12250 root 12u IPv4 243596 0t0 TCP *:smtp (LISTEN)
ubuntu@ip-10-116-41-207:~$

If not let me know!

This means that you have an SMTP server running at your machine at port 25. To send emails with PHP your PHP engine needs to know that it needs to connect to localhost at port 25. By default this is the case, but if this is not the case you need to configure your PHP correctly (search for 'SMTP' in your php.ini). If you install your SMTP server somewhere else you need to configure your PHP by specifying the server's host name and port (usually 25) in php.ini so that it connects to that SMTP server to send emails.

Step 2: Use mail()
In PHP the function that acts as the email client is mail(). Here is a sample code:
<?php
 $to = "recipient@someDomain.com";
 $subject = "Greetings";
 $body = "Hello, How are you?";
 if (mail($to, $subject, $body)) {
   echo("<p>Message successfully sent!</p>");
  } else {
   echo("<p>Message delivery failed...</p>");
  }
 ?>
 
Simply use actual email address for $to and run this code and your $to email should get this message. If not check again if SMTP server is running at port 25. If you still have trouble let me know!

However, you may find that the emails you send end up in the spam folder. Please read Step-by-Step Guide on Sending Emails PROGRAMMATICALLY and RELIABLY with Amazon SES, Postfix, Zoho! to see how to avoid emails ending up in the spam folder.

In case you are using the Yii framework to build your application, you can install a Yii extension that acts as an email client. One such extension that works for me is the 'email extension' which can be found at http://www.yiiframework.com/extension/email.

Any questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael