用PHP自动/动态生成的PDF作为附件发送电子邮件



我正试图用PHP发送一封带有自动/动态生成的PDF附件的电子邮件。我正试图用下面的代码来实现这一点,但它不起作用。

    <?php
require('fpdf.php');
class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo
        //$this->Image('logo.jpg',10,6,30);
        // Arial bold 15
        $this->SetFont('Arial','B',15);
        // Move to the right
        $this->Cell(80);
        // Title
        $this->Cell(60,10,'Convert HTML TO PDF',1,0,'C');
        // Line break
        $this->Ln(20);
    }
    // Page footer
    function Footer()
    {
        // Position at 1.5 cm from bottom
        $this->SetY(-15);
        // Arial italic 8
        $this->SetFont('Arial','I',8);
        // Page number
        $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    }
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->Cell(0,10,'Name : ',0,1);
$pdf->Cell(0,10,'Email : ',0,1);
$pdf->Cell(0,10,'Mobile : ',0,1);
$pdf->Cell(0,10,'Comment : ',0,1);
$pdf->Output("filename.pdf","F");
$pdfdoc = $pdf->Output("", "S");
//$attachment = chunk_split(base64_encode($pdfdoc));
$email_to      = "xxx@gmail.com"; // The email you are sending to (example)
$email_from    = "xxx@outlook.com"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt     = "text body of message"; // Message that the email has in it
$fileatt_type  = "application/pdf"; // File Type
$fileatt_name  = "filename.pdf"; // Filename that will be used for the file as the attachment
$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers       = "From: NameHere"; // Who the email is from (example)
$headers      .= "nMIME-Version: 1.0n" ."Content-Type: multipart/mixed;n" ." boundary="{$mime_boundary}"";
$email_message.= "This is a multi-part message in MIME format.nn" ."--{$mime_boundary}n" ."Content-Type:text/html; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" . $email_txt;
$email_message .= "nn";
$data = chunk_split(base64_encode($pdfdoc));
$email_message .= "--{$mime_boundary}n" ."Content-Type: {$fileatt_type};n" ." name="{$fileatt_name}"n" ."Content-Transfer-Encoding: base64nn" .$data . "nn" . "--{$mime_boundary}--n";
;
if(mail($email_to,$email_subject,$email_message,$headers))
{
    echo "File Sent Successfully.";
    //unlink($attachment); // delete a file after attachment sent.
}
else
{
    die("Sorry but the email could not be sent. Please go back and try again!");
    echo "File Not Sent .";
}
}
?>

我的方法有错误吗?

在PHP中尝试此代码作为附件

//send mail .....Function calling
mail_attachment($pdf_filename,$path, $email_to,$from_mail ,$from_name, "$replyto", $email_subject ,$msg);
//delete Generated PDF  File  
unlink($file_nm); 
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message1) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $typ=explode(".",$filename);
    $get_type=end($typ);
    $header = "From: ".$from_name." <".$from_mail.">rn";
    $header .= "Reply-To: ".$replyto."rn";
    $header .= "MIME-Version: 1.0rn";         
    $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
    $message = "--".$uid."rn"; 
    $message.= "Content-type:text/html; charset=iso-8859-1rn";
    $message .= "Content-type:image/jpeg; charset=iso-8859-1rn";
    $message  .= "Content-Transfer-Encoding: 7bitrnrn";
    $message .= $message1."rnrn";
    $message  .= "--".$uid."rn";
    $message .= "Content-Type:  application/octet-stream; name="".$filename.""rn";  
    $message .= "Content-Transfer-Encoding: base64rn";
    $message .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
    $message .= $content."rnrn";
    $message .= "--".$uid."--";
    if (mail($mailto, $subject, $message, $header)) {
        echo "your Mail sent.. " ;/
    }
    else 
    {
        echo "mail send ... ERROR!";
    }
}

亲爱的John,pdf是在线创建的,我对它进行了测试,但当我试图发送它时,发送时出现了错误。我认为错误的代码是:

$pdfdoc = $pdf->Output("", "S");
//$attachment = chunk_split(base64_encode($pdfdoc));
$email_to      = "xxx@gmail.com"; // The email you are sending to (example)
$email_from    = "xxx@outlook.com"; // The email you are sending from (example)
$email_subject = "subject line"; // The Subject of the email
$email_txt     = "text body of message"; // Message that the email has in it
$fileatt_type  = "application/pdf"; // File Type
$fileatt_name  = "filename.pdf"; // Filename that will be used for the file as the attachment
$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers       = "From: NameHere"; // Who the email is from (example)
$headers      .= "nMIME-Version: 1.0n" ."Content-Type: multipart/mixed;n" ." boundary="{$mime_boundary}"";
$email_message.= "This is a multi-part message in MIME format.nn" ."--{$mime_boundary}n" ."Content-Type:text/html; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" . $email_txt;
$email_message .= "nn";
$data = chunk_split(base64_encode($pdfdoc));
$email_message .= "--{$mime_boundary}n" ."Content-Type: {$fileatt_type};n" ." name="{$fileatt_name}"n" ."Content-Transfer-Encoding: base64nn" .$data . "nn" . "--{$mime_boundary}--n";
;
if(mail($email_to,$email_subject,$email_message,$headers))
{
    echo "File Sent Successfully.";
    //unlink($attachment); // delete a file after attachment sent.
}
else

相关内容

  • 没有找到相关文章

最新更新