Send MIME/HTML emails with PHP Mailer

July 4, 2008 by Sam Davis  
Filed under Featured, PHP

Sending bog-standard textual email with PHP is easy, however if you want to send some graphic-rich emails, it can get a bit tricky.  But never fear, I've put together a simple guide of how to do just this, and you'll be up and running sending beautifully scultped emails within minutes. Firstly you need to download PHP ...
Scroll down to read more...
Full Article:

Sending bog-standard textual email with PHP is easy, however if you want to send some graphic-rich emails, it can get a bit tricky.  But never fear, I’ve put together a simple guide of how to do just this, and you’ll be up and running sending beautifully scultped emails within minutes.

Firstly you need to download PHP Mailer ZIP file, which is a free open-source script available from Sourceforge.   Once you’ve done this extract the files.  Then create a directory on your web-site, for example www.yourdomain.com/phpmailer/.  In this directory you need to upload the following files from the zip file…

class.phpmailer.php
class.smtp.php
phpmailer.lang-en.php (If you are English!)

Pretty much there now!

On the page that will be sending the email, either a “thank you” page or a receipt page, you need to place the follow PHP code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Error Reporting Featured here
error_reporting(E_STRICT);
 
//Set the Time Zone</em>, search PHP Timezone on Google for more info
date_default_timezone_set("Europe/London");
 
//Locate the Mailer file within the directory you just set up
include_once('/phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
 
 
//Build the email....
$body = "&lt;html&gt;
&lt;body&gt;
&lt;table width='600' border='0' cellspacing='5' cellpadding='5'&gt;
&lt;tr&gt;
&lt;td&gt;This is my email!&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;";
 
 
//Replace any dodgy characters
$body = eregi_replace("[\]",'',$body);
 
//Send the email!
$mail-&gt;IsSendmail();
$mail-&gt;From       = "info@yourdomain.com";
$mail-&gt;FromName   = "Your Domain";
$mail-&gt;Subject    = "YourDomain.com - Test Email";
$mail-&gt;AltBody    = "To view the message, please use an HTML compatible email viewer";
$mail-&gt;MsgHTML($body);
 
$mail-&gt;AddAddress("message@recipient.com", "James Dean");
 
if(!$mail-&gt;Send()) {
echo "Message not sent" . $mail-&gt;ErrorInfo;
} else {
echo "Message Sent!";
}

Nothing to it!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google


Echo a Google search term on your page

Comments

Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!