我最近创建了一个在线模板,用于为我们的网站创建招聘信息。一切都已完成,它在浏览器中正确格式化,自动发布到我们的网站,bla bla bla。
我正在创建的最后一篇文章是为管理员提供一些选项,以便以一致,方便的方式将发布内容分发到各个地方(通过电子邮件)。我已经创建了一个PHP页面,可以使用TCPDF库动态创建PDF文档。当加载pdf.php?id = X时,页面显示一个PDF,其中包含作业发布X的内容。这意味着我永远不会将PDF文件保存到服务器,只需在每次调用时即时创建它。
但我想将此PDF附加到电子邮件中,然后将其发送到各个大学,内部邮件列表等。如果我将pdf.php?id = x附加到电子邮件中,则不附加PDF ,它附上一个看似空白的文件,上面有上面的名字。
是否可以将其附加到电子邮件而不将其保存到服务器?
下面根据JM4对进一步故障排除的响应进行了补充。我已将PDF文件创建放入一个函数中,并将其放入一个包含文件中,以便更容易管理。
// random hash necessary to send mixed content
$separator = md5(time());
$eol = PHP_EOL;
// attachment name
$filename = "_Desiredfilename.pdf";
include_once('pdf.php');
// encode data (puts attachment in proper format)
$pdfdoc = job_posting_to_pdf($posting_id);
$attachment = chunk_split(base64_encode($pdfdoc));
///////////HEADERS INFORMATION////////////
// main header (multipart mandatory) message
$headers = "From: Sender_Name<valid_email@mydomain.com>".$eol;
//$headers .= "Bcc: email@domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
//Email message
if(mail('valid_email@mydomain.com', 'test job posting', 'message body goes here', $headers)) {
echo 'mail sent';
} else {
echo 'error in email';
}
以下是pdf.php的精简版:
function job_posting_to_pdf($job_id) {
require_once(ROOT . 'assets/libs/tcpdf/config/lang/eng.php');
require_once(ROOT . 'assets/libs/tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('');
$pdf->SetTitle('OPL Job Posting');
$pdf->SetSubject('Job Posting');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(11, PDF_MARGIN_TOP, 11);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
$pdf->SetFont('times', 'I', 9);
$pdf->AddPage();
$left_cell_width = 60;
$row_height = 6;
$pdf->Image(ROOT . 'assets/gfx/logos/OPL-Logo.jpg', 0, 5, null, 16, null, null, 'N', false, null,'R');
$pdf->Ln('3');
if(!$row['internal']) {
$pdf->Cell(0,0,'This position will be posted internally and externally, concurrently.',0,2,'C');
} else {
$pdf->Cell(0,0,'Internal posting only.',0,2,'C');
}
//Remainder of actual PDF creation removed to keep things simple
return $pdf->Output("", "S");
}
答案 0 :(得分:5)
如果我完全明白你的要求,这很简单。我假设您已经使用fdpf或tcpdf之类的东西生成了PDF。在这种情况下 - 只需使用以下代码:
<?php
// random hash necessary to send mixed content
$separator = md5(time());
$eol = PHP_EOL;
// attachment name
$filename = "_Desiredfilename.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
///////////HEADERS INFORMATION////////////
// main header (multipart mandatory) message
$headers = "From: Sender_Name<sender@domain.com>".$eol;
$headers .= "Bcc: email@domain.com".$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
$headers .= "Content-Transfer-Encoding: 7bit".$eol;
$headers .= "This is a MIME encoded message.".$eol.$eol;
// message
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$headers .= $message.$eol.$eol;
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= "Content-Disposition: attachment".$eol.$eol;
$headers .= $attachment.$eol.$eol;
$headers .= "--".$separator."--";
//Email message
mail($emailto, $emailsubject, $emailbody, $headers);
?>
答案 1 :(得分:1)
我只需要弄清楚这一点,我的眼球肯定会在最后疼痛......
1)您需要将PHPMailer安装到php服务器。
2)在TCPDF脚本中包含PHPmailer类,如下所示(您的路径可能会有所不同):
require_once('../PHPMailer_v5.1/class.phpmailer.php');
3)现在你的pdf代码之后只需和PHPMailer交流:
$filename = "custompdf_$name_$time.pdf";
$pdf->Output($filename, 'F'); // save the pdf under filename
$mail = new PHPMailer(); $mail->IsSMTP();
$mail->Host = "mail.yourhost.com";
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "user+yourhost.com"; // SMTP account username
$mail->Password = "topsecret"; // SMTP account password
$mail->From = "noreply@yourhost.com";
$mail->FromName = "Stack Overflower";
$mail->AddAddress( $email, $name ); // in this case the variable has been passed
$mail->AddCC( "person@somehost.net", "Johnny Person"); // in this case we just hard code it
$mail->SMTPDebug = 0; // use 2 for debugging the email send
$pdf_content = file_get_contents($filename);
$mail->WordWrap = 50;
$mail->AddStringAttachment($pdf_content, "custompdf_for_$name_$time.pdf", "base64", "application/pdf"); // note second item is name of emailed pdf
$mail->IsHTML(true);
$mail->Subject = "Your pdf is here";
$mail->Body = "Dear $name,<br>
Here is your custom generated pdf generated at $t.<br><br>
Thank you";
if(!$mail->Send()) {
echo "Sorry ... EMAIL FAILED";
} else { echo "Done. . . Email sent to $email at $t."; }
unlink($filename); // this will delete the file off of server
当然,您发送的电子邮件有很多选项,例如不使用HTML,或发送HTML和文本,添加许多收件人和/或cc等。
编辑:这会将文件暂时保存在服务器上,但它会使用unlink命令自行清理。
答案 2 :(得分:0)
看看这个讨论PHP高级电子邮件的页面。
http://articles.sitepoint.com/article/advanced-email-php/5
他们获取上传的文件并将二进制数据加载到$ data中,但您可以从那里开始。
答案 3 :(得分:0)
您可能还希望通过PEAR Mail_Mime查看将其作为附件发送。它可以接受附件作为一串数据。
RMail包看起来好像会通过stringAttachment类做同样的事情。你必须谷歌,因为我是一个新用户,所以我一次只能发布一个链接。
答案 4 :(得分:0)
我同意使用邮件库代替使用默认的mail()函数手动构建mime消息。 SwiftMailer是另一个很好的开源PHP邮件库。以下是sample code将动态内容用作附件而无需先将其保存到文件系统。
答案 5 :(得分:0)
你的标题似乎有点出局:
application/octet-stream
应该成为application/octetstream
Content-Disposition: attachment ..
应该成为Content-Disposition: attachment; filename="' . basename($filename) . '"
下面是attachement标题应该是什么样的:
// attachment
$headers .= "--".$separator.$eol;
$headers .= "Content-Type: application/octetstream;".$eol; //Fixed
$headers .= "Content-Transfer-Encoding: base64".$eol;
$headers .= 'Content-Disposition: attachment; filename="' . basename(filename).'"'.$eol;
$headers .= 'Content-ID: <' . basename($filename) . '>' . $eol . $eol //EOL X2 Before
$headers .= $attachment;
//Run the above in a loop for multiple attachments, after add the final line
$headers .= '--' . $separator . '--' . $eol;
这是从我的一个工作应用程序中获取的,如果您希望看到它,请继续循环:
foreach ($this->attachments as $attachment) {
if (file_exists($attachment['file'])) {
$handle = fopen($attachment['file'], 'r');
$content = fread($handle, filesize($attachment['file']));
fclose($handle);
$message .= '--' . $boundary . $this->newline;
$message .= 'Content-Type: application/octetstream' . $this->newline;
$message .= 'Content-Transfer-Encoding: base64' . $this->newline;
$message .= 'Content-Disposition: attachment; filename="' . basename($attachment['filename']) . '"' . $this->newline;
$message .= 'Content-ID: <' . basename($attachment['filename']) . '>' . $this->newline . $this->newline;
$message .= chunk_split(base64_encode($content));
}
}
$message .= '--' . $boundary . '--' . $this->newline;