public function test_valid_subject() { $message = 'test'; $email = new BP_Email('activity-at-message'); $email->set_subject($message)->set_tokens(array('poster.name' => 'example')); $this->assertSame($message, $email->get_subject()); }
public function bp_email(BP_Email $email) { $recipients = $email->get_to(); $to = array(); foreach ($recipients as $recipient) { $to[] = $recipient->get_address(); } $subject = $email->get_subject('replace-tokens'); $message = normalize_whitespace($email->get_content_plaintext('replace-tokens')); $filter_set = false; if ('plaintext' != Sendgrid_Tools::get_content_type()) { add_filter('wp_mail_content_type', array($this, 'set_html_content_type'), 100); $filter_set = true; $message = $email->get_template('add-content'); } $result = wp_mail($to, $subject, $message); if ($filter_set) { remove_filter('wp_mail_content_type', array($this, 'set_html_content_type')); } return $result; }
/** * 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); } }
public function test_html_entities_are_decoded_in_email_recipient_names() { // Raw display name. $name = "Test o'Toole"; // Emulate rendered {poster.name} token. $token = apply_filters('bp_core_get_user_displayname', $name); $email = new BP_Email('activity-at-message'); $email->set_subject('{{poster.name}}')->set_tokens(array('poster.name' => $token)); // Subject always has to have tokens replaced before sending. $this->assertSame($name, $email->get_subject('replace-tokens')); }