extending.html 3.5 KB

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