如何发送多封电子邮件包含pdf与tcpdf

jutyujz0  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(376)

我试图发送大量的电子邮件与pdf附件使用 CURL 以及 TCPDF .
我做了一个 MySql 选择以检索我需要的信息并尝试使用 CURL 将变量提交到我的 TCPDF 文件。
在cpanel中选择在cronjob上运行的查询

$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代码。
我已经测试过了 SQL phpmyadmin中的语句,它们正在工作。我已经测试过了 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
//============================================================+
huus2vyu

huus2vyu1#

您正在发送 POST 使用curl请求并使用 $post = ['SchoolId' => $SchoolId]; curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); .
但在你的 TCPDF 文件,您正在使用 $SchoolId = $_GET['school']; 因为它是一个 POST 而不是一个 GET 请求。
要解决您的问题,请替换 $_GET['school'];$_POST['SchoolId']; 因为您正在发送名为 SchoolId 而不是 school

相关问题