Post Reply 
Using PHP to send mails
07-23-2011, 04:03 PM
Post: #1
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 an email alert to your technical administrator whenever an error occurs
... and there are many many more reasons to send mails.

Now, 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.
PHP Code:
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.
PHP Code:
// Set up parameters
$to "larry_crowne@example.com";
$subject "Thank you for registering on FreeVps.us";
$message "Hello Larry, Welcome to freevps.us! We hope that you enjoy your stay";
$from "no-reply@freevps.com";
$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.

PHP Code:
// Set up parameters
$to "larry_crowne@example.com";
$subject "Thank you for registering on FreeVps.us";
$message "Hello Larry, Welcome to freevps.us! We hope that you enjoy your stay";
$from "no-reply@freevps.com";
$headers "From: $from";

//THIS IS THE EXTRA SET OF HEADERS

//IF YOU ARE USING WINDOWS THEN USE THESE 3 LINES
$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.


Please let me know if I have missed some point or if something is not clear.

I hope you guys find this tutorial useful.

.
.
Thank you freeVps and Loomhosts for the amazing VPS
.
.
User Tools
Quote this message in a reply


07-23-2011, 04:17 PM
Post: #2
RE: Using PHP to send mails
Fantastic. Thanks for sharing.

Patience has a limit and if you choose to be patient beyond this limit then it causes anger.
User Tools
Quote this message in a reply
07-23-2011, 06:30 PM
Post: #3
RE: Using PHP to send mails
Whoa. Nice tutorial
User Tools
Quote this message in a reply
07-24-2011, 12:01 AM
Post: #4
RE: Using PHP to send mails
Most of the causes of emails being marked as spam are from the sending server being misconfigured, server being blacklisted or sending too much mail to one mail provider too fast.
User Tools
Quote this message in a reply
07-27-2011, 09:10 PM
Post: #5
RE: Using PHP to send mails
Good job the way it should look like when someone tries.
User Tools
Quote this message in a reply
Post Reply 


Forum Jump:



User(s) browsing this thread:
1 Guest(s)