简体   繁体   English

如何格式化电子邮件正文通过PHP脚本发送文本

[英]how to format email body for the text send through php script

Hello I have one contact us page, in which i have one textarea for the description, there user will type his/her message. 您好我有一个联系我们页面,其中我有一个textarea描述,用户将键入他/她的消息。 I want that message to be send in mail body as it is with all the formatting in it. 我希望该邮件在邮件正文中发送,因为它包含所有格式。 I am getting description from php script 我从php脚本获取描述

$header = "Content-type: text/html\n";
$Description = $_REQUEST["txtDescription"];
mail($To, $Subject, $Description, $header);

I have defined $To, $Subject, $header. 我已经定义了$ To,$ Subject,$ header。 Mail is going perfectly. 邮件很完美。 I want message to be displayed in mail body, as user typed in textarea with formatting (like enter) 我希望消息显示在邮件正文中,因为用户在textarea中键入了格式(如输入)

You can convert your newline characters \\n to <br/> using nl2br() and then send your email in actual HTML format. 您可以将您的换行符\\n<br/>使用nl2br()然后在实际的HTML格式发送电子邮件。

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

Or better yet, use SwiftMailer or PHPMailer. 或者更好的是,使用SwiftMailer或PHPMailer。 They're a lot more better than mail() function and I recommend using them for send emails from your PHP script. 它们比mail()函数更好,我建议使用它们从PHP脚本发送电子邮件。

Use nl2br() to convert new line characters to <br> tag. 使用nl2br()将新行字符转换为<br>标记。

$Description = nl2br($_REQUEST["txtDescription"]);

Additionally you can also do the following to preserve multiple spaces - 此外,您还可以执行以下操作以保留多个空格 -

$Description = str_replace('  ', ' &nbsp;', $Description);

For this purpose you should replace textarea with CKeditor 为此,您应该用CKeditor替换textarea

Download from here http://ckeditor.com/download 从这里下载http://ckeditor.com/download

Installation and configuring details on this link 在此链接上安装和配置详细信息

Ck Editor Ck编辑

If you want to keep formatting, just use plain/text instead of text/html 如果要保持格式化,只需使用plain/text而不是text/html

Or wrap the message inside pre tags: 或者将消息包装在pre标记内:

mail($To, $Subject, "<pre>{$Description}</pre>", $header)

With content type set on text/html the client will try to render it like a browser would do. text/html上设置内容类型时,客户端将尝试像浏览器那样呈现它。 So same rules as in html will be applied : multiple whitespaces and carriage returns will be treated as one. 因此将应用与html中相同的规则:多个空格和回车将被视为一个。 So you need to use tags that imply (by default) to keep formatting, such as pre tags. 因此,您需要使用暗示(默认情况下)保持格式化的标记,例如pre标记。

If you okey with using TCPDF then use TCPDF 如果你使用TCPDF,那么使用TCPDF

Try this example 试试这个例子

http://www.tcpdf.org/examples/example_001.phps http://www.tcpdf.org/examples/example_001.phps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM