Просмотр файла main_files/phpmailer/docs/extending.html

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