在wp_mail()中设置自定义电子邮件发件人名称和电子邮件地址

时间:2012-02-01 作者:Nitesh

我正在编写一个自定义插件,从管理员/前端部分向用户发送电子邮件。我正在使用wp\\u email()发送电子邮件,电子邮件正在正常发送。我在一个普通的WP安装上测试这个,只安装了我的插件,hostgator作为我的托管服务器。无论何时发送电子邮件,都会从[email protected]而不是来自WP admin帐户的电子邮件地址。我也尝试过设置上面提到的自定义挂钩here 和设置自定义标题,但它们都不起作用。我不知道我犯了什么错误。你能指导我解决这个问题吗。下面提供的代码

EDIT

我还试着从另一台专用的windows服务器上测试它,得到了相同的错误。

// new name
function smartsms_mail_from_name() {
    return "WebMaster";
    //$name =  get_option(\'blogname\');
    //$name = esc_attr($name);
    //return $name;
}

// new email-adress
function smartsms_mail_from() {
    return "[email protected]";
    //$email = get_option(\'admin_email\');
    //$email = is_email($email);
    //return $email;
}
add_filter( \'wp_mail_from\', \'smartsms_mail_from\' ); 
add_filter( \'wp_mail_from_name\', \'smartsms_mail_from_name\' );

//$headers = \'From: \'. get_option(\'blogname\') .\' <\' . get_option(\'admin_email\') . \'>\';
            $headers = \'From: [email protected]\' . "\\r\\n" .\'Reply-To: [email protected]\' . "\\r\\n" . \'X-Mailer: PHP/\' . phpversion(); 
            $mail = wp_mail($to, $subject, $message, $headers);
在Brady给出以下答案后,编辑代码如下。。但没有发送电子邮件。但是,我收到的消息是“消息发送成功”:(

if($to!="")
        {
            //$headers = \'From: \'. get_option(\'blogname\') .\' <\' . get_option(\'admin_email\') . \'>\';
            //$headers = \'From: [email protected]\' . "rn" .\'Reply-To: [email protected]\' . "rn" . \'X-Mailer: PHP/\' . phpversion(); 
            add_filter(\'wp_mail_from\', \'smartsms_mail_from\'); // add filter to modify the mail from
            add_filter(\'wp_mail_from_name\', \'smartsms_mail_from_name\'); // add filter to modify the mail from name
            add_filter(\'wp_mail_content_type\', \'smartsms_wp_mail_content_type\'); // add filter to modify the mail content type
            $mail = wp_mail($to, $subject, $message); // send mail
            remove_filter(\'wp_mail_from\', \'smartsms_mail_from\'); // remove applied filter
            remove_filter(\'wp_mail_from_name\', \'smartsms_mail_from_name\'); // remove applied filter
            remove_filter(\'wp_mail_content_type\', \'smartsms_wp_mail_content_type\'); // remove applied filter
            //$mail = wp_mail($to, $subject, $message, $headers);           
            if($mail)
            {
                echo \'Your message has been sent!\';             
            }
            else echo \'There was a problem sending your message. Please try again.\';
        }

// new name
function smartsms_mail_from_name() {
    return "WebMaster";
    //$name =  get_option(\'blogname\');
    //$name = esc_attr($name);
    //return $name;
}

// new email-adress
function smartsms_mail_from() {
    return "[email protected]";
    //$email = get_option(\'admin_email\');
    //$email = is_email($email);
    //return $email;
}

function smartsms_wp_mail_content_type() { return "text/html"; }

1 个回复
最合适的回答,由SO网友:Scott 整理而成

正确的方法是对wp\\u mail\\u from和wp\\u mail\\u from\\u name应用筛选器。你可能会认为使用$headers可以正常工作,但有许多插件会过滤,然后在他们发送电子邮件时不关闭过滤器,然后在电子邮件上留下这些详细信息供下次发送。下面是我使用这些过滤器的插件的一个片段。根据您的喜好调整代码。记下我是如何在通过发送邮件后删除过滤器的wp_mail():

    public function send_notify_email($alertMessage) {
    $options = get_option(self::$settings_option_field); // Get settings
    $subject = sprintf(__("WordPress File Monitor Plus: Alert (%s)", "wordpress-file-monitor-plus"), site_url()); // build subject
    $subject = apply_filters("sc_wpfmp_format_email_subject", $subject); // allow filter to alter subject
        add_filter(\'wp_mail_from\', array(__CLASS__, \'sc_wpfmp_wp_mail_from\')); // add filter to modify the mail from
        add_filter(\'wp_mail_from_name\', array(__CLASS__, \'sc_wpfmp_wp_mail_from_name\')); // add filter to modify the mail from name
        add_filter(\'wp_mail_content_type\', array(__CLASS__, \'sc_wpfmp_wp_mail_content_type\')); // add filter to modify the mail content type
        wp_mail($options[\'notify_address\'], $subject, $alertMessage); // send mail
        remove_filter(\'wp_mail_from\', array(__CLASS__, \'sc_wpfmp_wp_mail_from\')); // remove applied filter
        remove_filter(\'wp_mail_from_name\', array(__CLASS__, \'sc_wpfmp_wp_mail_from_name\')); // remove applied filter
        remove_filter(\'wp_mail_content_type\', array(__CLASS__, \'sc_wpfmp_wp_mail_content_type\')); // remove applied filter
}


/**
 * Set from address for email notification
 *
 * @return void
 */
    public function sc_wpfmp_wp_mail_from() {
    $options = get_option(self::$settings_option_field); // Get settings
    return $options[\'from_address\']; // Return the from address
}


/**
 * Set from name for email notification
 *
 * @return string $from_name
 */
    public function sc_wpfmp_wp_mail_from_name() {
    $from_name = __("WordPress File Monitor Plus", "wordpress-file-monitor-plus");
    $from_name = apply_filters("sc_wpfmp_format_email_from_name", $from_name); // allow filter to alter the from name
    return $from_name; // return from name
}


/**
 * Set content type for email notification
 *
 * @return string
 */
    public function sc_wpfmp_wp_mail_content_type() { return "text/html"; }

结束