使用WordPress wp_mail发送包含不同主题和消息的多封邮件

问题描述 投票:0回答:2

我正在使用WordPress +一个使用wp_mail函数发送电子邮件的联系表单。我现在需要的是它会自动发送两个(或更多)带有不同主题和消息的电子邮件。

  1. 对访客 - 根据他的意见单独调整
  2. 到nozbe.com将邮件转换为任务(主题中的hastags)
  3. 也许未来会更多
php wordpress
2个回答
1
投票

您可以收到来自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(因此它们无法执行无限次)。


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' );
© www.soinside.com 2019 - 2024. All rights reserved.