function wpses_mail($to, $subject, $message, $headers = '', $attachments = '')
{
global $SES;
global $wpses_options;
global $wp_better_emails;
// headers can be sent as array, too. convert them to string to avoid further complications.
if (is_array($headers)) {
$headers = implode("\r\n", $headers);
}
if (is_array($to)) {
$to = implode(",", $to);
}
extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers')));
wpses_log('wpses_mail ' . $to . "\t" . $headers);
wpses_check_SES();
if (isset($wp_better_emails)) {
// From wpbe plugin, not efficient nor elegant - Will do better next time.
// Could just call the php filter on a adhoc object, to be less dependant on the implementation of wpbe code.
$txt = wp_specialchars_decode($message, ENT_QUOTES);
$txt = $wp_better_emails->set_email_template($txt, 'plaintext_template');
$txt = apply_filters('wpbe_plaintext_body', $wp_better_emails->template_vars_replacement($txt));
/** HTML ******************************************************* */
$html = $wp_better_emails->esc_textlinks($message);
$html = nl2br(make_clickable($html));
$html = $wp_better_emails->set_email_template($html);
$html = apply_filters('wpbe_html_body', $wp_better_emails->template_vars_replacement($html));
} else {
$message = preg_replace('/<(http:.*)>/', '$1', $message);
$message = preg_replace('/<(https:.*)>/', '$1', $message);
// bad hack - handle httpS as well.
$html = $message;
$txt = strip_tags($html);
if (strlen($html) == strlen($txt)) {
$html = '';
// que msg text
}
// no html entity in txt.
$txt = html_entity_decode($txt, ENT_NOQUOTES, 'UTF-8');
}
// TODO: option pour que TXT si msg html, ou les deux comme ici par défaut.
$m = new SimpleEmailServiceMessage();
// To: may contain comma separated emails. If so, explode and add them all.
// what to do if more than 50 ? (SES limit)
if (preg_match('/,/im', $to)) {
$to = explode(',', $to);
foreach ($to as $toline) {
$m->addTo($toline);
}
} else {
$m->addTo($to);
}
$m->setReturnPath($wpses_options['return_path']);
$from = $wpses_options['from_name'] . ' <' . $wpses_options['from_email'] . '>';
if ('' != $wpses_options['reply_to']) {
if ('headers' == strtolower($wpses_options['reply_to'])) {
// extract replyto from headers
$rto = array();
//if (preg_match('/^Reply-To: ([a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4})\b/imsU', $headers, $rto)) {
//if (preg_match('/^Reply-To: (.*)\b/imsU', $headers, $rto)) {
if (preg_match('/^Reply-To: (.*)/im', $headers, $rto)) {
// does only support one email for now.
$m->addReplyTo($rto[1]);
}
if (preg_match('/^From: (.*)/im', $headers, $rto)) {
// Uses "From:" header - was /isU which broke things, see https://wordpress.org/support/topic/gravity-forms-18205-latest-contact-form-7-403-latest-not-working
$from = $rto[1];
}
// Handle multiple cc and bcc: from headers too ? Guess so... TODO
if ('' != $headers) {
$headers = str_replace("\r\n", "\n", $headers);
$lines = explode("\n", $headers);
foreach ($lines as $line) {
if (preg_match('/^cc: (.*)/im', $line, $cc)) {
$m->addCC($cc[1]);
}
if (preg_match('/^Bcc: (.*)/im', $line, $Bcc)) {
$m->addBCC($Bcc[1]);
}
}
}
// Test : use full headers if provided
//$m->addCustomHeader($headers);
} else {
$m->addReplyTo($wpses_options['reply_to']);
}
}
$m->setFrom($from);
$m->setSubject($subject);
if ($html == '') {
// que texte
$m->setMessageFromString($txt);
} else {
$m->setMessageFromString($txt, $html);
}
// Attachments
if ('' != $attachments) {
if (!is_array($attachments)) {
$attachments = explode("\n", $attachments);
}
// Now we got an array
foreach ($attachments as $afile) {
$m->addAttachmentFromFile(basename($afile), $afile);
}
}
set_error_handler('wpses_error_handler');
$res = $SES->sendEmail($m);
restore_error_handler();
// Call custom Hook
do_action('wpses_mailsent', $to, $subject, $message, $headers, $attachments);
if (is_array($res)) {
wpses_log('SES id=' . $res['MessageId']);
return $res['MessageId'];
} else {
return NULL;
}
}