我正在使用WordPress +一个使用wp_mail函数发送电子邮件的联系表单。我现在需要的是它会自动发送两个(或更多)带有不同主题和消息的电子邮件。
您可以收到来自wordpress的电子邮件发送,检查要发送的电子邮件是否来自您的联系表单,并最终执行您的自定义操作(发送2封电子邮件)。
您可以通过注册过滤器来执行此操作:
add_filter( 'wp_mail', 'my_wp_mail' );
function my_wp_mail($attributes)
{
//If the subject matches the subject from the contact form do the following:
//Change the subject (so that this code gets only performed once, and not EVERY time the wp_mail function is called)
//Send your custom mails by calling the wp_mail function: https://developer.wordpress.org/reference/functions/wp_mail/
//You can access the attributes by calling $attributes['subject'], $attributes['message'], $attributes['to'], ...
return $attributes;
}
因此,在您的示例中,您唯一需要编辑的是在执行自定义操作后删除密钥_wpnonce-et-pb-contact-form-submitted-0
(因此它们无法执行无限次)。
function my_wp_mail_func( $args ) {
// checks data origin (in this case contact module of Divi theme)
if ( array_key_exists('et_pb_contactform_submit_0', $_POST) ){
// prevents infinite loop
unset($_POST["et_pb_contactform_submit_0"]);
$message = $args['message'] . ' 2';
$subject = $args["subject"] . ' 2';
$to = "[email protected]";
wp_mail( $to, $subject, $message, $args['headers'], $args['attachments'] );
$message = $args['message'] . ' 3';
$subject = $args["subject"] . ' 3';
$to = "[email protected]";
wp_mail( $to, $subject, $message, $args['headers'], $args['attachments'] );
return $args;
}
return $args;
}
add_filter( 'wp_mail', 'my_wp_mail_func' );