0

我正在学习 php 并尝试将邮件发送到我的电子邮件。这是我的代码:

  $to = "name@domain.com";
  $subject = "Competition Entry - WIN A 2 NIGHT FAMILY PRICE";
  $message = "Name: ".$name. "<br />" ."Surname: ".$surname.;
  mail ($to, $subject, $message);
  ?>

当它发送到我的电子邮件时,它显示为:

Name:  <br /> Surname:

为什么我的<br />显示为文本而不是中断?

4

4 回答 4

3

附加标头Content-Type

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

使用 HTML 格式发送邮件的示例文档:http: //css-tricks.com/sending-nice-html-email-with-php/

于 2013-08-15T08:35:17.817 回答
1

因为你的邮件正文默认是纯文本,<br />是 HTML 标签。如果你想用 HTML 发送邮件,你需要正确地构建它(我建议你使用PHPMailer类来完成这项工作)。如果你只想换行。你需要使用\n.

于 2013-08-15T08:35:26.557 回答
1

如果您需要使用 html 发送邮件,则必须发送正确的标题:

 $header  = "MIME-Version: 1.0\r\n";
 $header .= "Content-type: text/html; charset: utf8\r\n";
 $to      = "name@domain.com";
 $subject = "Competition Entry - WIN A 2 NIGHT FAMILY PRICE";
 $message = "Name: ".$name. "<br />" ."Surname: ".$surname.;
 mail ($to, $subject, $message, $headers);
于 2013-08-15T08:35:56.427 回答
0

开始使用Swiftmailer,你的生活会更轻松。

require_once('swift/lib/swift_required.php');
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
    ->setFrom(array($from))
    ->setTo(array($to))
    ->setEncoder(Swift_Encoding::get7BitEncoding())
    ->setSubject($subject)
    ->setBody($body, 'text/html')
    ->addPart(strip_tags($body), 'text/plain')
    ->attach(Swift_Attachment::fromPath($filename))
;
$mailer->send($message);
于 2013-08-15T08:44:37.870 回答