如何将wp_mail更改为订阅者电子邮件ID

时间:2014-12-07 17:10:23

标签: php wordpress email

我正在尝试使用插件在wordpress上设置登录页面 - 即将推出CC。它有一个订阅框。每当有人注册时事通讯时,我会收到一封来自wordpress@mydomain.com的电子邮件,并附上通知。如何以我从订阅者的邮件ID本身而不是wordpress@mydomian.com获取邮件的方式更改它。换句话说,它获取并从订阅者电子邮件ID发送邮件而不是wp_mail。 这是我在插件文件中找到的 -

/**
 * [ajax_newsletter_subscribe description]
 * @return [type] [description]
 */
public function ajax_newsletter_subscribe() {

    check_ajax_referer( 'cc-cs-newsletter-subscribe' );

    $to_email = $this->get_option('newsletter', 'email') ? $this->get_option('newsletter', 'email') : get_option( 'admin_email' );

    if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {

        $sent = wp_mail(
            $to_email,
            __('You have a new subscriber!', $this->plugin_slug),
            sprintf(__("Hello,\n\nA new user has subscribed through your Coming Soon page.\n\nSubscriber's email: %s", $this->plugin_slug), $_POST['email'])
        );

        if($sent) {
            print json_encode(array('status' => 'ok'));
            die();
        }
    }

    print json_encode(array('status' => 'error'));
    die();
}

1 个答案:

答案 0 :(得分:0)

您可以设置电子邮件$headers,其中包括From

$headers = array();
$headers[] = 'From: ' . $_POST['email'] . ' <' . $_POST['email'] . '>';

$sent = wp_mail(
    $to_email,
    __( 'You have a new subscriber!', $this->plugin_slug ),
    sprintf( __( "Hello,\n\nA new user has subscribed through your Coming Soon page.\n\nSubscriber's email: %s", $this->plugin_slug ), $_POST['email'] ),
    $headers
);

检查Codex