/bp-core/classes/class-bp-phpmailer.php

  1. <?php 
  2. /** 
  3. * Core component classes. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage Core 
  7. */ 
  8.  
  9. // Exit if accessed directly 
  10. defined( 'ABSPATH' ) || exit; 
  11.  
  12. /** 
  13. * Email delivery implementation using PHPMailer. 
  14. * 
  15. * @since 2.5.0 
  16. */ 
  17. class BP_PHPMailer implements BP_Email_Delivery { 
  18.  
  19. /** 
  20. * Send email(s). 
  21. * 
  22. * @since 2.5.0 
  23. * 
  24. * @param BP_Email $email Email to send. 
  25. * @return bool|WP_Error Returns true if email send, else a descriptive WP_Error. 
  26. */ 
  27. public function bp_email( BP_Email $email ) { 
  28. static $phpmailer = null; 
  29.  
  30. /** 
  31. * Filter PHPMailer object to use. 
  32. * 
  33. * Specify an alternative version of PHPMailer to use instead of WordPress' default. 
  34. * 
  35. * @since 2.8.0 
  36. * 
  37. * @param null|PHPMailer $phpmailer The phpmailer class. 
  38. */ 
  39. $phpmailer = apply_filters( 'bp_phpmailer_object', $phpmailer ); 
  40.  
  41. if ( ! ( $phpmailer instanceof PHPMailer ) ) { 
  42. if ( ! class_exists( 'PHPMailer' ) ) { 
  43. require_once ABSPATH . WPINC . '/class-phpmailer.php'; 
  44. require_once ABSPATH . WPINC . '/class-smtp.php'; 
  45.  
  46. $phpmailer = new PHPMailer( true ); 
  47.  
  48.  
  49. /** 
  50. * Resets. 
  51. */ 
  52.  
  53. $phpmailer->clearAllRecipients(); 
  54. $phpmailer->clearAttachments(); 
  55. $phpmailer->clearCustomHeaders(); 
  56. $phpmailer->clearReplyTos(); 
  57. $phpmailer->Sender = ''; 
  58.  
  59.  
  60. /** 
  61. * Set up. 
  62. */ 
  63.  
  64. $phpmailer->IsMail(); 
  65. $phpmailer->CharSet = bp_get_option( 'blog_charset' ); 
  66.  
  67.  
  68. /** 
  69. * Content. 
  70. */ 
  71.  
  72. $phpmailer->Subject = $email->get_subject( 'replace-tokens' ); 
  73. $content_plaintext = PHPMailer::normalizeBreaks( $email->get_content_plaintext( 'replace-tokens' ) ); 
  74.  
  75. if ( $email->get( 'content_type' ) === 'html' ) { 
  76. $phpmailer->msgHTML( $email->get_template( 'add-content' ) ); 
  77. $phpmailer->AltBody = $content_plaintext; 
  78.  
  79. } else { 
  80. $phpmailer->IsHTML( false ); 
  81. $phpmailer->Body = $content_plaintext; 
  82.  
  83. $recipient = $email->get_from(); 
  84. try { 
  85. $phpmailer->SetFrom( $recipient->get_address(), $recipient->get_name(), false ); 
  86. } catch ( phpmailerException $e ) { 
  87.  
  88. $recipient = $email->get_reply_to(); 
  89. try { 
  90. $phpmailer->addReplyTo( $recipient->get_address(), $recipient->get_name() ); 
  91. } catch ( phpmailerException $e ) { 
  92.  
  93. $recipients = $email->get_to(); 
  94. foreach ( $recipients as $recipient ) { 
  95. try { 
  96. $phpmailer->AddAddress( $recipient->get_address(), $recipient->get_name() ); 
  97. } catch ( phpmailerException $e ) { 
  98.  
  99. $recipients = $email->get_cc(); 
  100. foreach ( $recipients as $recipient ) { 
  101. try { 
  102. $phpmailer->AddCc( $recipient->get_address(), $recipient->get_name() ); 
  103. } catch ( phpmailerException $e ) { 
  104.  
  105. $recipients = $email->get_bcc(); 
  106. foreach ( $recipients as $recipient ) { 
  107. try { 
  108. $phpmailer->AddBcc( $recipient->get_address(), $recipient->get_name() ); 
  109. } catch ( phpmailerException $e ) { 
  110.  
  111. $headers = $email->get_headers(); 
  112. foreach ( $headers as $name => $content ) { 
  113. $phpmailer->AddCustomHeader( $name, $content ); 
  114.  
  115.  
  116. /** 
  117. * Fires after PHPMailer is initialised. 
  118. * 
  119. * @since 2.5.0 
  120. * 
  121. * @param PHPMailer $phpmailer The PHPMailer instance. 
  122. */ 
  123. do_action( 'bp_phpmailer_init', $phpmailer ); 
  124.  
  125. /** This filter is documented in wp-includes/pluggable.php */ 
  126. do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) ); 
  127.  
  128. try { 
  129. return $phpmailer->Send(); 
  130. } catch ( phpmailerException $e ) { 
  131. return new WP_Error( $e->getCode(), $e->getMessage(), $email ); 
  132.  
  133.  
  134. /** 
  135. * Utility/helper functions. 
  136. */ 
  137.  
  138. /** 
  139. * Get an appropriate hostname for the email. Varies depending on site configuration. 
  140. * 
  141. * @since 2.5.0 
  142. * @deprecated 2.5.3 No longer used. 
  143. * 
  144. * @return string 
  145. */ 
  146. public static function get_hostname() { 
  147. return ''; 
.