TCPDF:PHPMailer 附件为空
TCPDF: PHPMailer attachment is empty
我正在使用 TCPDF 创建配置文件并将配置文件作为附件发送。我有很多客户使用该软件,因此需要群发电子邮件服务。
我一直在从本地服务器电子邮件发送配置文件,它运行良好。
这是之前的代码(只是邮件部分):
$pdf->writeHTML($tbl, true, false, false, false, '');
$js .= 'print(true);';
// set javascript
$pdf->IncludeJS($js);
//Close and output PDF document
//Close and output PDF document
//define the receiver of the email
$to = $Email;
$subject = "Profile";
$repEmail = 'me@mydomain.com';
$fileName = 'Profile.pdf';
$fileatt = $pdf->Output($fileName, 'S');
$attachment = chunk_split(base64_encode($fileatt));
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Identykidz <'.$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 .= "Dear parent/guardian \r\n\r\nPlease find attached the Profile attached.Kind regards\r\nIdentyKidz Information Centre".$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."--";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';
}
else {
echo "There was an error sending the mail.";
}
}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}
//============================================================+
// END OF FILE
//============================================================+
因为我需要使用批量电子邮件服务,所以我必须包括批量电子邮件服务提供商的 SMTP 详细信息,所以我将 TCPDF 的电子邮件部分从 mail() 更改为 PHPMailer。
这是 TCPDF 代码的新电子邮件部分:
$pdf->writeHTML($tbl, true, false, false, false, '');
$js .= 'print(true);';
// set javascript
$pdf->IncludeJS($js);
$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');
require ('PHPMailer/PHPMailerAutoload.php');
$pdf_content = file_get_contents($fileatt);
$sender = 'Me@mydomain.com';
$senderName = 'Information Centre';
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
$recipient = $Email;
// Replace smtp_username with your Amazon SES SMTP user name.
$usernameSmtp = 'Mycredentials';
// Replace smtp_password with your Amazon SES SMTP password.
$passwordSmtp = 'Mycredentials';
// Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
//$configurationSet = 'ConfigSet';
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$host = 'email-smtp.eu-central-1.com';
$port = 587;
// The subject line of the email
$subject = "IdentyKidz Profile of $name";
// The plain-text body of the email
$bodyText = "";
// The HTML-formatted body of the email
$bodyHtml = "Dear parent/guardian <br><br>
Kind regards<br>IdentyKidz Information Centre";
$mail = new PHPMailer(true);
try {
// Specify the SMTP settings.
$mail->isSMTP();
$mail->setFrom($sender, $senderName);
$mail->Username = $usernameSmtp;
$mail->Password = $passwordSmtp;
$mail->Host = $host;
$mail->Port = $port;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet);
// Specify the message recipients.
$mail->addAddress($recipient);
// You can also add CC, BCC, and additional To recipients here.
// Specify the content of the message.
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $bodyHtml;
$mail->AltBody = $bodyText;
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");
// Send the email
if($mail->Send()) {
echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';
}
else {
echo "There was an error sending the mail.";
}
} catch (phpmailerException $e) {
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}
}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}
//============================================================+
// END OF FILE
//============================================================+
第二个代码(PHP Mailer)发送了邮件但是却指出附上了PDF,但是附件是空的。
我错过了什么?
我想也许你必须 base64_decode
附件。见here,第一个回答:
$mail->AddStringAttachment(base64_decode($pdf_content), "Prodile.pdf", "base64", "application/pdf");
3rd parameter of AddStringAttachment
only tells the encoding but does not do any encoding for you apparently.
你好 CharlesWashington,我查看了你的代码示例:
根据您的代码,我看到您正在尝试从字符串加载文件。
这让我有些疑惑,因为我立即收到错误消息。您是否启用了 error_reporting
和 error_log
并检查了您的日志?无论如何,您只需要将字符串放入正确的变量中,其他一切都有效。您会在下面找到我对您的代码所做的更改。
之前:
$fileatt = $pdf->Output('Profile.pdf', 'S'); // S = Means return the PDF as string
// ... code ...
$pdf_content = file_get_contents($fileatt);
// .. code
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");
之后:
$pdf_content = $pdf->Output('Profile.pdf', 'S');
// ... code ...
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");
我刚刚使用 SES 凭据和自定义 PDF 以及之前生成的 PDF 测试了您的脚本。
如果这有帮助,请告诉我。
试试这个方法
$mail->AllowEmpty = true;
我能够根据 ivion 的评论对问题进行排序。
$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");
通过使用
$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');
require ('PHPMailer/PHPMailerAutoload.php');
$pdf_content = file_get_contents($fileatt);
您正在尝试从字符串中读取 file_content。
您只需通过
发送附件即可
$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");
我正在使用 TCPDF 创建配置文件并将配置文件作为附件发送。我有很多客户使用该软件,因此需要群发电子邮件服务。
我一直在从本地服务器电子邮件发送配置文件,它运行良好。
这是之前的代码(只是邮件部分):
$pdf->writeHTML($tbl, true, false, false, false, '');
$js .= 'print(true);';
// set javascript
$pdf->IncludeJS($js);
//Close and output PDF document
//Close and output PDF document
//define the receiver of the email
$to = $Email;
$subject = "Profile";
$repEmail = 'me@mydomain.com';
$fileName = 'Profile.pdf';
$fileatt = $pdf->Output($fileName, 'S');
$attachment = chunk_split(base64_encode($fileatt));
$eol = PHP_EOL;
$separator = md5(time());
$headers = 'From: Identykidz <'.$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 .= "Dear parent/guardian \r\n\r\nPlease find attached the Profile attached.Kind regards\r\nIdentyKidz Information Centre".$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."--";
// Send the email
if(mail($to, $subject, $message, $headers)) {
echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';
}
else {
echo "There was an error sending the mail.";
}
}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}
//============================================================+
// END OF FILE
//============================================================+
因为我需要使用批量电子邮件服务,所以我必须包括批量电子邮件服务提供商的 SMTP 详细信息,所以我将 TCPDF 的电子邮件部分从 mail() 更改为 PHPMailer。
这是 TCPDF 代码的新电子邮件部分:
$pdf->writeHTML($tbl, true, false, false, false, '');
$js .= 'print(true);';
// set javascript
$pdf->IncludeJS($js);
$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');
require ('PHPMailer/PHPMailerAutoload.php');
$pdf_content = file_get_contents($fileatt);
$sender = 'Me@mydomain.com';
$senderName = 'Information Centre';
// Replace recipient@example.com with a "To" address. If your account
// is still in the sandbox, this address must be verified.
$recipient = $Email;
// Replace smtp_username with your Amazon SES SMTP user name.
$usernameSmtp = 'Mycredentials';
// Replace smtp_password with your Amazon SES SMTP password.
$passwordSmtp = 'Mycredentials';
// Specify a configuration set. If you do not want to use a configuration
// set, comment or remove the next line.
//$configurationSet = 'ConfigSet';
// If you're using Amazon SES in a region other than US West (Oregon),
// replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP
// endpoint in the appropriate region.
$host = 'email-smtp.eu-central-1.com';
$port = 587;
// The subject line of the email
$subject = "IdentyKidz Profile of $name";
// The plain-text body of the email
$bodyText = "";
// The HTML-formatted body of the email
$bodyHtml = "Dear parent/guardian <br><br>
Kind regards<br>IdentyKidz Information Centre";
$mail = new PHPMailer(true);
try {
// Specify the SMTP settings.
$mail->isSMTP();
$mail->setFrom($sender, $senderName);
$mail->Username = $usernameSmtp;
$mail->Password = $passwordSmtp;
$mail->Host = $host;
$mail->Port = $port;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET', $configurationSet);
// Specify the message recipients.
$mail->addAddress($recipient);
// You can also add CC, BCC, and additional To recipients here.
// Specify the content of the message.
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $bodyHtml;
$mail->AltBody = $bodyText;
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");
// Send the email
if($mail->Send()) {
echo '<span style="color:#1e73be; text-align: center; font-family: Verdana">The profile has been sent successfully to the email address entered.</span>';
}
else {
echo "There was an error sending the mail.";
}
} catch (phpmailerException $e) {
echo "An error occurred. {$e->errorMessage()}", PHP_EOL; //Catch errors from PHPMailer.
} catch (Exception $e) {
echo "Email not sent. {$mail->ErrorInfo}", PHP_EOL; //Catch errors from Amazon SES.
}
}
//catch exception
catch(Exception $e) {
header("Location: something_went_wrong.php");
}
//============================================================+
// END OF FILE
//============================================================+
第二个代码(PHP Mailer)发送了邮件但是却指出附上了PDF,但是附件是空的。
我错过了什么?
我想也许你必须 base64_decode
附件。见here,第一个回答:
$mail->AddStringAttachment(base64_decode($pdf_content), "Prodile.pdf", "base64", "application/pdf");
3rd parameter of AddStringAttachment
only tells the encoding but does not do any encoding for you apparently.
你好 CharlesWashington,我查看了你的代码示例:
根据您的代码,我看到您正在尝试从字符串加载文件。
这让我有些疑惑,因为我立即收到错误消息。您是否启用了 error_reporting
和 error_log
并检查了您的日志?无论如何,您只需要将字符串放入正确的变量中,其他一切都有效。您会在下面找到我对您的代码所做的更改。
之前:
$fileatt = $pdf->Output('Profile.pdf', 'S'); // S = Means return the PDF as string
// ... code ...
$pdf_content = file_get_contents($fileatt);
// .. code
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");
之后:
$pdf_content = $pdf->Output('Profile.pdf', 'S');
// ... code ...
$mail->AddStringAttachment($pdf_content, "Prodile.pdf", "base64", "application/pdf");
我刚刚使用 SES 凭据和自定义 PDF 以及之前生成的 PDF 测试了您的脚本。
如果这有帮助,请告诉我。
试试这个方法
$mail->AllowEmpty = true;
我能够根据 ivion 的评论对问题进行排序。
$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");
通过使用
$fileatt = $pdf->Output('Profile.pdf', 'S');
//I have also tried $fileatt = $pdf->Output('Profile.pdf', 'I');
require ('PHPMailer/PHPMailerAutoload.php');
$pdf_content = file_get_contents($fileatt);
您正在尝试从字符串中读取 file_content。
您只需通过
发送附件即可 $mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");