In this article, we are going to look at some of the frequently asked questions regarding e-mail and PHP. We will begin by looking at a more fundamental issue: how to actually send an e-mail as HTML.
As the PHP mail function defaults to sending plain text e-mails unless otherwise specified, a frequent question is how to send HTML e-mails using the mail function. The format for the mail function is as follows:
mail($to, $subject, $message, $headers);
where $to is the e-mail address to send to, $subject is the subject line for the e-mail, $message contains the e-mail message, and $headers contains any optional headers you may wish to add.
To send an e-mail that's recognized and treated as HTML, you need to use two special headers:
MIME-Version: 1.0 Content-type: text/html; charset=iso-8859-1
These should be included in the $headers variable as shown in the following complete code:
<?php $to = "gareth@myemail.com"; $subject = "This is an HTML E-mail"; $message = " <span style=\"font-family: Arial, Helvetica, sans-serif; font-size: 20px; font-weight: bold; color: #009900\">"; $message .= "This is an HTML e-mail message"; $message .= "</span>"; $headers = "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; $headers .= "From: gareth<gareth@myemail.com>"; mail($to, $subject, $message, $headers); ?>
It's important to note that to run the code, you need to have an e-mail server set up, so it's easiest to test this code on your web host's server.
As the e-mail is being sent as HTML, you can include HTML tags in the message, as shown in the preceding code. When the e-mail is received, the e-mail will be rendered as an HTML page (assuming that the user's e-mail program has that facility).
If you wish to send a newsletter out to a number of users at once, hiding the e-mail addresses so the recipient can't see who else you sent the mail to, you can use the bcc header, which stands for "blind carbon copy." Any addresses in the bcc header will be sent a copy of the e-mail, but they can't see who else it was sent to. The following code shows a working example, which sends the e-mail to each address in the array provided:
<?php
$addresses = array("fred@cemetry.com","george@another.com");
?>
<?
$to = "gareth@myemail.co.uk";
$subject = "PHP Newsletter";
$message = "This e-mail shows how to use a BCC Header to send a newsletter";
$headers .= "From: gareth<gareth@myemail.co.uk>\n";
$headers .= "bcc: ";
$count = 0; foreach($addresses as $address){
if($count == 0){ $headers .= $address;
}else{
$headers .= ", " . $address;
}
$count ++;
}
$headers .= "\n";
mail($to, $subject, $message, $headers);
?>
In the preceding code, all the e-mail addresses are specified in the $addresses array. This could be changed to use a field from a recordset to get the e-mail addresses from a database table.
If you are sending the same e-mail to a large number of users, the previous method is the best way. This is because PHP contacts the mail server once, and then the mail server has the job of sending the e-mail to all the e-mail addresses specified, which means that the PHP script finishes quicker and uses fewer resources.
If, however, you're sending a personalized e-mail, each e-mail will have to be sent individually by PHP to add the individualized data.
If you're sending a personalized e-mail to a large number of users, you have to send each e-mail individually, and it may take a while for the script to send out all the e-mails. This creates a problem sometimes, as the script may time out before all the e-mails have been sent.
To avoid this, you need to increase the amount of time that the script can run for. The default setting is usually 30 seconds. You can increase the time limit for a script by adding the following code to the top of the page:
<?php set_time_limit(5*60); ?>
This will allow the script to run for up to 5 minutes (60 * 5 = 300 seconds) .
Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
This article was sent to us by:
Stratus Huo Quan at
11102007
1. How to Install PHP into Apache
All articles in this directory are property of their respective authors. Additionally, read our Privacy Policy
© 2010 WebWorldarticles.com - All Rights Reserved. Partners: Gunblade Saga