Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用TCPDF发送多封包含PDF的电子邮件_Php_Mysql_Curl_Tcpdf - Fatal编程技术网

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

Php 如何使用TCPDF发送多封包含PDF的电子邮件,php,mysql,curl,tcpdf,Php,Mysql,Curl,Tcpdf,我正在尝试使用CURL和TCPDF发送大量带有PDF附件的电子邮件 我做了一个MySql选择来检索我需要的信息,并尝试使用CURL将变量提交到我的TCPDF文件中 选择在Cpanel中的Cronjob上运行的查询 $stmt = $conn->prepare(" SELECT sID FROM School_Schedule LEFT JOIN Schools ON School_Schedule.School = Schools.s_ID WHERE

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

我做了一个
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代码

我已经在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
请求,并添加带有curl的参数

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

但是在您的
TCPDF
文件中,您使用的是
$SchoolId=$\u GET['school']将始终为空,因为它是
POST
而不是
GET
请求


要解决您的问题,请替换
$\u GET['school'带有
$\u POST['SchoolId']
由于您发送的是名为
SchoolId
的参数,而不是
school

使用curl\u exec后,$response的值是多少?那么,这个
/TCPDF master/s/schoolList.php
web服务方法的功能是什么?它应该发送电子邮件吗?请将您的问题告诉我们。您在代码中的什么位置发送电子邮件?读这个@琼斯。是
/TCPDF master/s/schoolList.php
发送电子邮件。我正确使用curl了吗?@O.Jones。我根据请求添加了TCPDF代码。我按照你的答案做了更改,但不起作用