PHP mail

by Stratus Huo Quan.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on php  

You are here: Categories » Computers and technology » PHP

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.

How Do I 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).

How Do I Send a Newsletter with PHP?

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.

How Do I Stop a Script from Timing Out When Sending Many E-mails?

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) .

Leave a comment or ask a question
Total comments: 0

PHP Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
How to Install PHP into Apache - In this section, we look at how to install PHP into Apache. The first step is to download it from the PHP web site. There are other sources for PHP around the Web, but it is much easier to get (more...)
PHP Date and Time - In this tutorial, we look at commonly asked questions regarding the date function in PHP. How Do I Read the Date or Time from the Server? To read the date or time from t (more...)
PHP Random Password - In some applications, it can be useful to generate a random password, such as setting the user's initial password and then letting the user change it if he or she wishes. The function shown i (more...)
Common PHP Errors - In this tutorial, we are going to look at some of the common PHP errors that occur and how to solve them. Parse Errors A parse error occurs when the format of your (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.