我一直在尝试让php发送附带PDF的电子邮件,我一直在寻找并发现这篇文章Email PDF Attachment with PHP Using FPDF。
我尝试了解决方案,但仍然没有运气,有谁知道我哪里出错了?我没有错误或电子邮件?
基本上,这样做是从表单中获取值,将它们放入数据库,然后向它们发送电子邮件。
非常感谢任何帮助。
require('fpdf.php');
include("database.php");
require_once('recaptchalib.php');
$publickey = "6LcBYNsSAAAAAKkf5LwKkrhTVTVlxmOblcOn70-r ";
$privatekey = " 6LcBYNsSAAAAAEAa9wFZfyjInRmsWCxpj79Nvqm7";
// email stuff (change data below)
$to = "brentfrench@bigwavemedia.co.uk";
$from = "websupport@bigwavemedia.co.uk";
$subject = "send email with pdf attachment";
$message = "<p>Please see the attachment.</p>";
// PDF Create
$pdf = new FPDF('P', 'pt', array(500,233));
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// attachment name
$filename = "test.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// form values
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$postcode = md5($_POST['post']);
$email = $_POST['email'];
if ($_POST["recaptcha_response_field"]) {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if ($resp->is_valid) {
$check = mysql_query("SELECT * FROM voucher WHERE first='$firstname' AND last='$lastname'");
if(mysql_num_rows($check) != 0)
{
echo "<p>Voucher already in use.</p>";
}
else
{
$insert = 'INSERT into voucher(first, last, postcode, email) VALUES ("'.$firstname.'","'.$lastname.'","'.$postcode.'","'.$email.'")';
mysql_query($insert);
// send message
mail($to, $subject, $message, $headers) or die("Mail Error");
echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>";
}
}
else
{
echo "The anti spam code you entered is not correct.";
}
}
答案 0 :(得分:4)
我会将您的反垃圾邮件检查移至顶部,但我将下面的代码用于工作。我将您的EOL标签更改为“\ r \ n”,并在标题末尾添加了另一个$ eol。此外,我在mail()调用中将$ message更改为$ body,因为所有准备工作都是针对$ body完成的,但是没有使用它。
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = "\r\n";
// attachment name
$filename = "test.txt";
// encode data (puts attachment in proper format)
$attachment = chunk_split(base64_encode("I am text file"));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
// no more headers after this, we start the body! //
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
$body .= "This is a MIME encoded message.".$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$body .= $message.$eol;
// attachment
$body .= "--".$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$body .= "Content-Transfer-Encoding: base64".$eol;
$body .= "Content-Disposition: attachment".$eol.$eol;
$body .= $attachment.$eol;
$body .= "--".$separator."--";
// form values
$firstname = $_POST['first'];
$lastname = $_POST['last'];
$postcode = md5($_POST['post']);
$email = $_POST['email'];
echo mail($to, $subject, $body, $headers) or die("Mail Error");
echo "<p>Voucher has been emailed, we looked foward to seeing you soon</p>\n";
答案 1 :(得分:2)
我写了一个邮件类。附件代码如下:
$body .= "--".$separator."\r\n";
$body .= "Content-Disposition: attachment; filename=\"".$filename."\";\r\n";
$body .= "Content-Length: ".$filesize.";\r\n";
$body .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= $attachment."\r\n";
$body .= "--".$separator."--";
您发现存在一些差异:我已Content-Length
,2x $filename
和\r\n
为EOL。
我希望这会有所帮助。
答案 2 :(得分:1)
这段代码中有很多可能的断点,不可能确切地说出出了什么问题。你需要自己做一些调试,比如说。使用测试输出,看看脚本有多远。另外,请确保激活错误报告。
要发送附件,可以使用SwiftMailer library.