在php中通过邮件使用表发送附件和消息

时间:2017-08-08 10:28:28

标签: php

我想附上一个生成的pdf文件和一条感谢信息,其中包含使用html表格完成的邮件正文中pdf中生成的内容。 附加和邮寄文件工作正常。但谢谢你的消息和细节没有出现在邮件正文中。这是代码。

$pdf->Output('abc-REG.pdf', 'I');
$to = 'abc@gmail.com, ' . $email;
$subject = 'ABC :: ACTIVATION';
$repEmail = 'abc@gmail.com';
$fileName = 'abc-REG.pdf';
$fileatt = $pdf->Output($fileName, 'E');
$attachment = chunk_split($fileatt);
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: ABC <'.$repEmail.'>'.$eol;
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= '<html><body>';
$message .= '<img src="http://google.com/images/logo.png"  /><br/>';
$message .= "Thanks for Activation. Your online registration number is A0". mysql_insert_id() ". "
    $message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>" . strip_tags($_POST['mobile']) . "</td></tr>";
$message .= "<tr><td><strong>Organisation Name:</strong> </td><td>" . strip_tags($_POST['cname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" .  strip_tags($_POST['Address']) . "</td></tr>"; 
$message .= "</table>";
$message .= "</body></html>".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
$message .= $attachment.$eol;
$message .= "--".$separator."--";
if (mail($to, $subject, $message, $headers)){
    echo "Email sent";
}
else {
    echo "Email failed";
}

1 个答案:

答案 0 :(得分:0)

由于缺少时间段,您似乎错过了第一个分隔符:

$message = "--".$separator.$eol; <--- This separator 

$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;  <--- and this encoding is dropped

$message = '<html><body>';  <--- due to missing period here (= not .=)

$message .= '<img src="http://google.com/images/logo.png"  /><br/>';

修改

好的,似乎HTML在内容类型为HTML的适当位置缺失:

...
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= '<html><body>';
$message .= '<img src="http://google.com/images/logo.png"  /><br/>';
$message .= "Thanks for Activation. Your online registration number is A0". mysql_insert_id() ". "
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>" . strip_tags($_POST['mobile']) . "</td></tr>";
$message .= "<tr><td><strong>Organisation Name:</strong> </td><td>" . strip_tags($_POST['cname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" .  strip_tags($_POST['Address']) . "</td></tr>"; 
$message .= "</table>";
$message .= "</body></html>".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;

//missing content here

$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
...

像这样:

...
$headers .= 'MIME-Version: 1.0' .$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$message = "--".$separator.$eol;
$message .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$message .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$message .= '<html><body>';
$message .= '<img src="http://google.com/images/logo.png"  /><br/>';
$message .= "Thanks for Activation. Your online registration number is A0". mysql_insert_id() ". "
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Mobile:</strong> </td><td>" . strip_tags($_POST['mobile']) . "</td></tr>";
$message .= "<tr><td><strong>Organisation Name:</strong> </td><td>" . strip_tags($_POST['cname']) . "</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>" .  strip_tags($_POST['Address']) . "</td></tr>"; 
$message .= "</table>";
$message .= "</body></html>".$eol;
$message .= "--".$separator.$eol;
$message .= "Content-Type: application/pdf; name=\"".$fileName."\"".$eol; 
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment".$eol.$eol;
...
顺便问一下?你需要7位编码部分吗?也许将其标记为内容类型text / plain并提供免费的标记文本版本的邮件?您会惊讶地发现人们不会或不会支持HTML电子邮件。

编辑2

注意到你使用PHP_EOL作为行尾。如果这是从非Windows系统发送的,您可能会得到错误的行结尾。用"\r\n"分隔这些行。

同时尝试将此行设置为正文中的第一行,而不是标题:

$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";

将其更改为:

$message = "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
...

您可能还需要混合使用内容类型混合的内容类型。见这里:http://webcheatsheet.com/php/send_email_text_html_attachment.php