【问题标题】:wp_mail line breaks in textarea for html emailhtml电子邮件的textarea中的wp_mail换行符
【发布时间】:2013-01-19 07:08:27
【问题描述】:

我在使用 wp_mail 功能时遇到问题,希望有人能帮助我。

我需要在用户添加到文本区域“消息”的电子邮件中插入换行符。

有人可以帮忙吗?

我目前有以下代码从联系表单发送电子邮件:

<?php

if( !isset($_REQUEST) ) return;

require('../../../../wp-load.php');
function wpse27856_set_content_type(){
return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

$name = $_REQUEST['name'];
$phone = $_REQUEST['phone'];
$email = $_REQUEST['email'];
$msg = $_REQUEST['message'];


$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$message = '
<html>
<body>
Someone has made an enquiry on the online contact form:

<br /><br />
        <b>Contact Details:</b><br />
        '.$name.'<br />
        '.$phone.'<br />
        '.$email.'<br /><br />
        <b>Message:</b><br />
        '.$msg.'<br />

        <br /><br />


</body>
</html>
            ';

wp_mail('email@email.co.uk', 'Contact form Message' , $message, $headers);


?>

【问题讨论】:

    标签: php html wordpress email


    【解决方案1】:

    使用此标头在电子邮件中发送 html

    $headers  = "MIME-Version: 1.0" . "\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1" . "\n";
        $headers .= "X-Priority: 1 (Higuest)\n";
        $headers .= "X-MSMail-Priority: High\n";
        $headers .= "Importance: High\n";
    
        $headers .= "From: Approprice <".$mailfrom.">" . "\n";
        $headers .= "Return-Path: Approprice <".$mailfrom.">" . "\n";
        $headers .= "Reply-To: Approprice <".$mailfrom.">";
    

    【讨论】:

      【解决方案2】:

      默认情况下,wp_mail() 以纯文本形式发送消息,因此电子邮件客户端不会解析 HTML。

      在您的电子邮件中包含以下标题:

      $headers .= "MIME-Version: 1.0\r\n";
      $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
      

      由于您的电子邮件采用 HTML 格式,因此最好使用适当的 HTML 样板(HTML、HEAD、BODY...)使其有效

      或者,您可以用回车符 (\r\n) 替换您的
      标签,尽管您仍然需要去掉标签。

      这已经包含在 mail() 的 PHP 文档中,wp_mail() 围绕该文档进行包装。

      http://php.net/manual/en/function.mail.php

      【讨论】:

      • 谢谢。为我工作。
      【解决方案3】:

      $msg = nl2br($_REQUEST['message']);

      【讨论】:

        【解决方案4】:

        如果您的 textarea 包含 html,您可以在邮件中添加标题

        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        

        【讨论】:

          猜你喜欢
          • 2011-05-01
          • 1970-01-01
          • 2021-01-05
          • 2016-05-23
          • 1970-01-01
          • 1970-01-01
          • 2016-10-18
          • 2011-03-13
          • 1970-01-01
          相关资源
          最近更新 更多