gpt4 book ai didi

php - 使用 PHP 发送带有 PDF 文件附件的电子邮件

转载 作者:可可西里 更新时间:2023-11-01 12:58:08 26 4
gpt4 key购买 nike

好的,这是我的第一个主题,我在网上搜索过但没有找到。我正在实习,我正在做一个项目,该项目要求我创建一个网页,在用户提交他/她的信息时生成一个 pdf 文件。一旦客户点击提交按钮,就需要发生 3 件事:

  1. 将信息存储到数据库(完成),
  2. 向员工发送包含新客户信息的电子邮件(完成),以及
  3. 向客户发送带有 pdf 文件附件的“感谢信息”电子邮件(无效)。

我的意思是,客户确实收到了一封电子邮件,但是当他/她打开 pdf 文件时,我收到以下错误消息:

"Acrobat could not oen 'file_name' because it is either not a supported file type or because the file has been damaged(for example, it was sent as an email attachment and wasn't correctly decoded)..."

请记住,这是我第一次做有关创建 pdf 文件附件的项目。如果有人可以帮助我解决这个问题,那就太好了。谢谢!

这是我的代码:

<?php
// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
."Email: " .$_POST['email'] . "\n"
."Phone: " .$_POST['telephone'] . "\n"
."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
."Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffinfo";

mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)

//once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Hello World!");

// email information
$to = $_POST['email'];
$from = $staffEmail;
$subject = "Thank you for your business";
$message = "Thank you for submitting your information!";

// 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 = "yourinformation.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));

// encode data (multipart mandatory)
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Enconding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;

// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charsrt=\"iso-8859-1\"".$eol;
$headers .= $message.$eol.$eol;

// attachment
$headers .= "--".$separator.$eol;
//$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Type: application/zip; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";

// send message
mail($to, $subject, $message, $headers);

}
}
?>

最佳答案

// once there are no errors, as soon as the customer hits the submit button, it needs to send an email to the staff with the customer information
$msg = "Name: " .$_POST['name'] . "\n"
."Email: " .$_POST['email'] . "\n"
."Phone: " .$_POST['telephone'] . "\n"
."Number Of Guests: " .$_POST['numberOfGuests'] . "\n"
."Date Of Reunion: " .$_POST['date'];
$staffEmail = "staffemail";

mail($staffEmail, "You have a new customer", $msg); // using the mail php function to send the email. mail(to, subject line, message)

//once the customer submits his/her information, he/she will receive a thank you message attach with a pdf file.
// creating a pdf file
$pdf_filename = tempnam(sys_get_temp_dir(), "pdf");
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont("Arial", "B", 16);
$pdf->Cell(40, 10, "Title");
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Name:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['name']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Email:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['email']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(15, 10, "Phone:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(15, 10, $_POST['telephone']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(40, 10, "Number of Guests:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(40, 10, $_POST['numberOfGuests']);
$pdf->Ln();
$pdf->SetFont("Arial", "", 12);
$pdf->Cell(40, 10, "Date Of Reunion:");
$pdf->SetFont("Arial", "I", 12);
$pdf->Cell(40, 10, $_POST['date']);
// if file doesn't exists or if it is writable, create and save the file to a specific place
if(!file_exists($pdf_filename) || is_writable($pdf_filename)){
$pdf->Output($pdf_filename, "F");
} else {
exit("Path Not Writable");
}

// using the phpmailer class
// create a new instance called $mail and use its properties and methods.
$mail = new PHPMailer();
$staffEmail = "staffemail";
$mail->From = $staffEmail;
$mail->FromName = "name";
$mail->AddAddress($_POST['email']);
$mail->AddReplyTo($staffEmail, "name");

$mail->AddAttachment($pdf_filename);
$mail->Subject = "PDF file attachment";

$mail->Body = "message!";

// if mail cannot be sent, diplay error message
//if(!$mail->Send()){
//echo "<div id=\"mailerrors\">Message could not be sent</div>";
//echo "<div id=\"mailerrors\">Mailer Error: " . $mail->ErrorInfo . "</div>";
//} else { // else...if mail is sent, diplay sent message
//echo "<div id=\"mailerrors\">Message sent</div>";
//}

// delete the temp file
unlink($pdf_filename);
}
}

关于php - 使用 PHP 发送带有 PDF 文件附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8477566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com