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

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

Related Articles

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

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

3. Benefits of PHP programmers in freelance programming industry
Before you hire a contract programmer to put more features into your website, you need to know which programming language will be helpful for your website as well as afford...

4. How PHP useful for business and individual
PHP has become a very reliable and infallible platform for building complex websites. One of the most important and most powerful constituent elements of PHP is its ability...

5. PHP Development: Turn Your Website into Multi Featured Web Application
Are you looking for developing a web application with charming features which have a range integrated database? PHP is one of the best technologies for web applications int...

6. How to choose a Programming Language for Web Application
Web Application is an application that is accessed over the Internet and hosted in a browser-controlled environment. Web application is developed using...

7. Advantages of Using PHP Frameworks in Contract PHP Programming
PHP is known as server side scripting language and used in all known operating systems like Windows, UNIX, Linux etc. Nowadays PHP frameworks are a step ahead of the genera...

8. Tips on how to hire a PHP developer
PHP is an open-source, general-purpose scripting language that is particularly suited for Web Development & can be written in HTML. PHP is a strong Server-Side language...

9. How Programming Languages Work
OUR most fundamental tool as intelligent beings is language. It is via language that you learn new information and share your knowledge, feelings, and experiences with ot...