extending.html 3.5 KB

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