在textarea中,wp_mail断行用于html电子邮件。

时间:2022-06-25 15:05:42

I am having an issue using the wp_mail function, and hoping someone can help me.

我在使用wp_mail函数时遇到了一个问题,希望有人能帮助我。

I need to insert line breaks in the email that user added to the text area 'message.'

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

Can anyone please help?

谁能请帮助?

I currently have the follow code sending an email from a contact form:

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

<?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);


?>

4 个解决方案

#1


4  

By default, wp_mail() sends messages as plain text, so the HTML is not parsed by email clients.

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

Include the following headers with your email:

在你的邮件中包含以下标题:

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

Since your email is in HTML, it's always good practice to make it valid with the proper HTML boilerplate (HTML, HEAD, BODY...)

由于您的电子邮件是HTML格式的,所以使用适当的HTML样板文件(HTML、HEAD、BODY…)使其有效总是很好的做法。

Alternatively, you can replace your
tags by carriage returns (\r\n), although you'll still need to get rid of tags.

或者,您也可以用回车(\r\n)替换您的标记,尽管您仍然需要删除标记。

This is already covered by the PHP documentation for mail(), around which wp_mail() wraps.

mail()的PHP文档已经介绍了这一点,wp_mail()围绕这个文档进行包装。

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

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

#2


3  

You can add header to your mail if your textarea contains html

如果你的textarea包含html,你可以向你的邮件添加标题

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

#3


1  

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

味精= nl2br美元($ _REQUEST['信息']);

#4


1  

use this header for send html in email

在电子邮件中使用此标题发送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.">";

#1


4  

By default, wp_mail() sends messages as plain text, so the HTML is not parsed by email clients.

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

Include the following headers with your email:

在你的邮件中包含以下标题:

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

Since your email is in HTML, it's always good practice to make it valid with the proper HTML boilerplate (HTML, HEAD, BODY...)

由于您的电子邮件是HTML格式的,所以使用适当的HTML样板文件(HTML、HEAD、BODY…)使其有效总是很好的做法。

Alternatively, you can replace your
tags by carriage returns (\r\n), although you'll still need to get rid of tags.

或者,您也可以用回车(\r\n)替换您的标记,尽管您仍然需要删除标记。

This is already covered by the PHP documentation for mail(), around which wp_mail() wraps.

mail()的PHP文档已经介绍了这一点,wp_mail()围绕这个文档进行包装。

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

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

#2


3  

You can add header to your mail if your textarea contains html

如果你的textarea包含html,你可以向你的邮件添加标题

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

#3


1  

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

味精= nl2br美元($ _REQUEST['信息']);

#4


1  

use this header for send html in email

在电子邮件中使用此标题发送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.">";