Skip to:
Content

BuddyPress.org

Changeset 12688


Ignore:
Timestamp:
07/22/2020 07:14:39 PM (3 years ago)
Author:
imath
Message:

Emails: ensure compatibility with PHPMailer 6.0

Since version 5.5, WordPress has deprecated version 5.2 of PHPMailer in favor of its 6.0 version. We are using this library for our BP Emails feature. To ensure compatibility with this new version of WordPress as well as with the previous versions we are supporting (4.8 to 5.4), we need to make sure to use the right version of PHPMailer according to the version of WordPress installed.

Props hareesh-pillai, man4toman

Fixes #8322 (6.0 branch)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/6.0/src/bp-core/classes/class-bp-phpmailer.php

    r12547 r12688  
    2727    public function bp_email( BP_Email $email ) {
    2828        static $phpmailer = null;
     29        $phpmailer_is_6_0 = false;
    2930
    3031        /**
     
    3940        $phpmailer = apply_filters( 'bp_phpmailer_object', $phpmailer );
    4041
    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 
    47             $phpmailer = new PHPMailer( true );
    48         }
    49 
     42        /**
     43         * WordPress 5.5 deprecated version 5.2 of PHPMailer
     44         * and is now using version 6.0 of PHPMailer.
     45         */
     46        if ( file_exists( ABSPATH . WPINC . '/PHPMailer/PHPMailer.php' ) ) {
     47            if ( ! ( $phpmailer instanceof PHPMailer\PHPMailer\PHPMailer ) ) {
     48                if ( ! class_exists( 'PHPMailer\\PHPMailer\\PHPMailer' ) ) {
     49                    require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
     50                    require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
     51                    require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
     52                }
     53
     54                $phpmailer        = new PHPMailer\PHPMailer\PHPMailer( true );
     55                $phpmailer_is_6_0 = true;
     56            }
     57        } else {
     58            if ( ! ( $phpmailer instanceof PHPMailer ) ) {
     59                if ( ! class_exists( 'PHPMailer' ) ) {
     60                    require_once ABSPATH . WPINC . '/class-phpmailer.php';
     61                    require_once ABSPATH . WPINC . '/class-smtp.php';
     62                }
     63
     64                $phpmailer = new PHPMailer( true );
     65            }
     66        }
    5067
    5168        /*
     
    5976        $phpmailer->Sender = '';
    6077
    61 
    6278        /*
    6379         * Set up.
    6480         */
    65 
    6681        $phpmailer->IsMail();
    6782        $phpmailer->CharSet = bp_get_option( 'blog_charset' );
    6883
    69 
    7084        /*
    7185         * Content.
    7286         */
    73 
    7487        $phpmailer->Subject = $email->get_subject( 'replace-tokens' );
    75         $content_plaintext  = PHPMailer::normalizeBreaks( $email->get_content_plaintext( 'replace-tokens' ) );
     88        if ( $phpmailer_is_6_0 ) {
     89            $content_plaintext = PHPMailer\PHPMailer\PHPMailer::normalizeBreaks( $email->get_content_plaintext( 'replace-tokens' ) );
     90        } else {
     91            $content_plaintext = PHPMailer::normalizeBreaks( $email->get_content_plaintext( 'replace-tokens' ) );
     92        }
    7693
    7794        if ( $email->get( 'content_type' ) === 'html' ) {
     
    85102
    86103        $recipient = $email->get_from();
    87         try {
    88             $phpmailer->SetFrom( $recipient->get_address(), $recipient->get_name(), false );
    89         } catch ( phpmailerException $e ) {
     104        if ( $phpmailer_is_6_0 ) {
     105            try {
     106                $phpmailer->setFrom( $recipient->get_address(), $recipient->get_name(), false );
     107            } catch ( PHPMailer\PHPMailer\Exception $e ) {
     108            }
     109        } else {
     110            try {
     111                $phpmailer->SetFrom( $recipient->get_address(), $recipient->get_name(), false );
     112            } catch ( phpmailerException $e ) {
     113            }
    90114        }
    91115
    92116        $recipient = $email->get_reply_to();
    93         try {
    94             $phpmailer->addReplyTo( $recipient->get_address(), $recipient->get_name() );
    95         } catch ( phpmailerException $e ) {
     117        if ( $phpmailer_is_6_0 ) {
     118            try {
     119                $phpmailer->addReplyTo( $recipient->get_address(), $recipient->get_name() );
     120            } catch ( PHPMailer\PHPMailer\Exception $e ) {
     121            }
     122        } else {
     123            try {
     124                $phpmailer->addReplyTo( $recipient->get_address(), $recipient->get_name() );
     125            } catch ( phpmailerException $e ) {
     126            }
    96127        }
    97128
    98129        $recipients = $email->get_to();
    99         foreach ( $recipients as $recipient ) {
    100             try {
    101                 $phpmailer->AddAddress( $recipient->get_address(), $recipient->get_name() );
    102             } catch ( phpmailerException $e ) {
     130        if ( $phpmailer_is_6_0 ) {
     131            foreach ( $recipients as $recipient ) {
     132                try {
     133                    $phpmailer->AddAddress( $recipient->get_address(), $recipient->get_name() );
     134                } catch ( PHPMailer\PHPMailer\Exception $e ) {
     135                }
     136            }
     137        } else {
     138            foreach ( $recipients as $recipient ) {
     139                try {
     140                    $phpmailer->AddAddress( $recipient->get_address(), $recipient->get_name() );
     141                } catch ( phpmailerException $e ) {
     142                }
    103143            }
    104144        }
    105145
    106146        $recipients = $email->get_cc();
    107         foreach ( $recipients as $recipient ) {
    108             try {
    109                 $phpmailer->AddCc( $recipient->get_address(), $recipient->get_name() );
    110             } catch ( phpmailerException $e ) {
     147        if ( $phpmailer_is_6_0 ) {
     148            foreach ( $recipients as $recipient ) {
     149                try {
     150                    $phpmailer->AddCc( $recipient->get_address(), $recipient->get_name() );
     151                } catch ( PHPMailer\PHPMailer\Exception $e ) {
     152                }
     153            }
     154        } else {
     155            foreach ( $recipients as $recipient ) {
     156                try {
     157                    $phpmailer->AddCc( $recipient->get_address(), $recipient->get_name() );
     158                } catch ( phpmailerException $e ) {
     159                }
    111160            }
    112161        }
    113162
    114163        $recipients = $email->get_bcc();
    115         foreach ( $recipients as $recipient ) {
    116             try {
    117                 $phpmailer->AddBcc( $recipient->get_address(), $recipient->get_name() );
    118             } catch ( phpmailerException $e ) {
     164        if ( $phpmailer_is_6_0 ) {
     165            foreach ( $recipients as $recipient ) {
     166                try {
     167                    $phpmailer->AddBcc( $recipient->get_address(), $recipient->get_name() );
     168                } catch ( PHPMailer\PHPMailer\Exception $e ) {
     169                }
     170            }
     171        } else {
     172            foreach ( $recipients as $recipient ) {
     173                try {
     174                    $phpmailer->AddBcc( $recipient->get_address(), $recipient->get_name() );
     175                } catch ( phpmailerException $e ) {
     176                }
    119177            }
    120178        }
     
    125183        }
    126184
    127 
    128185        /**
    129186         * Fires after PHPMailer is initialised.
     
    138195        do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
    139196
    140         try {
    141             return $phpmailer->Send();
    142         } catch ( phpmailerException $e ) {
    143             return new WP_Error( $e->getCode(), $e->getMessage(), $email );
     197        if ( $phpmailer_is_6_0 ) {
     198            try {
     199                return $phpmailer->Send();
     200            } catch ( PHPMailer\PHPMailer\Exception $e ) {
     201                return new WP_Error( $e->getCode(), $e->getMessage(), $email );
     202            }
     203        } else {
     204            try {
     205                return $phpmailer->Send();
     206            } catch ( phpmailerException $e ) {
     207                return new WP_Error( $e->getCode(), $e->getMessage(), $email );
     208            }
    144209        }
    145210    }
    146 
    147211
    148212    /*
Note: See TracChangeset for help on using the changeset viewer.