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

时间:2018-09-21 10:47:53

标签: php mysql curl tcpdf

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

我执行MySql选择以检索所需的信息,并且尝试使用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,并且代码正在工作。

我尝试用两行代码注释掉了顶部代码(标头和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
//============================================================+

1 个答案:

答案 0 :(得分:1)

您正在发送带有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的参数