PHP mail

written by: Stratus Huo Quan; article published: year 2007, month 11;



In: Categories » Computers and technology » PHP » PHP mail

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

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. 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 PHP code is incorrect. For example, the following code: <?php for($i=1;$i<10;$i++){ $output = "Current Iteration: " . $i . "<br>" echo $output; } ?> will return an error similar to the following: Parse error: parse error, unexpected T_ECHO in c:\webserver\test2.php on lin...

2. 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 the server, you need to use the PHP date function. You pass the date function a string, with special tokens in place of the date parameters you require. When the code is run, the tokens will be replaced with the date or time section that they represent. For example: <?php echo date("d/m/Y"); ?> outputs 11/02/2003 &...

3. 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 in the following code generates a password with a number of random characters, and you can set the length of the password when you call the function: <?php function randomPassword($length) { $possibleCharacters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $characterLength = strlen($possibleCharac...

4. 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 it from the source. Downloading PHP PHP is available as a free download from the PHP web site, http://www.php.net/ downloads.php. The file that you need to download is at the top of the page, in the section labeled Complete Source Code. The current file at the time of this writing was for PHP 4.3....

5. Installing PHP with Apache on Windows
We try to install PHP into Apache so it can process PHP pages and static HTML pages. We assume that you have installed and tested Apache. Downloading PHP The first step is to download PHP, which is available from the PHP downloads page at http://www.php.net/downloads.php. Scroll down the downloads page until you find the section labeled Windows Binaries. The current version at the time of this writing is PHP 4.3.0, and the...