[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-phpmailer.php (source)

   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          $phpmailer_is_6_0 = false;
  30  
  31          /**
  32           * Filter PHPMailer object to use.
  33           *
  34           * Specify an alternative version of PHPMailer to use instead of WordPress' default.
  35           *
  36           * @since 2.8.0
  37           *
  38           * @param null|PHPMailer $phpmailer The phpmailer class.
  39           */
  40          $phpmailer = apply_filters( 'bp_phpmailer_object', $phpmailer );
  41  
  42          /**
  43           * WordPress 5.5 deprecated version 5.2 of PHPMailer
  44           * and is now using version 6.0 of PHPMailer.
  45           */
  46          if ( bp_get_major_wp_version() >= 5.5 ) {
  47              $phpmailer_is_6_0 = true;
  48  
  49              if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {
  50                  if ( ! class_exists( 'PHPMailer\\PHPMailer\\PHPMailer' ) ) {
  51                      require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
  52                      require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
  53                      require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
  54                  }
  55  
  56                  $phpmailer = new PHPMailer\PHPMailer\PHPMailer( true );
  57              }
  58          } else {
  59              if ( ! ( $phpmailer instanceof PHPMailer ) ) {
  60                  if ( ! class_exists( 'PHPMailer' ) ) {
  61                      require_once ABSPATH . WPINC . '/class-phpmailer.php';
  62                      require_once ABSPATH . WPINC . '/class-smtp.php';
  63                  }
  64  
  65                  $phpmailer = new PHPMailer( true );
  66              }
  67          }
  68  
  69          /*
  70           * Resets.
  71           */
  72          $phpmailer->MessageDate = date( 'D, j M Y H:i:s O' );
  73          $phpmailer->clearAllRecipients();
  74          $phpmailer->clearAttachments();
  75          $phpmailer->clearCustomHeaders();
  76          $phpmailer->clearReplyTos();
  77          $phpmailer->Sender = '';
  78  
  79          /*
  80           * Set up.
  81           */
  82          $phpmailer->IsMail();
  83          $phpmailer->CharSet = bp_get_option( 'blog_charset' );
  84  
  85          /*
  86           * Content.
  87           */
  88          $phpmailer->Subject = $email->get_subject( 'replace-tokens' );
  89          if ( $phpmailer_is_6_0 ) {
  90              $content_plaintext = PHPMailer\PHPMailer\PHPMailer::normalizeBreaks( $email->get_content_plaintext( 'replace-tokens' ) );
  91          } else {
  92              $content_plaintext = PHPMailer::normalizeBreaks( $email->get_content_plaintext( 'replace-tokens' ) );
  93          }
  94  
  95          if ( $email->get( 'content_type' ) === 'html' ) {
  96              $phpmailer->msgHTML( $email->get_template( 'add-content' ) );
  97              $phpmailer->AltBody = $content_plaintext;
  98  
  99          } else {
 100              $phpmailer->IsHTML( false );
 101              $phpmailer->Body = $content_plaintext;
 102          }
 103  
 104          $recipient = $email->get_from();
 105          if ( $phpmailer_is_6_0 ) {
 106              try {
 107                  $phpmailer->setFrom( $recipient->get_address(), $recipient->get_name(), false );
 108              } catch ( PHPMailer\PHPMailer\Exception $e ) {
 109              }
 110          } else {
 111              try {
 112                  $phpmailer->SetFrom( $recipient->get_address(), $recipient->get_name(), false );
 113              } catch ( phpmailerException $e ) {
 114              }
 115          }
 116  
 117          $recipient = $email->get_reply_to();
 118          if ( $phpmailer_is_6_0 ) {
 119              try {
 120                  $phpmailer->addReplyTo( $recipient->get_address(), $recipient->get_name() );
 121              } catch ( PHPMailer\PHPMailer\Exception $e ) {
 122              }
 123          } else {
 124              try {
 125                  $phpmailer->addReplyTo( $recipient->get_address(), $recipient->get_name() );
 126              } catch ( phpmailerException $e ) {
 127              }
 128          }
 129  
 130          $recipients = $email->get_to();
 131          if ( $phpmailer_is_6_0 ) {
 132              foreach ( $recipients as $recipient ) {
 133                  try {
 134                      $phpmailer->AddAddress( $recipient->get_address(), $recipient->get_name() );
 135                  } catch ( PHPMailer\PHPMailer\Exception $e ) {
 136                  }
 137              }
 138          } else {
 139              foreach ( $recipients as $recipient ) {
 140                  try {
 141                      $phpmailer->AddAddress( $recipient->get_address(), $recipient->get_name() );
 142                  } catch ( phpmailerException $e ) {
 143                  }
 144              }
 145          }
 146  
 147          $recipients = $email->get_cc();
 148          if ( $phpmailer_is_6_0 ) {
 149              foreach ( $recipients as $recipient ) {
 150                  try {
 151                      $phpmailer->AddCc( $recipient->get_address(), $recipient->get_name() );
 152                  } catch ( PHPMailer\PHPMailer\Exception $e ) {
 153                  }
 154              }
 155          } else {
 156              foreach ( $recipients as $recipient ) {
 157                  try {
 158                      $phpmailer->AddCc( $recipient->get_address(), $recipient->get_name() );
 159                  } catch ( phpmailerException $e ) {
 160                  }
 161              }
 162          }
 163  
 164          $recipients = $email->get_bcc();
 165          if ( $phpmailer_is_6_0 ) {
 166              foreach ( $recipients as $recipient ) {
 167                  try {
 168                      $phpmailer->AddBcc( $recipient->get_address(), $recipient->get_name() );
 169                  } catch ( PHPMailer\PHPMailer\Exception $e ) {
 170                  }
 171              }
 172          } else {
 173              foreach ( $recipients as $recipient ) {
 174                  try {
 175                      $phpmailer->AddBcc( $recipient->get_address(), $recipient->get_name() );
 176                  } catch ( phpmailerException $e ) {
 177                  }
 178              }
 179          }
 180  
 181          $headers = $email->get_headers();
 182          foreach ( $headers as $name => $content ) {
 183              $phpmailer->AddCustomHeader( $name, $content );
 184          }
 185  
 186          /**
 187           * Fires after PHPMailer is initialised.
 188           *
 189           * @since 2.5.0
 190           *
 191           * @param PHPMailer $phpmailer The PHPMailer instance.
 192           */
 193          do_action( 'bp_phpmailer_init', $phpmailer );
 194  
 195          /** This filter is documented in wp-includes/pluggable.php */
 196          do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
 197  
 198          if ( $phpmailer_is_6_0 ) {
 199              try {
 200                  return $phpmailer->Send();
 201              } catch ( PHPMailer\PHPMailer\Exception $e ) {
 202                  return new WP_Error( $e->getCode(), $e->getMessage(), $email );
 203              }
 204          } else {
 205              try {
 206                  return $phpmailer->Send();
 207              } catch ( phpmailerException $e ) {
 208                  return new WP_Error( $e->getCode(), $e->getMessage(), $email );
 209              }
 210          }
 211      }
 212  
 213      /*
 214       * Utility/helper functions.
 215       */
 216  
 217      /**
 218       * Get an appropriate hostname for the email. Varies depending on site configuration.
 219       *
 220       * @since 2.5.0
 221       * @deprecated 2.5.3 No longer used.
 222       *
 223       * @return string
 224       */
 225  	public static function get_hostname() {
 226          return '';
 227      }
 228  }


Generated: Wed Apr 24 01:01:03 2024 Cross-referenced by PHPXref 0.7.1