Ответ на вопрос wp_mail— обычное требование при работе с веб-сайтами WordPress. Функция wp_mailиспользуется для отправки электронных писем из WordPress. Часто бывает необходимо настроить содержимое электронного письма или добавить дополнительные функции при ответе на электронные письма, отправленные через wp_mail. В этой статье блога я рассмотрю несколько методов с примерами кода, как ответить на wp_mailв WordPress.
Метод 1: использование крючка phpmailer_init
function custom_wp_mail_reply($phpmailer) {
// Check if the email is a reply to `wp_mail`
if (isset($phpmailer->reply_to) && !empty($phpmailer->reply_to)) {
// Get the reply-to email address
$reply_to = $phpmailer->reply_to[0];
// Retrieve the original message sent through `wp_mail`
$original_message = get_original_message(); // Implement your own logic to retrieve the original message
// Compose the reply email
$reply_subject = 'Re: ' . $original_message['subject'];
$reply_body = 'Hi there,' . "\n\n" . 'In response to your email:';
$reply_body .= "\n\n" . $original_message['body'];
// Set the reply email content
$phpmailer->ClearAllRecipients();
$phpmailer->addAddress($reply_to);
$phpmailer->Subject = $reply_subject;
$phpmailer->Body = $reply_body;
}
}
add_action('phpmailer_init', 'custom_wp_mail_reply');
Метод 2. Создание пользовательской функции ответа по электронной почте
function custom_wp_mail_reply($to, $subject, $message, $headers, $attachments) {
// Check if the email is a reply to `wp_mail`
if (isset($headers['Reply-To']) && !empty($headers['Reply-To'])) {
// Get the reply-to email address
$reply_to = $headers['Reply-To'];
// Retrieve the original message sent through `wp_mail`
$original_message = get_original_message(); // Implement your own logic to retrieve the original message
// Compose the reply email
$reply_subject = 'Re: ' . $original_message['subject'];
$reply_message = 'Hi there,' . "\n\n" . 'In response to your email:';
$reply_message .= "\n\n" . $original_message['body'];
// Set the reply email content
$reply_headers = array(
'Content-Type: text/plain; charset=UTF-8',
'From: ' . $headers['From'],
'Reply-To: ' . $headers['Reply-To']
);
// Send the reply email
wp_mail($reply_to, $reply_subject, $reply_message, $reply_headers, $attachments);
}
}
add_action('wp_mail', 'custom_wp_mail_reply', 10, 5);
Метод 3. Использование собственного плагина
Создайте собственный плагин и добавьте следующий код:
/
* Plugin Name: Custom WP Mail Reply
* Description: Handles replying to `wp_mail` emails.
*/
// Reply to `wp_mail`
function custom_wp_mail_reply($phpmailer) {
// Your code here
}
add_action('phpmailer_init', 'custom_wp_mail_reply');
Не забудьте заменить заполнители комментариев собственной логикой, чтобы получить исходное сообщение и составить ответное письмо.
в WordPress: несколько методов с примерами кода»