简体   繁体   English

生成PDF并通过电子邮件作为附件发送(PDF未打开)

[英]Generate PDF and sent it through Email as Attachment (PDF is not opening)

I am generating pdf and sent it through email as attachment the code is given below for this i skipped some code for generating pdf (not needed). 我正在生成pdf并通过电子邮件将其作为附件发送,下面给出了此代码,为此,我跳过了一些用于生成pdf的代码(不需要)。 Please check the code: 请检查代码:

//pdf generated not given avobe code of how it comes 
$pdfdoc = $pdf->Output("confirmation-".$client_code.".pdf", "S");

$attachment = chunk_split(base64_encode($pdfdoc));

$to = $mail_to;       

$from        = "From: <info@ksilbd.com>";

$subject     = "Here is your attachment";

$mainMessage = "BUY/SALE CONFIRMATION";

$fileatt     = $attachment;

$fileatttype = "application/pdf";

$fileattname = "confirmation-".$client_code.".pdf"; 

$headers = "From: $from";

$file = fopen($fileatt, 'rb');

$data = fread($file, filesize($fileatt));

fclose($file);


$semi_rand     = md5(time());

$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers      .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$message = "This is a multi-part message in MIME format.\n\n" .
"-{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$mainMessage  . "\n\n";
//base_64 
//encoding used
//to encode data
$data = chunk_split(base64_encode($data));

//message
//concat the 
//message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .

$data . "\n\n" .
"-{$mime_boundary}-\n";

 // Send the email
if(mail($to, $subject, $message, $headers)) {
    echo "EMAIL SENT SUCCESSFUL.";
}
else {
    echo "There was an error sending the mail.";

The message showed email sent successful and i got email too but i failed to open the attached pdf file with the mail .... please check the problem 该消息显示电子邮件发送成功,我也收到了电子邮件,但我无法用该邮件打开附件的pdf文件..请检查问题

打开PDF文件后

You may use "phpmailer". 您可以使用“ phpmailer”。 Download the PHPMailer script from here: http://github.com/PHPMailer/PHPMailer and Enjoy it. 从这里下载PHPMailer脚本: http : //github.com/PHPMailer/PHPMailer并享受它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM