如何在 php 中创建临时文件以通过电子邮件发送?
How to create temporary files in php to send them by email?
我有这段代码可以让我从 base64 编码的 XML 文件中检索字符串。
我把它放在一个 TXT 文件中,然后我对其进行解码以获得所需的 PDF。
然后我将此 PDF 作为附件发送,并将 base64 字符串插入数据库。然后我从文件夹中删除TXT文件和PDF。
一切正常。但是我想要一个不在服务器上创建这些文件然后删除它们的解决方案。
我正在寻找更安全的解决方案,我尝试了tmpfile
功能但是文件不是通过邮件发送的,因为它被直接删除了。
你能帮帮我吗?
<?php
//Function to keep only the content of the <GraphicImage> tags
function multiSplit($string)
{
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach ($cols as $col)
{
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$envoi = $submission_id.'_envoi';
//We put the content of the tags in the variable $DHL
$DHL = multiSplit($output);
//We create two unique file names
$filename1 = $envoi.'.txt';
$filename2 = $envoi.'.pdf';
//We write the content of the tags in the txt file
$file1 = "D:/Data/Sites/sav/API_label/TXT/$filename1";
file_put_contents($file1, print_r($DHL[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$pdf_base64 = $file1;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ("D:/Data/Sites/sav/API_label/PDF/$filename2",'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// echo 'Done';
//SendGrid Part
$email = new Mail();
$email->setFrom("test@test.com", "Sender");
$email->addTo("$EMAIL", "Recipient");
$email->setSubject("Etiquettes DHL");
$email->addContent("text/html", "<strong>Hello</strong>");
$file_encoded = base64_encode(file_get_contents("D:/Data/Sites/sav/retours/API_label/PDF/$filename2"));
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.m7481IOix24');
try {
$response = $sendgrid->send($email);
} catch (Exception $e) {
// echo 'Caught exception: ' . $e->getMessage() . "\n";
}
//Insertion of the values in the database
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label_envoi, commercial_invoice)
VALUES (:submission_id, :formID, :NOM, :EMAIL, :ADRESSE, :TELEPHONE, :file_encoded, :COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
// Execute query
$query->execute(array(':submission_id' => $submission_id, ':formID' => $formID, ':NOM' => $NOM, ':EMAIL' => $EMAIL, ':ADRESSE' => $ADRESSE, ':TELEPHONE' => $TELEPHONE,
':file_encoded' => $file_encoded, ':COMMERCIAL_INVOICE' => $COMMERCIAL_INVOICE));
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
//Deleting txt and pdf files
//PDF
unlink("D:/Data/Sites/sav/API_label/PDF/$filename2");
//TXT
unlink("D:/Data/Sites/sav/API_label/TXT/$filename1");
除非我误解了什么,否则这两个临时文件似乎没有增加任何价值并且可以被删除 - 相反,您可以直接使用您写入文件的变量作为下一步的输入过程。
此版本应在不写入任何文件的情况下完成相同的过程:
//Function to keep only the content of the <GraphicImage> tags
function multiSplit($string) {
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach($cols as $col) {
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$envoi = $submission_id.'_envoi';
$filename2 = $envoi.'.pdf';
//We put the content of the tags in the variable $DHL
$DHL = multiSplit($output);
//We write the content of the tags
$print1 = print_r($DHL[1], true).PHP_EOL;
//Decode pdf content
$pdf_decoded = base64_decode($print1);
//SendGrid Part
$email = new Mail();
$email->setFrom("test@test.com", "Sender");
$email->addTo("$EMAIL", "Recipient");
$email->setSubject("Etiquettes DHL");
$email->addContent("text/html", "<strong>Hello</strong>");
$file_encoded = base64_encode($pdf_decoded);
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.m7481IOix24');
try {
$response = $sendgrid->send($email);
} catch (Exception $e) {
// echo 'Caught exception: ' . $e->getMessage() . "\n";
}
//Insertion of the values in the database
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label_envoi, commercial_invoice)
VALUES(:submission_id,:formID,:NOM,:EMAIL,:ADRESSE,:TELEPHONE,:file_encoded,:COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
// Execute query
$query->execute(array(':submission_id' => $submission_id, ':formID' => $formID, ':NOM' => $NOM, ':EMAIL' => $EMAIL, ':ADRESSE' => $ADRESSE, ':TELEPHONE' => $TELEPHONE, ':file_encoded' => $file_encoded, ':COMMERCIAL_INVOICE' => $COMMERCIAL_INVOICE));
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
}
我不清楚你为什么使用 base64_decode
来读取第一个文件,因为没有证据表明它是 base64 编码的(它只是 print_r据我所知,关于某些 XML 的声明)。因此我也删除了它。
我有这段代码可以让我从 base64 编码的 XML 文件中检索字符串。 我把它放在一个 TXT 文件中,然后我对其进行解码以获得所需的 PDF。 然后我将此 PDF 作为附件发送,并将 base64 字符串插入数据库。然后我从文件夹中删除TXT文件和PDF。
一切正常。但是我想要一个不在服务器上创建这些文件然后删除它们的解决方案。
我正在寻找更安全的解决方案,我尝试了tmpfile
功能但是文件不是通过邮件发送的,因为它被直接删除了。
你能帮帮我吗?
<?php
//Function to keep only the content of the <GraphicImage> tags
function multiSplit($string)
{
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach ($cols as $col)
{
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$envoi = $submission_id.'_envoi';
//We put the content of the tags in the variable $DHL
$DHL = multiSplit($output);
//We create two unique file names
$filename1 = $envoi.'.txt';
$filename2 = $envoi.'.pdf';
//We write the content of the tags in the txt file
$file1 = "D:/Data/Sites/sav/API_label/TXT/$filename1";
file_put_contents($file1, print_r($DHL[1], true) . PHP_EOL, FILE_APPEND | LOCK_EX);
$pdf_base64 = $file1;
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ("D:/Data/Sites/sav/API_label/PDF/$filename2",'w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
// echo 'Done';
//SendGrid Part
$email = new Mail();
$email->setFrom("test@test.com", "Sender");
$email->addTo("$EMAIL", "Recipient");
$email->setSubject("Etiquettes DHL");
$email->addContent("text/html", "<strong>Hello</strong>");
$file_encoded = base64_encode(file_get_contents("D:/Data/Sites/sav/retours/API_label/PDF/$filename2"));
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.m7481IOix24');
try {
$response = $sendgrid->send($email);
} catch (Exception $e) {
// echo 'Caught exception: ' . $e->getMessage() . "\n";
}
//Insertion of the values in the database
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label_envoi, commercial_invoice)
VALUES (:submission_id, :formID, :NOM, :EMAIL, :ADRESSE, :TELEPHONE, :file_encoded, :COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
// Execute query
$query->execute(array(':submission_id' => $submission_id, ':formID' => $formID, ':NOM' => $NOM, ':EMAIL' => $EMAIL, ':ADRESSE' => $ADRESSE, ':TELEPHONE' => $TELEPHONE,
':file_encoded' => $file_encoded, ':COMMERCIAL_INVOICE' => $COMMERCIAL_INVOICE));
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage();
}
//Deleting txt and pdf files
//PDF
unlink("D:/Data/Sites/sav/API_label/PDF/$filename2");
//TXT
unlink("D:/Data/Sites/sav/API_label/TXT/$filename1");
除非我误解了什么,否则这两个临时文件似乎没有增加任何价值并且可以被删除 - 相反,您可以直接使用您写入文件的变量作为下一步的输入过程。
此版本应在不写入任何文件的情况下完成相同的过程:
//Function to keep only the content of the <GraphicImage> tags
function multiSplit($string) {
$output = array();
$cols = explode("<GraphicImage>", $string);
foreach($cols as $col) {
$dashcols = explode("</GraphicImage>", $col);
$output[] = $dashcols[0];
}
return $output;
}
$envoi = $submission_id.'_envoi';
$filename2 = $envoi.'.pdf';
//We put the content of the tags in the variable $DHL
$DHL = multiSplit($output);
//We write the content of the tags
$print1 = print_r($DHL[1], true).PHP_EOL;
//Decode pdf content
$pdf_decoded = base64_decode($print1);
//SendGrid Part
$email = new Mail();
$email->setFrom("test@test.com", "Sender");
$email->addTo("$EMAIL", "Recipient");
$email->setSubject("Etiquettes DHL");
$email->addContent("text/html", "<strong>Hello</strong>");
$file_encoded = base64_encode($pdf_decoded);
$email->addAttachment($file_encoded, "application/pdf", "$filename2", "attachment");
$sendgrid = new \SendGrid('SG.m7481IOix24');
try {
$response = $sendgrid->send($email);
} catch (Exception $e) {
// echo 'Caught exception: ' . $e->getMessage() . "\n";
}
//Insertion of the values in the database
try {
// Connect to db
$db = new db('mysql:dbname=jotform; host=localhost', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Set SQL
$sql = 'INSERT INTO DHL (submission_id, formID, identite, email, adresse, telephone, label_envoi, commercial_invoice)
VALUES(:submission_id,:formID,:NOM,:EMAIL,:ADRESSE,:TELEPHONE,:file_encoded,:COMMERCIAL_INVOICE)';
// Prepare query
$query = $db->prepare($sql);
// Execute query
$query->execute(array(':submission_id' => $submission_id, ':formID' => $formID, ':NOM' => $NOM, ':EMAIL' => $EMAIL, ':ADRESSE' => $ADRESSE, ':TELEPHONE' => $TELEPHONE, ':file_encoded' => $file_encoded, ':COMMERCIAL_INVOICE' => $COMMERCIAL_INVOICE));
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
}
我不清楚你为什么使用 base64_decode
来读取第一个文件,因为没有证据表明它是 base64 编码的(它只是 print_r据我所知,关于某些 XML 的声明)。因此我也删除了它。