source: hadoop-register/etc/phpMailer/docs/extending.html @ 57

Last change on this file since 57 was 57, checked in by jazz, 15 years ago
  • 第一版 Hadoop 叢集之帳號申請網頁 by Wade
File size: 3.8 KB
Line 
1<html>
2<head>
3<title>Examples using phpmailer</title>
4</head>
5
6<body bgcolor="#FFFFFF">
7
8<h2>Examples using phpmailer</h2>
9
10<h3>1. Advanced Example</h3>
11<p>
12
13This demonstrates sending out multiple email messages with binary attachments
14from a MySQL database with multipart/alternative support.<p>
15<table cellpadding="4" border="1" width="80%">
16<tr>
17<td bgcolor="#CCCCCC">
18<pre>
19require("class.phpmailer.php");
20
21$mail = new phpmailer();
22
23$mail->From     = "list@example.com";
24$mail->FromName = "List manager";
25$mail->Host     = "smtp1.example.com;smtp2.example.com";
26$mail->Mailer   = "smtp";
27
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);
32
33while ($row = mysql_fetch_array ($result))
34{
35    // HTML body
36    $body  = "Hello &lt;font size=\"4\"&gt;" . $row["full_name"] . "&lt;/font&gt;, &lt;p&gt;";
37    $body .= "&lt;i&gt;Your&lt;/i&gt; personal photograph to this message.&lt;p&gt;";
38    $body .= "Sincerely, &lt;br&gt;";
39    $body .= "phpmailer List manager";
40
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";
46
47    $mail->Body    = $body;
48    $mail->AltBody = $text_body;
49    $mail->AddAddress($row["email"], $row["full_name"]);
50    $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");
51
52    if(!$mail->Send())
53        echo "There has been a mail error sending to " . $row["email"] . "&lt;br&gt;";
54
55    // Clear all addresses and attachments for next loop
56    $mail->ClearAddresses();
57    $mail->ClearAttachments();
58}
59</pre>
60</td>
61</tr>
62</table>
63<p>
64
65<h3>2. Extending phpmailer</h3>
66<p>
67
68Extending classes with inheritance is one of the most
69powerful features of object-oriented
70programming.  It allows you to make changes to the
71original class for your
72own personal use without hacking the original
73classes.  Plus, it is very
74easy to do. I've provided an example:
75
76<p>
77Here's a class that extends the phpmailer class and sets the defaults
78for the particular site:<br>
79PHP include file: <b>mail.inc.php</b>
80<p>
81
82<table cellpadding="4" border="1" width="80%">
83<tr>
84<td bgcolor="#CCCCCC">
85<pre>
86require("class.phpmailer.php");
87
88class 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()
94    var $WordWrap = 75;
95
96    // Replace the default error_handler
97    function error_handler($msg) {
98        print("My Site Error");
99        print("Description:");
100        printf("%s", $msg);
101        exit;
102    }
103
104    // Create an additional function
105    function do_something($something) {
106        // Place your new code here
107    }
108}
109</td>
110</tr>
111</table>
112<br>
113
114Now here's a normal PHP page in the site, which will have all the defaults set
115above:<br>
116Normal PHP file: <b>mail_test.php</b>
117<p>
118
119<table cellpadding="4" border="1" width="80%">
120<tr>
121<td bgcolor="#CCCCCC">
122<pre>
123require("mail.inc.php");
124
125// Instantiate your new class
126$mail = new my_phpmailer;
127
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
133
134if(!$mail->Send())
135{
136   echo "There was an error sending the message";
137   exit;
138}
139
140echo "Message was sent successfully";
141</pre>
142</td>
143</tr>
144</table>
145</p>
146
147</body>
148</html>
Note: See TracBrowser for help on using the repository browser.