php - 如何使用 TCPDF 发送多封包含 PDF 的电子邮件

标签 php mysql curl tcpdf

我正在尝试使用 CURLTCPDF 发送大量带有 PDF 附件的电子邮件。

我做了一个 MySql Select 来检索我需要的信息,我尝试使用 CURL 将变量提交到我的 TCPDF 文件。

在 Cpanel 中的 Cronjob 上运行的 SELECT 查询

$stmt = $conn->prepare("
    SELECT sID
    FROM School_Schedule 
    LEFT JOIN Schools ON School_Schedule.School = Schools.s_ID 
    WHERE Date_Schedule = (CURDATE() - INTERVAL 2 DAY)
");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $SchoolId = $row["sID"];
    $post = ['SchoolId' => $SchoolId];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://www.test.com.au/secure/TCPDF-master/s/schoolList.php');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
    $response = curl_exec($ch);
    var_export($response);
    //header("Location:../secure/TCPDF-master/s/schoolList.php?school=" . $SchoolId . "");
    //foreach($post as $SchoolId){
    //    header("Location:../secure/TCPDF-master/s/schoolList.php?school=" . $SchoolId . "");
    //}
}

在 TCPDF 中我得到变量:

$SchoolId = $_GET['SchoolId'];

然后在 TCPDF 中选择一个

SELECT RefNr, SchoolName, ChildInitials, SchoolEmail, Date_issued FROM Ref 
LEFT JOIN School_Schedule ON School_Schedule.SchoolList = Ref.Schools
LEFT JOIN Schools ON Schools.sc_ID = Ref.Schools
WHERE Schools.sID = $SchoolId

然后是正常的 TCPDF 代码的其余部分。

我已经在 phpMyAdmin 中测试了 SQL 语句,它们可以正常工作。 我已经使用硬编码值测试了 TCPDF,代码可以正常工作。

我已经尝试使用注释掉的两行代码(header 和 foreach),但没有成功。

无效 我的意思是电子邮件不是由脚本发送的。 控制台没有错误日志。

我怀疑我没有有效地使用 CURL。这是我第一次使用它。

**编辑:** 下面是TCPDF代码

<?php

$SchoolId = $_GET['SchoolId'];

require_once('tcpdf_include.php');

require_once('../../connect.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, 'LETTER', true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Identykidz');
$pdf->SetTitle('Identykidz Child Identity Kit');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' ', PDF_HEADER_STRING);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set certificate file
$certificate = 'file://data/cert/tcpdf.crt';

// set additional information
$info = array(
'Name' => 'Name',
'Location' => 'My Address',
'Reason' => 'Reason',
'ContactInfo' => 'https://www.test.com.au',
);

// set document signature
$pdf->setSignature($certificate, $certificate, 'tcpdfdemo', '', 2, $info);

// set font
$pdf->SetFont('helvetica', 'B', 7);

// add a page
$pdf->AddPage();
//output the HTML content
$tbl_header = '<table border="1" bordercolor=”grey”>';
$tbl_footer = '</table>';
$text ='';

// print a line of text
$findSchoolList = $conn->prepare("SELECT RefNr, SchoolName, ChildInitials, SchoolEmail, Date_issued FROM Ref 
LEFT JOIN School_Schedule ON School_Schedule.SchoolList = Ref.Schools
LEFT JOIN Schools ON Schools.sc_ID = Ref.Schools
WHERE Schools.sID = $SchoolId");
$findSchoolList->execute();

while ($result = $findSchoolList->fetch(PDO::FETCH_ASSOC)){
$RefNr= htmlentities($result['RefNr']);
$SchoolName = htmlentities($result['SchoolName']);
$ChildInitials = htmlentities($result['ChildInitials']);
$SchoolEmail = htmlentities($result['SchoolEmail']);    
$Date_issued= htmlentities($result['Date_issued']);


 $text .= '<tr><td>' . $UniqueRefNr . '</td><td>' . $ChildInitials . '</td><td>' . $School . '</td></tr>';


 }

$pdf->writeHTML($tbl_header . $text . $tbl_footer, true, false, false, false, '');

$pdf->setSignatureAppearance(180, 60, 15, 15);

$pdf->addEmptySignatureAppearance(180, 80, 15, 15);

$to = $SchoolEmail;
$subject = $School;
$repEmail = 'info@test.com.au';

$fileName = 'FileName.pdf';
$fileatt = $pdf->Output($fileName, 'S');
$attachment = chunk_split(base64_encode($fileatt));
$eol = PHP_EOL;
$separator = md5(time());

$headers = 'From: Test <'.$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.  \r\n\r\nPlease find attached list as requested.\r\n".$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 "Sent";
}
else {

  echo "There was an error sending the mail.";
}
exit;
//============================================================+
// END OF FILE
//============================================================+

最佳答案

您正在使用 curl 发送一个 POST 请求并使用

添加参数

$post = ['SchoolId' => $SchoolId]; curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));.

但是在您的 TCPDF 文件中,您使用的是 $SchoolId = $_GET['school']; 因为它是一个 POST,所以它永远是空的 而不是 GET 请求。

要解决您的问题,请将 $_GET['school']; 替换为 $_POST['SchoolId']; 因为您要发送名为 的参数SchoolId 而不是 school

关于php - 如何使用 TCPDF 发送多封包含 PDF 的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52442063/

相关文章:

unit-testing - 您如何对需要执行 http 请求的方法进行单元测试

php - 在js文件中包含php

php - 服务器迁移后图像文件名中的特殊字符编码

php - 如何在 Laravel 中使用基本路由方法?

mysql - 在MySQL中选择不包含某列的表

MySQL 只获取一列一次

PHP MySQL 统计具有特定值的 SELECT COUNT 记录并排序

python - 使用 session 身份验证通过带有 Tasty Pie 的 curl 进行身份验证

php - Netbeans 中的 Yii 框架 - 对象方法自动完成未出现

python - 为什么 Pocket API 总是返回 403 Forbidden?