php中的邮件头不工作
请帮助我,我在使用phpphp中的邮件头不工作,php,sendmail,email-headers,Php,Sendmail,Email Headers,请帮助我,我在使用phpmail()发送html格式的邮件时遇到问题。 我认为问题在于头球。我已经包括了两个标题,但只有单引号或双引号略有不同: 标题1: $headers = 'From: webmaster@example.com\r\n Reply-To: webmaster@example.com'; $headers .= '\r\nContent-Type: multipart/alternative; boundary="'.$random_hash.'"'; $headers
mail()发送html格式的邮件时遇到问题。
我认为问题在于头球。我已经包括了两个标题,但只有单引号或双引号略有不同:
标题1:
$headers = 'From: webmaster@example.com\r\n Reply-To: webmaster@example.com';
$headers .= '\r\nContent-Type: multipart/alternative; boundary="'.$random_hash.'"';
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";
当我像上面那样使用单引号时,我所有的html代码都以简单文本的形式打印在邮件中,没有正确的html格式。此外,我的标题在\r\n
缺失后显示为全部混乱
标题2:
$headers = 'From: webmaster@example.com\r\n Reply-To: webmaster@example.com';
$headers .= '\r\nContent-Type: multipart/alternative; boundary="'.$random_hash.'"';
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";
使用这一个,我得到了一个完美的标题,但现在我的邮件发送空的附件。我不知道这是从哪里来的,因为我没有在邮件上附加任何东西
如果在PHP字符串上使用单引号,请建议如何操作,如转义字符\r\n将停止工作
我不知道如何在没有更多上下文的情况下帮助处理附件。如果在PHP字符串上使用单引号,则转义字符\r\n将停止工作
我不知道如何在没有更多上下文的情况下帮助您处理附件。我不喜欢php邮件。我建议使用XpertMailer:做得很好。我不喜欢php邮件。我建议使用XpertMailer:做一件出色的工作。
- 使用
- 如果你做不到,就不要重新发明轮子
- 用一把枪李>
- 使用
- 如果你做不到,就不要重新发明轮子
- 用一把枪李>
// 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."--";
这是我在切换到phpmailer之前使用的。如前所述,使用图书馆// 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."--";
如果您实际上没有使用多部分边界定义两个版本(纯文本/HTML),那么您应该将内容类型:multipart/alternative更改为邮件正文的适当内容类型
此外,像PHPMailer等库通常比PHP的原生
函数更受欢迎,因为它们提供了更大的灵活性,同时不需要您手动构造复杂的标题。如果您实际上没有定义两个版本(纯文本/HTML)对于多部分边界,您应该将内容类型:multipart/alternative更改为邮件正文的适当内容类型mail()
此外,像PHPMailer等库通常比PHP本机的
函数更受欢迎,因为它们提供了更大的灵活性,同时不需要您手动构造复杂的头。+1个漂亮的库,还没有见过这个库。看起来不错+1个不错的图书馆,还没见过那个。看起来不错!我不能忍受“不要重新发明轮子”这句话,因为它需要假设轮子天生是完美的(而且任何框架/库何时都是完美的)。。。但你是我想把正确的观点带回家+1我无法忍受“不要重新发明轮子”这句话,因为它需要假设轮子天生是完美的(而且任何框架/库何时都是完美的)。。。但你是我想把正确的观点带回家+1.mail()