extending.html 3.8 KB

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