添加标头时wp_mail不发送

时间:2015-05-01 16:25:17

标签: wordpress

电子邮件发送成功但我想发送电子邮件以包含页脚html模板,所以我添加了以下内容:

$headers .= 'MIME-Version: 1.0' . "\r\n";    
$headers .= "Content-type: text/html; charset=UTF-8 \r\n"; 

现在电子邮件尚未发送。我该如何解决这个问题?

$emails = array("test@gmail.com");
        $emails = array_unique($emails);
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        $message.="<div class='footer'>
        <ul style='padding: 0;margin: 0;'>
            <li><img style='width:300px' src='https://www.test.co.uk/wp-content/themes/test/images/structural/head-logo.jpg' /></li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Tel:</span> +44 203 051 1214</li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Fax:</span> +44 207 657 3322</li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>E-mail:</span> info@test.co.uk </li>
            <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Web:</span> www.test.co.uk</li>
        </ul>
    </div>";
                $headers = 'From: test <info@test.co.uk>' . "\r\n"; 
                $headers .= 'MIME-Version: 1.0' . "\r\n";    
                $headers .= "Content-type: text/html; charset=UTF-8 \r\n";

                $success = wp_mail( $emails, $subject, $message, $headers );  //send emails

1 个答案:

答案 0 :(得分:0)

首先,我看到了这段代码。你不需要这一行的标题摘录没有错:

$headers .= 'MIME-Version: 1.0' . "\r\n";

wp_mail会自动为你添加它。

其次,试试这段代码:

    $emails = "yourmail@gmail.com";
    //$emails .= ', ' . 'test2@gmail.com';

    $subject = !empty($_POST['subject']) ? sanitize_text_field($_POST['subject']) : 'test subject';
    $message = !empty($_POST['message']) ? $_POST['message'] : '';

    $message.="<div class='footer'>
            <ul style='padding: 0;margin: 0;'>
                <li><img style='width:300px' src='https://www.test.co.uk/wp-content/themes/test/images/structural/head-logo.jpg' /></li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Tel:</span> +44 203 051 1214</li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Fax:</span> +44 207 657 3322</li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>E-mail:</span> info@test.co.uk </li>
                <li style='list-style-type: none;margin-bottom:6px;'><span style='color: #73a724;font-weight: bold;'>Web:</span> www.test.co.uk</li>
            </ul>
        </div>";

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
    $headers .= 'From: test <info@test.co.uk>' . "\r\n";

    $success = mail($emails, $subject, $message, $headers);

我认为这里有一些混合。

如果仍然无效,也许您应该使用您的Gmail信息安装此插件here并再次尝试使用您的代码。希望有所帮助!