BP_PHPMailer

Email delivery implementation using PHPMailer.

Description

Source

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

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class BP_PHPMailer implements BP_Email_Delivery {
 
    /**
     * Send email(s).
     *
     * @since BuddyPress 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;
 
        /**
         * Filter PHPMailer object to use.
         *
         * Specify an alternative version of PHPMailer to use instead of WordPress' default.
         *
         * @since BuddyPress 2.8.0
         *
         * @param null|PHPMailer $phpmailer The phpmailer class.
         */
        $phpmailer = apply_filters( 'bp_phpmailer_object', $phpmailer );
 
        if ( ! ( $phpmailer instanceof PHPMailer ) ) {
            if ( ! class_exists( 'PHPMailer' ) ) {
                require_once ABSPATH . WPINC . '/class-phpmailer.php';
                require_once ABSPATH . WPINC . '/class-smtp.php';
            }
 
            $phpmailer = new PHPMailer( true );
        }
 
 
        /*
         * Resets.
         */
        $phpmailer->MessageDate = date( 'D, j M Y H:i:s O' );
        $phpmailer->clearAllRecipients();
        $phpmailer->clearAttachments();
        $phpmailer->clearCustomHeaders();
        $phpmailer->clearReplyTos();
        $phpmailer->Sender = '';
 
 
        /*
         * Set up.
         */
 
        $phpmailer->IsMail();
        $phpmailer->CharSet = bp_get_option( 'blog_charset' );
 
 
        /*
         * 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' ) );
            $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 BuddyPress 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 );
        }
    }
 
 
    /*
     * Utility/helper functions.
     */
 
    /**
     * Get an appropriate hostname for the email. Varies depending on site configuration.
     *
     * @since BuddyPress 2.5.0
     * @deprecated 2.5.3 No longer used.
     *
     * @return string
     */
    public static function get_hostname() {
        return '';
    }
}

Changelog

Changelog
Version Description
BuddyPress 2.5.0 Introduced.

Methods

  • bp_email — Send email(s).
  • get_hostname — Get an appropriate hostname for the email. Varies depending on site configuration. — deprecated

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.