1

请帮助我,我在使用 php 发送 html 格式的邮件时遇到问题,mail() 我认为问题出在标题上。我包含了两个仅在单引号或双引号中略有不同的标题:

标题 1:

$headers = 'From: webmaster@example.com\r\n Reply-To: webmaster@example.com';
$headers .= '\r\nContent-Type: multipart/alternative; boundary="'.$random_hash.'"'; 

当我像上面那样使用单引号时,我所有的 html 代码都作为简单的文本打印在邮件中,没有正确的 html 格式。\r\n此外,我的标题在丢失后显示所有内容都搞砸了。

标题 2:

$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";

使用这个,我得到了一个完美的标题,但现在我的邮件是空的,附件是空的。我不知道这是从哪里来的,因为我没有在邮件中附加任何内容。

请建议做什么

4

5 回答 5

5

If you use single quotes on your PHP strings escape characters like \r\n will stop working.

I'm not sure how to help with your attachment without more context.

于 2010-11-19T20:34:51.457 回答
4

I don't like php mail. I recommend use XpertMailer: http://www.xpertmailer.com/ do an excellent work.

于 2010-11-19T20:38:51.123 回答
3
  • Use a library
  • Don't reinvent the wheel if you can't make it round.
  • Use a library!
于 2010-11-19T20:39:50.833 回答
0

This is what I used before switching to phpmailer. As mentioned use a library.

// Make email headers
$separator = '--==Multipart_Boundary_'.md5(time());
$eol = PHP_EOL;

$filepath = "filename.pdf";
// open pdf file
$handle = fopen($filepath, "r");
// read pdf file
$f_contents=fread($handle,filesize($filepath));
// encode read pdf file
$attachment = chunk_split(base64_encode($f_contents));
// close pdf file
fclose($handle);
$message = "Text goes here";

// main header (multipart mandatory)
$headers = "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "X-Priority: 1 (Highest)".$eol;
$headers .= "X-MSMail-Priority: High".$eol;
$headers .= "Importance: High".$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/plain; charset=utf-8".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol;

// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/pdf; name=".$filename.$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment; filename=".$filename.$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
于 2010-11-19T20:40:55.053 回答
0

If you're not actually defining two versions (plaintext/HTML) with multipart boundaries then you should change the Content-type: multipart/alternative to the proper content-type for your mail body.

Additionally, libraries like PHPMailer, et cetera are generally preferred over PHP's native mail() function because they offer a great deal more flexibility while not requiring you to manually construct complex headers.

于 2010-11-19T20:41:03.413 回答