Using PHP to send mails

Using PHP to send mails

We all know that PHP is one of the most widely used web scripting languages. When we create websites or web-applications (whether using PHP or not), we at many times need to send emails. We may use mails to

  • Send updates or newletters to a pre-compiled email list
  • Sending invoices or receipts to users after they successfully pay or purchase your products/services
  • Sending “Welcome mail” to new members of your website/web-application
  • Sending email alerts to your server admin whenever an server / site error occurs

… and there are many many more reasons to send mails.

PHP has an inbuilt function called mail().

Let us discuss that. The PHP mail() function accepts 5 parameters. Please note that not all 5 parameters are mandatory.

mail($to,$subject,$message,$headers,$parameters)
  • $to is the recipient email address. This is the email address you want to send the mail to
  • $subject is the text that you want to appear in the subject part of the mail
  • $message is the actual text containing the body of the email
  • $headers is the hidden part of message that is required for the email client to understand the nature of the message (for eg. the sender’s email address, sender’s name etc.)
  • $parameters is the set of extra information that you wish to send with your email

Only the first three parameters are mandatory (i.e.) To email address, email subject and the actual message that you want to send.

Now that we understand the mail() function. Let us see a simple example.

// Set up parameters
$to = "[email protected]";
$subject = "larry thanks for signup";
$message = "Hello Larry, Welcome to our site! ";
$from = "[email protected]";
$headers = "From: $from";

// Send email
mail($to,$subject,$message,$headers); 

Thats it !

Now, the example that we just saw sends only plain text as the body of the email. What if you want to send some HTML as well ??

It can be made possible, by adding a little bit more information in your email headers.

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: $from" . "\r\n";

//IF YOU ARE USING A UNIX BASED SYSTEM THEN THESE 3 LINES
$headers = "MIME-Version: 1.0" . "\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\n";
$headers .= "From: $from" . "\n";

// Send email
mail($to,$subject,$message,$headers); 

Please note that, you have to separate headers by introducing a line break in between them.

For windows system we use “\r\n” to introduce line breaks.
For unix bases system we use “\n” to introduce line breaks.

Isn’t it easy ??

Limitations: (yes there are limitations!)

  • Because of its ease of use, people have used mail() function to spoof and send fake emails. So many times emails sent using mail() function end up in spam box (this is what I have heard and sometimes faced myself, not sure how true it is)
  • It is slow to send bulk emails.This is because each time you call mail(), PHP opens the mail socket, sends the mail, and then closes it again.

So what is the solution for this ? Use SMTP to send emails. We will see about it in a follow up post. But for now, lets enjoy the power and simplicity of the PHP mail() function.