【问题标题】:how to attach mail in my output file using fpdf如何使用 fpdf 在我的输出文件中附加邮件
【发布时间】:2020-01-23 06:05:25
【问题描述】:
$path=$_SERVER['DOCUMENT_ROOT']."/unwant/test3.pdf";
$filename="test3.pdf";
$pdf->Output($filename,'F');

我在这段代码中得到了我的输出文件 $pdf->Output($filename,'F');我想在邮件中附加我的输出文件。

【问题讨论】:

    标签: php html fpdf


    【解决方案1】:
    require('lib/fpdf/fpdf.php');
    
    $pdf = new FPDF('P', 'pt', array(500,233));
    $pdf->AddFont('Georgiai','','georgiai.php');
    $pdf->AddPage();
    $pdf->Image('lib/fpdf/image.jpg',0,0,500);
    $pdf->SetFont('georgiai','',16);
    $pdf->Cell(40,10,'Hello World!');
    
    // email stuff (change data below)
    $to = "myemail@example.com"; 
    $from = "me@example.com"; 
    $subject = "send email with pdf attachment"; 
    $message = "<p>Please see the attachment.</p>";
    
    // a random hash will be necessary to send mixed content
    $separator = md5(time());
    
    // carriage return type (we use a PHP end of line constant)
    $eol = PHP_EOL;
    
    // attachment name
    $filename = "test.pdf";
    
    // encode data (puts attachment in proper format)
    $pdfdoc = $pdf->Output("", "S");
    $attachment = chunk_split(base64_encode($pdfdoc));
    
    // main header
    $headers  = "From: ".$from.$eol;
    $headers .= "MIME-Version: 1.0".$eol; 
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
    
    // no more headers after this, we start the body! //
    
    $body = "--".$separator.$eol;
    $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
    $body .= "This is a MIME encoded message.".$eol;
    
    // message
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $body .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $body .= $message.$eol;
    
    // attachment
    $body .= "--".$separator.$eol;
    $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
    $body .= "Content-Transfer-Encoding: base64".$eol;
    $body .= "Content-Disposition: attachment".$eol.$eol;
    $body .= $attachment.$eol;
    $body .= "--".$separator."--";
    
    // send message
    mail($to, $subject, $body, $headers);
    

    使用 PHPMailer:

    • 从这里下载 PHPMailer 脚本: http://github.com/PHPMailer/PHPMailer
    • 解压存档并将脚本文件夹复制到方便的 放置在您的项目中。
    • 包含主脚本文件 -- require_once('path/to/file/class.phpmailer.php');

    现在,发送带有附件的电子邮件变得异常简单:

    $attachment= $pdf->Output('attachment.pdf', 'S');
    
    $mailer->AddStringAttachment($attachment, 'attachment.pdf');
    

    【讨论】:

    • 嗨@rajat.gite 我需要我的代码帮助来更改你的代码我只是尝试它但它不起作用请澄清我的代码$path=$_SERVER['DOCUMENT_ROOT']."/unwant/test3.pdf"; $pdf-&gt;Output($path,'F');
    • @khairuddeen 你有哪个错误?如果你去不想要的/*不存在的文件?
    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多