/**
  * Send email(s).
  *
  * @since 2.5.0
  *
  * @param BP_Email $email Email to send.
  * @return bool|WP_Error Returns true if email send, else a descriptive WP_Error.
  */
 public function bp_email(BP_Email $email)
 {
     static $phpmailer = null;
     if ($phpmailer === null) {
         if (!class_exists('PHPMailer')) {
             require_once ABSPATH . WPINC . '/class-phpmailer.php';
             require_once ABSPATH . WPINC . '/class-smtp.php';
         }
         $phpmailer = new PHPMailer(true);
     }
     /*
      * Resets.
      */
     $phpmailer->clearAllRecipients();
     $phpmailer->clearAttachments();
     $phpmailer->clearCustomHeaders();
     $phpmailer->clearReplyTos();
     $phpmailer->Sender = '';
     /*
      * Set up.
      */
     $phpmailer->IsMail();
     $phpmailer->CharSet = bp_get_option('blog_charset');
     $phpmailer->Hostname = self::get_hostname();
     /*
      * Content.
      */
     $phpmailer->Subject = $email->get_subject('replace-tokens');
     $content_plaintext = PHPMailer::normalizeBreaks($email->get_content_plaintext('replace-tokens'));
     if ($email->get('content_type') === 'html') {
         $phpmailer->msgHTML($email->get_template('add-content'), '', 'wp_strip_all_tags');
         $phpmailer->AltBody = $content_plaintext;
     } else {
         $phpmailer->IsHTML(false);
         $phpmailer->Body = $content_plaintext;
     }
     $recipient = $email->get_from();
     try {
         $phpmailer->SetFrom($recipient->get_address(), $recipient->get_name(), false);
     } catch (phpmailerException $e) {
     }
     $recipient = $email->get_reply_to();
     try {
         $phpmailer->addReplyTo($recipient->get_address(), $recipient->get_name());
     } catch (phpmailerException $e) {
     }
     $recipients = $email->get_to();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddAddress($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $recipients = $email->get_cc();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddCc($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $recipients = $email->get_bcc();
     foreach ($recipients as $recipient) {
         try {
             $phpmailer->AddBcc($recipient->get_address(), $recipient->get_name());
         } catch (phpmailerException $e) {
         }
     }
     $headers = $email->get_headers();
     foreach ($headers as $name => $content) {
         $phpmailer->AddCustomHeader($name, $content);
     }
     /**
      * Fires after PHPMailer is initialised.
      *
      * @since 2.5.0
      *
      * @param PHPMailer $phpmailer The PHPMailer instance.
      */
     do_action('bp_phpmailer_init', $phpmailer);
     /** This filter is documented in wp-includes/pluggable.php */
     do_action_ref_array('phpmailer_init', array(&$phpmailer));
     try {
         return $phpmailer->Send();
     } catch (phpmailerException $e) {
         return new WP_Error($e->getCode(), $e->getMessage(), $email);
     }
 }
 /**
  * Set various email headers for BP 2.5 emails.
  *
  * This includes the 'Reply-To' and custom 'From' headers.
  *
  * @since 1.0-RC4
  *
  * @param bool|WP_Error $retval Returns true if validation is successful, else a descriptive WP_Error.
  * @param BP_Email      $email  Current instance of the email type class.
  */
 public function set_bp_email_headers($retval, $email)
 {
     if (!$email->get_to()) {
         return $retval;
     }
     if (empty($this->listener->item_id)) {
         return $retval;
     }
     $to = $email->get_to();
     // Backpat headers to be used for checks in 'bp_rbe_querystring' filter.
     $headers = array();
     $headers['to'] = array_shift($to)->get_address();
     $reply_to = $this->get_reply_to_address($headers);
     // If no reply to, bail.
     if (empty($reply_to)) {
         return $retval;
     }
     /**
      * Should we use the poster's display name as the 'From' name?
      *
      * @since 1.0-RC4.
      *
      * @param bool $retval
      */
     $use_custom_from_header = apply_filters('bp_rbe_use_custom_from_header', true);
     // Set custom 'From' header.
     if (!empty($this->listener->user_id) && true === $use_custom_from_header) {
         // Fetch current 'From' email address.
         $from = $email->get_from()->get_address();
         // Grab the host.
         $host = substr($from, strpos($from, '@') + 1);
         // Set the custom From email address and name.
         $email->set_from("noreply@{$host}", bp_core_get_user_displayname($this->listener->user_id));
     }
     /**
      * Set our custom 'Reply-To' email header.
      *
      * Have to workaround a mailbox character limit PHPMailer bug by wiping out
      * the Reply-To header and then setting it as a custom header.
      *
      * @link https://github.com/PHPMailer/PHPMailer/issues/706
      */
     $email->set_reply_to('');
     $email->set_headers(array('Reply-To' => $reply_to));
     return $retval;
 }