f7c3200afca00007e2ec1e4cbffa09cc1b599b85
[core.git] / inc / classes / third_party / php_mailer / docs / extending.html
1 <html>\r
2 <head>\r
3 <title>Examples using phpmailer</title>\r
4 </head>\r
5 \r
6 <body bgcolor="#FFFFFF">\r
7 \r
8 <h2>Examples using phpmailer</h2>\r
9 \r
10 <h3>1. Advanced Example</h3>\r
11 <p>\r
12 \r
13 This demonstrates sending out multiple email messages with binary attachments\r
14 from a MySQL database with multipart/alternative support.<p>\r
15 <table cellpadding="4" border="1" width="80%">\r
16 <tr>\r
17 <td bgcolor="#CCCCCC">\r
18 <pre>\r
19 require("class.phpmailer.php");\r
20 \r
21 $mail = new phpmailer();\r
22 \r
23 $mail->From     = "list@example.com";\r
24 $mail->FromName = "List manager";\r
25 $mail->Host     = "smtp1.example.com;smtp2.example.com";\r
26 $mail->Mailer   = "smtp";\r
27 \r
28 @MYSQL_CONNECT("localhost","root","password");\r
29 @mysql_select_db("my_company");\r
30 $query  = "SELECT full_name, email, photo FROM employee WHERE id=$id";\r
31 $result = @MYSQL_QUERY($query);\r
32 \r
33 while ($row = mysql_fetch_array ($result))\r
34 {\r
35     // HTML body\r
36     $body  = "Hello &lt;font size=\"4\"&gt;" . $row["full_name"] . "&lt;/font&gt;, &lt;p&gt;";\r
37     $body .= "&lt;i&gt;Your&lt;/i&gt; personal photograph to this message.&lt;p&gt;";\r
38     $body .= "Sincerely, &lt;br&gt;";\r
39     $body .= "phpmailer List manager";\r
40 \r
41     // Plain text body (for mail clients that cannot read HTML)\r
42     $text_body  = "Hello " . $row["full_name"] . ", \n\n";\r
43     $text_body .= "Your personal photograph to this message.\n\n";\r
44     $text_body .= "Sincerely, \n";\r
45     $text_body .= "phpmailer List manager";\r
46 \r
47     $mail->Body    = $body;\r
48     $mail->AltBody = $text_body;\r
49     $mail->AddAddress($row["email"], $row["full_name"]);\r
50     $mail->AddStringAttachment($row["photo"], "YourPhoto.jpg");\r
51 \r
52     if(!$mail->Send())\r
53         echo "There has been a mail error sending to " . $row["email"] . "&lt;br&gt;";\r
54 \r
55     // Clear all addresses and attachments for next loop\r
56     $mail->ClearAddresses();\r
57     $mail->ClearAttachments();\r
58 }\r
59 </pre>\r
60 </td>\r
61 </tr>\r
62 </table>\r
63 <p>\r
64 \r
65 <h3>2. Extending phpmailer</h3>\r
66 <p>\r
67 \r
68 Extending classes with inheritance is one of the most\r
69 powerful features of object-oriented\r
70 programming.  It allows you to make changes to the\r
71 original class for your\r
72 own personal use without hacking the original\r
73 classes.  Plus, it is very\r
74 easy to do. I've provided an example:\r
75 \r
76 <p>\r
77 Here's a class that extends the phpmailer class and sets the defaults\r
78 for the particular site:<br>\r
79 PHP include file: <b>mail.inc.php</b>\r
80 <p>\r
81 \r
82 <table cellpadding="4" border="1" width="80%">\r
83 <tr>\r
84 <td bgcolor="#CCCCCC">\r
85 <pre>\r
86 require("class.phpmailer.php");\r
87 \r
88 class my_phpmailer extends phpmailer {\r
89     // Set default variables for all new objects\r
90     var $From     = "from@example.com";\r
91     var $FromName = "Mailer";\r
92     var $Host     = "smtp1.example.com;smtp2.example.com";\r
93     var $Mailer   = "smtp";                         // Alternative to IsSMTP()\r
94     var $WordWrap = 75;\r
95 \r
96     // Replace the default error_handler\r
97     function error_handler($msg) {\r
98         print("My Site Error");\r
99         print("Description:");\r
100         printf("%s", $msg);\r
101         exit;\r
102     }\r
103 \r
104     // Create an additional function\r
105     function do_something($something) {\r
106         // Place your new code here\r
107     }\r
108 }\r
109 </td>\r
110 </tr>\r
111 </table>\r
112 <br>\r
113 \r
114 Now here's a normal PHP page in the site, which will have all the defaults set\r
115 above:<br>\r
116 Normal PHP file: <b>mail_test.php</b>\r
117 <p>\r
118 \r
119 <table cellpadding="4" border="1" width="80%">\r
120 <tr>\r
121 <td bgcolor="#CCCCCC">\r
122 <pre>\r
123 require("mail.inc.php");\r
124 \r
125 // Instantiate your new class\r
126 $mail = new my_phpmailer;\r
127 \r
128 // Now you only need to add the necessary stuff\r
129 $mail->AddAddress("josh@example.com", "Josh Adams");\r
130 $mail->Subject = "Here is the subject";\r
131 $mail->Body    = "This is the message body";\r
132 $mail->AddAttachment("c:/temp/11-10-00.zip", "new_name.zip");  // optional name\r
133 \r
134 if(!$mail->Send())\r
135 {\r
136    echo "There was an error sending the message";\r
137    exit;\r
138 }\r
139 \r
140 echo "Message was sent successfully";\r
141 </pre>\r
142 </td>\r
143 </tr>\r
144 </table>\r
145 </p>\r
146 \r
147 </body>\r
148 </html>\r