php 如何格式化电子邮件的消息正文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5646236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:05:09  来源:igfitidea点击:

how to format message body of email?

php

提问by Sheery

i have developed a simple php contact form, that's working fine, but i am unable to format the body of the message as per my requirement, my code is given below, i am getting mail in a single line format,

我开发了一个简单的 php 联系表单,它工作正常,但我无法按照我的要求格式化消息正文,我的代码如下,我正在以单行格式接收邮件,

where i want every information on a new line like this
"Name: Syed Sheeraz Ahmed
Contact No: 03453594552
Email: [email protected]
Address: R-47, Sector 9, North city.
Requirement: hello how are you"

我想要这样的新行的所有信息
“姓名:Syed Sheeraz Ahmed
联系电话:03453594552
电子邮件:[email protected]
地址:R-47, Sector 9, North city。
要求:你好,你好吗”

<?php
$to = "[email protected]";
$subject = "From Website Contact Form";
$name = $_REQUEST['name'] ;
$contact = $_REQUEST['contact'] ;
$address = $_REQUEST['address'] ;
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br>"; 
$MESSAGE_BODY .= "Contact No: ".$_POST["contact"]."<br>"; 
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>"; 
$MESSAGE_BODY .= "Address: ".$_POST["address"]."<br>"; 
$MESSAGE_BODY .= "Requirement: ".nl2br($_POST["message"])."<br>"; 
$message = $_REQUEST['message' + 'address' + 'contact'] ;
$from = $_REQUEST['email'] ;
$headers = "From:" . $from;
mail($to,$subject,$MESSAGE_BODY,$headers);
echo "Mail Sent.";
?> 

回答by Quentin

You want a new line (\n) not an HTML line break (<br>) since your email isn't marked as having an HTML body (and emails that have HTML bodies should really have multipart MIME bodies with both plain text and HTML versions since "HTML only" is a nice flag for spam detectors).

您需要一个新行 ( \n) 而不是 HTML 换行符 ( <br>),因为您的电子邮件没有被标记为具有 HTML 正文(并且具有 HTML 正文的电子邮件实际上应该具有包含纯文本和 HTML 版本的多部分 MIME 正文,因为“仅 HTML " 是垃圾邮件检测器的一个很好的标志)。

回答by Shakti Singh

As you are using html tag in your email content.

当您在电子邮件内容中使用 html 标签时。

Set the content- typetext/htmlin the header of your mail

content- typetext/html在邮件标题中设置

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

OR

或者

you can save yourself from this type of problems using phpmailer

您可以使用phpmailer避免此类问题

回答by MRW

If your email is sending as text then the
tags will do nothing. Try using a new line character for example:

如果您的电子邮件以文本形式发送,那么
标签将无所作为。尝试使用换行符,例如:

$MESSAGE_BODY = "Name: ".$_POST["name"]."\n";

$MESSAGE_BODY = "姓名:".$_POST["姓名"]."\n";

That should sort your problem.

那应该可以解决您的问题。

in above example <br>is printed after the variable printed, why? how to remove <br>for example:

上面的例子<br>是在变量打印后打印的,为什么?如何删除<br>例如:

$_Post["name"]has value "jhon".
it prints: jhon<br>

$_Post["name"]具有值“jhon”。
它打印:jhon<br>