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 | |
---|
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%"> |
---|
16 | <tr> |
---|
17 | <td bgcolor="#CCCCCC"> |
---|
18 | <pre> |
---|
19 | require("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 | |
---|
33 | while ($row = mysql_fetch_array ($result)) |
---|
34 | { |
---|
35 | // HTML body |
---|
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"; |
---|
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"] . "<br>"; |
---|
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 | |
---|
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: |
---|
75 | |
---|
76 | <p> |
---|
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> |
---|
80 | <p> |
---|
81 | |
---|
82 | <table cellpadding="4" border="1" width="80%"> |
---|
83 | <tr> |
---|
84 | <td bgcolor="#CCCCCC"> |
---|
85 | <pre> |
---|
86 | require("class.phpmailer.php"); |
---|
87 | |
---|
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() |
---|
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 | |
---|
114 | Now here's a normal PHP page in the site, which will have all the defaults set |
---|
115 | above:<br> |
---|
116 | Normal 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> |
---|
123 | require("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 | |
---|
134 | if(!$mail->Send()) |
---|
135 | { |
---|
136 | echo "There was an error sending the message"; |
---|
137 | exit; |
---|
138 | } |
---|
139 | |
---|
140 | echo "Message was sent successfully"; |
---|
141 | </pre> |
---|
142 | </td> |
---|
143 | </tr> |
---|
144 | </table> |
---|
145 | </p> |
---|
146 | |
---|
147 | </body> |
---|
148 | </html> |
---|