Php 将HTML显示为文本的邮件函数

Php 将HTML显示为文本的邮件函数,php,html-email,mail-form,Php,Html Email,Mail Form,我有以下邮件功能: <?php { $to = "alee@####.com"; $subject = "General Contact."; $headers = "From: ####<noreply@####.com>\r\n"; $headers .= "Reply-To: info@####.com\r\n"; $headers .= "Return-Path: info@####.com\r\n"; $headers .= 'MIME-Version: 1.0'

我有以下邮件功能:

<?php
{
$to = "alee@####.com";
$subject = "General Contact.";
$headers  = "From: ####<noreply@####.com>\r\n";
$headers .= "Reply-To: info@####.com\r\n";
$headers .= "Return-Path: info@####.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$name = filter_input(INPUT_POST, 'name');
$telephone = filter_input(INPUT_POST, 'phone');
$nature = filter_input(INPUT_POST, 'nature');   
$comments = filter_input(INPUT_POST, 'comments');
$message = 
"<span style='font-weight: bold;'>Name: </span>"."<br />".$name."<br />"."<br />".
"<span style='font-weight: bold;'>Telephone: </span>"."<br />".$telephone."<br />"."<br   />".
"<span style='font-weight: bold;'>Nature of enquiry: </span>"."<br />".$nature."<br />"."<br />".
"<span style='font-weight: bold;'>Comments: </span>"."<br />".$comments."<br />"."<br />";

mail($to, $subject,"We have received a message via the online form at www.####.com<br />  <br />" . $message, $headers);
echo "Your message was successfully sent. Please <a href='contact.php'>click here</a> to return to the site.";
?>
<?php  }
?>

尝试替换以下行:

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

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

看起来它正在发送带有额外换行符的标题

从您的示例来看,标题后面似乎有两个换行符。我将尝试删除
\r
,以便标题的最后一行只有
\n

试着换线

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

我知道这是不合适的,但我已经看到同样的问题用这种方法解决了。

试着换线

$headers  = "From: ####<noreply@####.com>\r\n";
$headers=“From:#####\r\n”;

$headers='From:'。“\r\n”;

我把双引号改成了单引号,对我来说很好。试试运气。

您可以尝试更改以下行:

$headers .= 'MIME-Version: 1.0' . "\n";


所有行都应该以\r\n结尾,而不是其他任何行。

我正在使用此方法发送邮件。也许你能看到一些它能帮助你的东西:

public static function mailSend($mail_to,$fname,$mail_sub,$mail_msg,$from){

    $To=$mail_to;

    $Body =$mail_msg."<BR>";

    $Subject=$mail_sub;

    $headers  = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $headers .= "From: Robot <info@#####.com>  \n";
    $headers .= "X-Mailer: www.#####.com\n"; //mailer
    $headers .= "Return-Path: <info@#####.com>\n";
    $headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal

    $mail = new PHPMailer();

    $mail->IsSMTP();            // set mailer to use SMTP
    $mail->IsHTML(true);

    $mail->From = $from;
    $mail->FromName = $fname;
    $mail->AddAddress($To);

    $mail->WordWrap = 100;      // set word wrap to 100 characters

    $mail->Subject = $Subject;
    $mail->Body    = $Body;
    $mail->AltBody    = $Body;

    $mail->Send();

}
公共静态函数mailSend($mail\u to,$fname,$mail\u sub,$mail\u msg,$from){
$To=$mail\U To;
$Body=$mail_msg.“
”; $Subject=$mail\u sub; $headers=“MIME版本:1.0\n”; $headers.=“内容类型:text/html;字符集=iso-8859-1\n”; $headers.=“发件人:机器人\n”; $headers.=“X-Mailer:www $headers.=“返回路径:\n”; $headers.=“X优先级:3\n”;//1个UrgentMessage,3个Normal $mail=new PHPMailer(); $mail->IsSMTP();//设置邮件程序以使用SMTP $mail->IsHTML(true); $mail->From=$From; $mail->FromName=$fname; $mail->AddAddress($To); $mail->WordWrap=100;//将word wrap设置为100个字符 $mail->Subject=$Subject; $mail->Body=$Body; $mail->AltBody=$Body; $mail->Send(); }
消息

您传递给
mail()
函数的消息不是有效的HTML。这可能是您的客户没有正确显示的原因

试试这个:

$message =
    "<!DOCTYPE html>\r\n"
    . "<html lang="en">\r\n"
    . "<body>\r\n"
    . "We have received a message via the online form at my.domain.com<br><br>\r\n"
    . "<strong>Name:</strong><br>{$name}<br><br>\r\n"
    . "<strong>Telephone:</strong><br>{$telephone}<br><br>\r\n"
    . "<strong>Nature of enquiry:</strong><br>{$nature}<br><br>\r\n"
    . "<strong>Comments:</strong><br>{$comments}<br><br>\r\n"
    . "</body>\r\n"
    . "</html>\r\n";
$headers = implode(
    "\r\n",
    array(
        'From: "My Name" <noreply@my.domain.com>',
        'Reply-To: <info@my.domain.com>',
        'Return-Path: <info@my.domain.com>',
        'MIME-Version: 1.0',
        'Content-type: text/html; charset=iso-8859-1',
        'Content-Length: ' . strlen($message)
    )
);
不要使用邮件()

PHP
mail()
函数有很多问题。你应该考虑使用第三方库来发送电子邮件。
我建议你看一下,不要使用“邮件传输”(
Swift\u-MailTransport
);)

在发送HTML格式的邮件时,我总是使用混合邮件,因为有些人禁用了HTML。 构建邮件,多部分/混合,并添加一些行以划分部分($trenner)

我的工作解决方案与您的解决方案的主要区别在于属性 内容传输编码,对于文本部分为7bit,对于html为可打印引用

$crlf = "\r\n";
$trenner = '---wbs'.md5(uniqid(time()));

if(!$this->from)throw new exception('wbs_mail:Kein Absender angegeben');
if(!$this->to)throw new exception('wbs_mail:Kein Empfänger angegeben');
if(!$this->subject)throw new exception('wbs_mail:Kein Betreff angegeben');
if(!$this->content['html'])throw new exception('wbs_mail:Kein HTML-Inhalt angegeben');
if(!$this->content['text'])throw new exception('wbs_mail:Kein Text-Inhalt angegeben');

$this->header = "From: ".$this->from.$crlf;
$this->header  .= "X-Mailer: wbs_mail php_ver". phpversion().$crlf;
$this->header  .= "MIME-Version: 1.0".$crlf;
$this->header .= 'Content-Type: multipart/mixed; boundary="'.$trenner.'"'.$crlf;

// Der Textblock
$content = $trenner.$crlf;
$content .= 'Content-Type: text/plain; charset="iso-8859-1"'.$crlf;
$content .= 'Content-Transfer-Encoding: 7bit'.$crlf;
$content .= $this->getContent('text').$crlf.$crlf;

// Der HTML Block
$content .= $trenner.$crlf.$crlf;
$content_html = 'Content-Type: text/html; charset="iso-8859-1"'.$crlf;
$content_html .= 'Content-Transfer-Encoding: quoted-printable'.$crlf;
$content_html .= $this->getContent('html').$crlf;
$content .= $content_html.$crlf;
$content .= $trenner.$crlf;

您还可以使用
PHP\u EOL
(PHP行结束)常量,而不是
\n
\r\n

PHP_下线(字符串)
此平台的正确“线端”符号。从PHP4.3.10和PHP5.0.2开始提供

来源:
另见此:


实际上,您可以按如下方式使用它:

$br = PHP_EOL;

$headers = 'From: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Reply-To: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Subject: Hello'.$br;
$headers .= 'MIME-Version: 1.0'.$br;
$headers .= 'X-Mailer: PHP'.phpversion();
$br=PHP\u EOL;
$headers='From:Avatar.$br;
$headers.=“回复:阿凡达”。$br;
$headers.='Subject:Hello'.$br;
$headers.=“MIME版本:1.0”。$br;
$headers.='X-Mailer:PHP'.phpversion();

尝试使用以下功能,它发送包含多部分数据的邮件

function sendEmail($to,$from,$sub,$msg){

    $subject = $sub; 
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .  $msg . "\n\n"; 
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $from;

    $ok = @mail($to, $subject, $message, $headers, $returnpath); 
    return $ok;
  }

没有joy,outlook和我的webmail程序仍然显示内联HTML元素。哎呀,我修复了我答案中的一个输入错误。不确定它是否有效,但你能重试吗?我错过了。太好了,哈哈。恐怕它还是做不到。上面的编辑显示了我在电子邮件中得到的信息。嗯。。斯坦奇。也许尝试为邮件添加正确的HTML结构(这里是您的HTML消息),但确实不确定:/@Diamondo25。SMTP并不是很奇怪。很好。据我所知,RFC中的“”与“\r\n”相同。回车符后跟换行符。。。它可以追溯到打字机。你使用什么样的电子邮件客户端?我已经在Outlook Express中运行了您的代码并打开了电子邮件-这绝对可以。我认为OP用“#####”来代替私人约会。你不觉得吗?如果你忽略了#######,那么你的字符串和他的没有什么不同。
$headers = implode(
    "\r\n",
    array(
        'From: "My Name" <noreply@my.domain.com>',
        'Reply-To: <info@my.domain.com>',
        'Return-Path: <info@my.domain.com>',
        'MIME-Version: 1.0',
        'Content-type: text/html; charset=iso-8859-1',
        'Content-Length: ' . strlen($message)
    )
);
$crlf = "\r\n";
$trenner = '---wbs'.md5(uniqid(time()));

if(!$this->from)throw new exception('wbs_mail:Kein Absender angegeben');
if(!$this->to)throw new exception('wbs_mail:Kein Empfänger angegeben');
if(!$this->subject)throw new exception('wbs_mail:Kein Betreff angegeben');
if(!$this->content['html'])throw new exception('wbs_mail:Kein HTML-Inhalt angegeben');
if(!$this->content['text'])throw new exception('wbs_mail:Kein Text-Inhalt angegeben');

$this->header = "From: ".$this->from.$crlf;
$this->header  .= "X-Mailer: wbs_mail php_ver". phpversion().$crlf;
$this->header  .= "MIME-Version: 1.0".$crlf;
$this->header .= 'Content-Type: multipart/mixed; boundary="'.$trenner.'"'.$crlf;

// Der Textblock
$content = $trenner.$crlf;
$content .= 'Content-Type: text/plain; charset="iso-8859-1"'.$crlf;
$content .= 'Content-Transfer-Encoding: 7bit'.$crlf;
$content .= $this->getContent('text').$crlf.$crlf;

// Der HTML Block
$content .= $trenner.$crlf.$crlf;
$content_html = 'Content-Type: text/html; charset="iso-8859-1"'.$crlf;
$content_html .= 'Content-Transfer-Encoding: quoted-printable'.$crlf;
$content_html .= $this->getContent('html').$crlf;
$content .= $content_html.$crlf;
$content .= $trenner.$crlf;
$br = PHP_EOL;

$headers = 'From: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Reply-To: Avatar <avatarparto@gmail.com>.'.$br;
$headers .= 'Subject: Hello'.$br;
$headers .= 'MIME-Version: 1.0'.$br;
$headers .= 'X-Mailer: PHP'.phpversion();
function sendEmail($to,$from,$sub,$msg){

    $subject = $sub; 
    $headers = "From: $from";

    // boundary 
    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    // headers for attachment 
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 

    // multipart boundary 
    $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .  $msg . "\n\n"; 
    $message .= "--{$mime_boundary}--";
    $returnpath = "-f" . $from;

    $ok = @mail($to, $subject, $message, $headers, $returnpath); 
    return $ok;
  }