3 <title>Examples using phpmailer
</title>
6 <body bgcolor=
"#FFFFFF">
8 <h2>Examples using phpmailer
</h2>
10 <h3>1. Advanced Example
</h3>
13 This demonstrates sending out multiple email messages with binary attachments
14 from a MySQL database with multipart/alternative support.
<p>
15 <table cellpadding=
"4" border=
"1" width=
"80%">
17 <td bgcolor=
"#CCCCCC">
19 require(
"class.phpmailer.php");
21 $mail = new phpmailer();
23 $mail-
>From =
"list@example.com";
24 $mail-
>FromName =
"List manager";
25 $mail-
>Host =
"smtp1.example.com;smtp2.example.com";
26 $mail-
>Mailer =
"smtp";
28 @MYSQL_CONNECT(
"localhost",
"root",
"password");
29 @mysql_select_db(
"my_company");
30 $query =
"SELECT full_name, email, photo FROM employee WHERE id=$id";
31 $result = @MYSQL_QUERY($query);
33 while ($row = mysql_fetch_array ($result))
36 $body =
"Hello <font size=\"4\
">" . $row[
"full_name"] .
"</font>, <p>";
37 $body .=
"<i>Your</i> personal photograph to this message.<p>";
38 $body .=
"Sincerely, <br>";
39 $body .=
"phpmailer List manager";
41 // Plain text body (for mail clients that cannot read HTML)
42 $text_body =
"Hello " . $row[
"full_name"] .
", \n\n";
43 $text_body .=
"Your personal photograph to this message.\n\n";
44 $text_body .=
"Sincerely, \n";
45 $text_body .=
"phpmailer List manager";
48 $mail-
>AltBody = $text_body;
49 $mail-
>AddAddress($row[
"email"], $row[
"full_name");
50 $mail-
>AddStringAttachment($row[
"photo"],
"YourPhoto.jpg");
53 echo
"There has been a mail error sending to " . $row[
"email"] .
"<br>";
55 // Clear all addresses and attachments for next loop
56 $mail-
>ClearAddresses();
57 $mail-
>ClearAttachments();
65 <h3>2. Extending phpmailer
</h3>
68 Extending classes with inheritance is one of the most
69 powerful features of object-oriented
70 programming. It allows you to make changes to the
71 original class for your
72 own personal use without hacking the original
73 classes. Plus, it is very
74 easy to do. I've provided an example:
77 Here's a class that extends the phpmailer class and sets the defaults
78 for the particular site:
<br>
79 PHP include file:
<b>mail.inc.php
</b>
82 <table cellpadding=
"4" border=
"1" width=
"80%">
84 <td bgcolor=
"#CCCCCC">
86 require(
"class.phpmailer.php");
88 class my_phpmailer extends phpmailer {
89 // Set default variables for all new objects
90 var $From =
"from@example.com";
91 var $FromName =
"Mailer";
92 var $Host =
"smtp1.example.com;smtp2.example.com";
93 var $Mailer =
"smtp"; // Alternative to IsSMTP()
96 // Replace the default error_handler
97 function error_handler($msg) {
98 print(
"My Site Error");
99 print(
"Description:");
104 // Create an additional function
105 function do_something($something) {
106 // Place your new code here
114 Now here's a normal PHP page in the site, which will have all the defaults set
116 Normal PHP file:
<b>mail_test.php
</b>
119 <table cellpadding=
"4" border=
"1" width=
"80%">
121 <td bgcolor=
"#CCCCCC">
123 require(
"mail.inc.php");
125 // Instantiate your new class
126 $mail = new my_phpmailer;
128 // Now you only need to add the necessary stuff
129 $mail-
>AddAddress(
"josh@example.com",
"Josh Adams");
130 $mail-
>Subject =
"Here is the subject";
131 $mail-
>Body =
"This is the message body";
132 $mail-
>AddAttachment(
"c:/temp/11-10-00.zip",
"new_name.zip"); // optional name
136 echo
"There was an error sending the message";
140 echo
"Message was sent successfully";