TCPDF:phpMail附件为空
我正在使用TCPDF创建配置文件,并将配置文件作为附件发送。我有许多客户使用该软件,因此需要批量电子邮件服务 我一直在从本地服务器发送配置文件的电子邮件,它工作得很好 这是前面的代码(只是电子邮件部分):TCPDF:phpMail附件为空,php,phpmailer,tcpdf,Php,Phpmailer,Tcpdf,我正在使用TCPDF创建配置文件,并将配置文件作为附件发送。我有许多客户使用该软件,因此需要批量电子邮件服务 我一直在从本地服务器发送配置文件的电子邮件,它工作得很好 这是前面的代码(只是电子邮件部分): $pdf->writeHTML($tbl, true, false, false, false, ''); $js .= 'print(true);'; // set javascript $pdf->IncludeJS($js); //Close and output P
$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
//============================================================+
$pdf->writeHTML($tbl,true,false,false,”);
$js.='打印(真);';
//设置javascript
$pdf->IncludeJS($js);
//关闭并输出PDF文档
//关闭并输出PDF文档
//定义电子邮件的收件人
$to=$Email;
$subject=“Profile”;
$repEmail=me@mydomain.com';
$fileName='Profile.pdf';
$fileatt=$pdf->Output($fileName,'S');
$attachment=chunk_split(base64_encode($fileatt));
$eol=PHP\u eol;
$separator=md5(time());
$headers='From:Identykidz'$eol;
$headers.=“MIME版本:1.0”。$eol;
$headers.=“内容类型:多部分/混合;边界=\”.$separator.\”;
$message=“--”$separator.$eol;
$message.=“内容传输编码:7bit”。$eol.$eol;
$message.=“亲爱的家长/监护人\r\n\r\n请查看附件中的个人资料。亲切问候\r\n nIdentyKidz信息中心”。$eol;
$message.=“-”$separator.$eol;
$message.=“内容类型:text/html;字符集=\“iso-8859-1\”。$eol;
$message.=“内容传输编码:8位”。$eol.$eol;
$message.=“-”$separator.$eol;
$message.=“内容类型:application/pdf;name=\”.$fileName.\”.$eol;
$message.=“内容传输编码:base64”。$eol;
$message.=“内容处置:附件”。$eol.$eol;
$message.=$attachment.$eol;
$message.=“--”“分隔符”“--”;
//发送电子邮件
if(邮件($to、$subject、$message、$headers)){
echo“配置文件已成功发送到输入的电子邮件地址”;
}
否则{
echo“发送邮件时出错。”;
}
}
//捕获异常
捕获(例外$e){
标题(“Location:something_出错了。php”);
}
//============================================================+
//文件结束
//============================================================+
因为我需要使用批量电子邮件服务,我必须包含批量电子邮件服务提供商的SMTP详细信息,所以我将TCPDF的电子邮件部分从mail()更改为phpMail()
以下是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
//============================================================+
$pdf->writeHTML($tbl,true,false,false,”);
$js.='打印(真);';
//设置javascript
$pdf->IncludeJS($js);
$fileatt=$pdf->Output('Profile.pdf','S');
//我也尝试过$fileatt=$pdf->Output('Profile.pdf','I');
require('PHPMailer/phpmailerautoad.php');
$pdf\u content=file\u get\u contents($fileatt);
$sender=Me@mydomain.com';
$senderName=‘信息中心’;
//替换recipient@example.com带有“收件人”地址。如果你的帐户
//仍在沙箱中,必须验证此地址。
$recipient=$Email;
//将smtp_用户名替换为您的Amazon SES smtp用户名。
$usernameSmtp='Mycredentials';
//将smtp_密码替换为您的Amazon SES smtp密码。
$passwordSmtp='Mycredentials';
//指定一个配置集。如果您不想使用配置
//设置、注释或删除下一行。
//$configurationSet='ConfigSet';
//如果您在美国西部(俄勒冈州)以外的地区使用亚马逊SES,
//将email-smtp.us-west-2.amazonaws.com替换为Amazon SES smtp
//端点位于适当的区域。
$host='email smtp.eu-central-1.com';
$port=587;
//电子邮件的主题行
$subject=“IdentyKidz$name的配置文件”;
//电子邮件的纯文本正文
$bodyText=“”;
//电子邮件的HTML格式正文
$bodyHtml=“亲爱的家长/监护人
亲切问候
IdentyKidz信息中心”;
$mail=新的PHPMailer(true);
试一试{
//指定SMTP设置。
$mail->isSMTP();
$mail->setFrom($sender,$senderName);
$mail->Username=$usernamemtp;
$mail->Password=$passwordSmtp;
$mail->Host=$Host;
$mail->Port=$Port;
$mail->SMTPAuth=true;
$mail->SMTPSecure='tls';
$mail->addCustomHeader('X-SES-CONFIGURATION-SET',$configurationSet);
//指定邮件收件人。
$mail->addAddress($recipient);
//您还可以在此处向收件人添加抄送、密件抄送和其他内容。
//指定消息的内容。
$mail->isHTML(true);
$mail->Subject=$Subject;
$mail->Body=$bodyHtml;
$mail->AltBody=$bodyText;
$mail->AddStringAttachment($pdf_内容,“profile.pdf”,“base64”,“application/pdf”);
//发送电子邮件
如果($mail->Send()){
echo“配置文件已成功发送到输入的电子邮件地址”;
}
否则{
echo“发送邮件时出错。”;
}
}捕获(phpmailerException$e){
echo“发生错误。{$e->errorMessage()}”,PHP_EOL;//从PHPMailer捕获错误。
}捕获(例外$e){
echo“Email not sent.{$mail->ErrorInfo}”,PHP_EOL;//从Amazon SES捕获错误。
}
}
//捕获异常
捕获(例外$e){
标题(“Location:something_出错了。php”);
}
//============================================================+
//文件结束
//============================================================+
第二个代码(PHP Mailer)发送电子邮件,但表明PDF已附加,但附件为空
我遗漏了什么?我想也许你必须对附件进行解码。见第一个答案:
$mail->AddStringAttachment(base64_decode($pdf_内容)、“profile.pdf”、“base64”、“application/pdf”);
AddStringAttachment
只告诉编码。您好,CharlesWaschington,我已经检查了您的代码示例:
根据您的代码,我看到您正在尝试从字符串加载文件
这让我有点困惑,因为我立即收到了一个错误。您是否启用了错误报告
和错误日志
并检查了日志?无论如何,您只需要将字符串放入正确的变量中,其他一切都可以正常工作。下面您将看到我对您的代码所做的更改
之前:
$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");
$mail->AllowEmpty = true;
$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);
$mail->AddStringAttachment($fileatt, "Prodile.pdf", "base64", "application/pdf");