【发布时间】:2021-11-19 06:26:06
【问题描述】:
你好,我正在尝试通过 phpmailer 发送带有输入表单 javascript jspdf 的邮件
邮件发送正常,但如果我通过原始 php 文件获取 $_POST['data'],则文件已损坏。 我如何将带有 $_POST['data'] 的 jspdf 文件传送到 phpmailer?
我尝试了很多方法,但没有多大帮助。 感谢您阅读本文
form_input.php
var create_pdf = document.getElementById("create_pdf");
create_pdf.addEventListener('click', function (event) {
html2canvas($('#pdf_wrap')[0] ,{
//logging : true,
//proxy: "html2canvasproxy.php",
allowTaint : true,
useCORS: true,
scale : 2
}).then(function(canvas) {
var imgData = canvas.toDataURL('image/jpeg');
var doc = new jsPDF("p", "px");
var options = {
pagesplit: true
};
var imgWidth = 210;
var pageHeight = 295;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
var doc = new jsPDF('p', 'mm');
var position = 0;
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
doc.addPage();
doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
//Send mail on other php
$.post("mail_send.php",
{
data: doc.output('datauristring')
}, function () {}).done(function() {/*SOME CODE*/});
// doc.save( 'file.pdf');
});
});
mail_send.php
require 'plugin/PHPMailer/class.phpmailer.php';
require 'plugin/PHPMailer/class.smtp.php';
require 'plugin/PHPMailer/PHPMailerAutoload.php';
if(!empty($_POST['data']))
{
echo $_POST['data'];
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = false; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.test.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'smtpid@dot.com'; // SMTP username
$mail->Password = 'password'; // SMTP password
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true; // TCP port to connect to
$mail->SMTPSecure = 'ssl';
$mail->setFrom('setmail@mail.com', 'Mailer');
$mail->addAddress('user@mail.com', 'Joe User'); // Add a recipient
$mail->CharSet = "utf-8";
// ----------Attachments this doesn't work. delivered broken pdf file.
$base = explode('data:application/pdf;base64,', $_POST['data']);
$mail->addStringAttachment($base, 'pdfName.pdf');
// -----------
$mail->isHTML(true); // Set email format to HTML
$mail->Body = $body;
$mail->Subject = 'Here is the subject';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
【问题讨论】:
标签: php base64 phpmailer jspdf html2pdf