BP_PHPMailer

Email delivery implementation using PHPMailer.

Defined (1)

The class is defined in the following location(s).

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