2013-08-07 08:38:38 +02:00
|
|
|
<?php
|
2020-06-16 15:57:05 +02:00
|
|
|
require_once ABSPATH . 'wp-includes/PHPMailer/PHPMailer.php';
|
|
|
|
require_once ABSPATH . 'wp-includes/PHPMailer/Exception.php';
|
2013-08-07 08:38:38 +02:00
|
|
|
|
2020-06-12 17:45:30 +02:00
|
|
|
class MockPHPMailer extends PHPMailer\PHPMailer\PHPMailer {
|
2013-08-07 08:38:38 +02:00
|
|
|
var $mock_sent = array();
|
|
|
|
|
2015-07-08 19:15:02 +02:00
|
|
|
function preSend() {
|
|
|
|
$this->Encoding = '8bit';
|
|
|
|
return parent::preSend();
|
|
|
|
}
|
|
|
|
|
2014-03-03 21:24:31 +01:00
|
|
|
/**
|
2015-07-08 19:15:02 +02:00
|
|
|
* Override postSend() so mail isn't actually sent.
|
2014-03-03 21:24:31 +01:00
|
|
|
*/
|
2015-07-08 19:15:02 +02:00
|
|
|
function postSend() {
|
|
|
|
$this->mock_sent[] = array(
|
2016-02-20 04:40:49 +01:00
|
|
|
'to' => $this->to,
|
|
|
|
'cc' => $this->cc,
|
|
|
|
'bcc' => $this->bcc,
|
Mail: Improve handling of UTF-8 address headers.
Previously, `wp_mail()` implemented Reply-To as a generic header, using
PHPMailer's `addCustomHeader()`. As such, the email address portion of
the header was being incorrectly encoded when the name portion
contained UTF-8 characters. Switching to PHPMailer's more specific
`addReplyTo()` method fixes the issue.
For greater readability, the handling of all address-related headers
(To, CC, BCC, Reply-To) has been standardized.
Props szepe.viktor, iandunn, bpetty, stephenharris.
Fixes #21659.
git-svn-id: https://develop.svn.wordpress.org/trunk@38058 602fd350-edb4-49c9-b593-d223f7449a82
2016-07-13 20:03:52 +02:00
|
|
|
'header' => $this->MIMEHeader . $this->mailHeader,
|
2016-02-20 04:40:49 +01:00
|
|
|
'subject' => $this->Subject,
|
|
|
|
'body' => $this->MIMEBody,
|
2015-07-08 19:15:02 +02:00
|
|
|
);
|
2013-08-07 08:38:38 +02:00
|
|
|
|
2015-07-08 19:15:02 +02:00
|
|
|
return true;
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
2016-02-20 04:40:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decorator to return the information for a sent mock.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
|
|
|
* @param int $index Optional. Array index of mock_sent value.
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_sent( $index = 0 ) {
|
|
|
|
$retval = false;
|
|
|
|
if ( isset( $this->mock_sent[ $index ] ) ) {
|
|
|
|
$retval = (object) $this->mock_sent[ $index ];
|
|
|
|
}
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a recipient for a sent mock.
|
|
|
|
*
|
|
|
|
* @since 4.5.0
|
|
|
|
*
|
|
|
|
* @param string $address_type The type of address for the email such as to, cc or bcc.
|
|
|
|
* @param int $mock_sent_index Optional. The sent_mock index we want to get the recipient for.
|
|
|
|
* @param int $recipient_index Optional. The recipient index in the array.
|
|
|
|
* @return bool|object Returns object on success, or false if any of the indices don't exist.
|
|
|
|
*/
|
|
|
|
public function get_recipient( $address_type, $mock_sent_index = 0, $recipient_index = 0 ) {
|
|
|
|
$retval = false;
|
2017-12-01 00:09:33 +01:00
|
|
|
$mock = $this->get_sent( $mock_sent_index );
|
2016-02-20 04:40:49 +01:00
|
|
|
if ( $mock ) {
|
|
|
|
if ( isset( $mock->{$address_type}[ $recipient_index ] ) ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$address_index = $mock->{$address_type}[ $recipient_index ];
|
2016-02-20 04:40:49 +01:00
|
|
|
$recipient_data = array(
|
|
|
|
'address' => ( isset( $address_index[0] ) && ! empty( $address_index[0] ) ) ? $address_index[0] : 'No address set',
|
|
|
|
'name' => ( isset( $address_index[1] ) && ! empty( $address_index[1] ) ) ? $address_index[1] : 'No name set',
|
|
|
|
);
|
|
|
|
|
|
|
|
$retval = (object) $recipient_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to return the global phpmailer instance defined in the bootstrap
|
|
|
|
*
|
|
|
|
* @since 4.4.0
|
|
|
|
*
|
|
|
|
* @return object|bool
|
|
|
|
*/
|
|
|
|
function tests_retrieve_phpmailer_instance() {
|
|
|
|
$mailer = false;
|
|
|
|
if ( isset( $GLOBALS['phpmailer'] ) ) {
|
|
|
|
$mailer = $GLOBALS['phpmailer'];
|
|
|
|
}
|
|
|
|
return $mailer;
|
2013-08-07 08:38:38 +02:00
|
|
|
}
|
2016-05-04 05:48:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to reset the phpmailer instance.
|
|
|
|
*
|
|
|
|
* @since 4.6.0
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function reset_phpmailer_instance() {
|
|
|
|
$mailer = tests_retrieve_phpmailer_instance();
|
2017-07-29 22:52:45 +02:00
|
|
|
if ( $mailer ) {
|
2020-07-27 22:17:36 +02:00
|
|
|
$mailer = new MockPHPMailer( true );
|
|
|
|
$mailer::$validator = static function ( $email ) {
|
|
|
|
return (bool) is_email( $email );
|
|
|
|
};
|
|
|
|
|
|
|
|
$GLOBALS['phpmailer'] = $mailer;
|
2016-05-04 05:48:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|