Sending email from your website with PHP

Written by Joe on Thursday, January 28, 2010

Sending email from your website can be a very useful feature.  When a user makes a purchase, fills out a form, forgets a password; being able to send them an automated email adds value to your website.  In this article I will go over how to send an email using PHP.

In PHP, the mail() function is used to send emails.  The mail function takes four parameters:

  1. to email
  2. email subject
  3. email message
  4. additional headers

Simple right?  The to email is the recipient you would like to send the email to.  Email subject is the subject the user first sees when recieving the email.  The message is the content of the email, which can be plain text or HTML.  Additonal headers can be anything from, a from email address, reply-to email address, carbon copy email address, blind carbon copy email and more.

Here is a simple example with no additional headers:

$to = 'email@domain.com';
$subject = 'My Email Subject';
$message = 'Thank you for filling out our form';

mail($to,$subject,$message);

Here is another example using some additional headers:

$headers .= 'To: Jane <jane@example.com>, Kim <kim@example.com>' . "rn";
$headers .= 'From: My Website <info@example.com>' . "rn";
$headers .= 'Cc: joe@example.com' . "rn";
$headers .= 'Bcc: mike@example.com' . "rn";

$to = 'email@domain.com';
$subject = 'My Email Subject';
$message = 'Thank you for filling out our form';

mail($to,$subject,$message,$headers);

As you can see, sending email messages using php is very simple.  There is much more you can do, including sending HTML emails.  Click here to read part two "Sending HTML emails from your website with PHP".

Like what you’ve read?

Link back to us.
http://www.revolutionunlimited.com/news-and-articles/php/2010-01-28/sending-email-from-your-website-with-php/

OR Join Our Mailing List

* indicates required fields

Want to drive more traffic to your website?

Our results driven search engine optimization services will get you the results you want.

Contact Us Today

Post A Comment