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";
26
@MYSQL_CONNECT("localhost","root","password");
27
@mysql_select_db("my_company");
28
$query� =�"SELECT full_name, email,�photo�FROM employee�WHERE�id=$id";
29
$result�=�@MYSQL_QUERY($query);
31
while ($row = mysql_fetch_array ($result))
34
$body = "Hello <font size=\"4\">" . $row["full_name"] . "</font>, <p>";
35
$body .= "<i>Your</i> personal photograph to this message.<p>";
36
$body .= "Sincerely, <br>";
37
$body .= "phpmailer List manager";
39
// Plain text body (for mail clients that cannot read HTML)
40
$text_body = "Hello " . $row["full_name"] . ", \n\n";
41
$text_body .= "Your personal photograph to this message.\n\n";
42
$text_body .= "Sincerely, \n";
43
$text_body .= "phpmailer List manager";
46
$mail->AltBody = $text_body;
47
$mail->AddAddress($row["email"], $row["full_name"]);
48
$mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");
51
echo "There has been a mail error sending to " . $row["email"] . "<br>";
53
// Clear all addresses and attachments for next loop
54
$mail->ClearAddresses();
55
$mail->ClearAttachments();
63
<h3>2. Extending phpmailer</h3>
66
Extending classes with inheritance is one of the most
67
powerful features of object-oriented
68
programming. It allows you to make changes to the
69
original class for your
70
own personal use without hacking the original
71
classes. Plus, it is very
72
easy to do. I've provided an example:
75
Here's a class that extends the phpmailer class and sets the defaults
76
for the particular site:<br>
77
PHP include file: <b>mail.inc.php</b>
80
<table cellpadding="4" border="1" width="80%">
82
<td bgcolor="#CCCCCC">
84
require("class.phpmailer.php");
86
class my_phpmailer extends phpmailer {
87
// Set default variables for all new objects
88
var $From = "from@example.com";
89
var $FromName = "Mailer";
92
// Replace the default error_handler
93
function error_handler($msg) {
94
print("My Site Error");
95
print("Description:");
100
// Create an additional function
101
function do_something($something) {
102
// Place your new code here
110
Now here's a normal PHP page in the site, which will have all the defaults set
112
Normal PHP file: <b>mail_test.php</b>
115
<table cellpadding="4" border="1" width="80%">
117
<td bgcolor="#CCCCCC">
119
require("mail.inc.php");
121
// Instantiate your new class
122
$mail = new my_phpmailer;
124
// Now you only need to add the necessary stuff
125
$mail->AddAddress("josh@example.com", "Josh Adams");
126
$mail->Subject = "Here is the subject";
127
$mail->Body = "This is the message body";
128
$mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip"); // optional name
132
echo "There was an error sending the message";
136
echo "Message was sent successfully";