【问题标题】:wp_mail not sending when adding headers添加标题时wp_mail未发送
【发布时间】:2015-05-01 16:25:16
【问题描述】:

电子邮件已成功发送,但我想发送电子邮件以包含页脚 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

【问题讨论】:

  • WordPress 只是将这些东西传递给 PHP 的默认函数。尝试将标头的 from 部分放在 mime 和内容类型之后。
  • 试过但还是没用.. 仅当我删除 mime/content-type 时才有效
  • 查看您服务器上的邮件日志,看看是否有任何错误。

标签: wordpress


【解决方案1】:

首先,我看到了这段代码。该标题摘录没有任何问题,您不需要这一行:

$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,然后再次尝试您的代码。希望有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多