2

我有以下邮件功能:

<?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  }
?>

我遇到的问题是,当使用表单并且邮件到达我的收件箱时,HTML 显示为纯文本,并且无法正确显示。所以我可以看到所有的内联 HTML 元素,
即将显示为段落的一部分而不显示新行。任何帮助都是极好的!

编辑:

Reply-To: info@###.com
Return-Path: info@###.com
MIME-Version: 1.0
Content-type: text/html; charset: utf8

We have received a message via the online form at www.###.com<br /><br /><span    style='font-weight: bold;'>Name: </span><br />Aaron Lee<br /><br /><span style='font-weight:   bold;'>Telephone: </span><br />#####<br /><br /><span style='font-weight:   bold;'>Nature of enquiry: </span><br />###<br /><br /><span style='font-weight:   bold;'>Comments: </span><br />Testttttttttttt<br /><br />
4

9 回答 9

1

尝试替换行:

$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";
于 2014-03-14T10:08:01.180 回答
0

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

从您的示例中,您似乎在标题后有两个换行符。我会尝试删除\r所以标题的最后一行只有\n

换行试试

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

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

我知道这是不正确的,但我已经看到同样的问题以这种方式解决了。

于 2014-03-25T19:22:34.207 回答
0

您可以尝试更改以下行:

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

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

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

于 2014-03-26T13:40:26.700 回答
0

我正在使用这种方法发送邮件。也许你可以看到它可以帮助的东西:

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

}
于 2014-03-26T16:41:56.653 回答
0

讯息

您传递给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";

还要确保标签中的属性值用双引号而不是单引号括起来。所以它是<a href="#">link</a>,而不是 <a href='#'>link</a>

标头

关于您正在使用的标题的一些注释(尽管我认为这不会导致您的问题):

  • 标题应该用\r\n\n不像你的MIME-Version)分隔。
  • From应该有语法From: <email>or From: "name" <email>
  • Reply-To应该有语法Reply-To: <email>or Reply-To: "name" <email>
  • Return-Path应该有语法Return-Path: <email>
  • 您应该添加一个Content-Length标题。

试试这个:

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

不要使用邮件()

mail()PHP函数有很多问题。您真的应该考虑使用 3rd 方库来发送电子邮件。

我建议你看看Swift Mailer不要使用“邮件传输”(Swift_MailTransport);)

于 2014-03-26T22:03:05.423 回答
0

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

我的工作解决方案与您的解决方案的主要区别在于属性 Content-Transfer-Encoding,文本部分为 7 位,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;
于 2014-03-29T19:16:32.047 回答
0

您还可以使用PHP_EOL(PHP end of line) 常量代替\nor \r\n

PHP_EOL (string)
此平台的正确“行尾”符号。自 PHP 4.3.10 和 PHP 5.0.2 起可用

来源:http
://www.php.net/manual/en/reserved.constants.php 另见:何时使用 PHP 常量“PHP_EOL”?


在实践中,您可以按如下方式使用它:

$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();
于 2014-03-31T10:08:05.317 回答
0

尝试使用以下函数,它发送带有多部分数据的邮件,

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;
  }
于 2014-03-31T10:13:31.013 回答
-1

换行试试

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

$headers = 'From: <urmail@mail.com>' . "\r\n";

我已将双引号更改为单引号。它对我来说很好。试试你的运气。

于 2014-03-26T12:52:52.183 回答