【问题标题】:Send email with auto / dynamically generated PDF as attachment in PHP在 PHP 中使用自动/动态生成的 PDF 作为附件发送电子邮件
【发布时间】:2015-11-18 03:17:48
【问题描述】:

我正在尝试发送一封电子邮件,其中包含自动/动态生成的 PDF 作为 PHP 中的附件。我正在尝试使用下面的代码来实现这一点,但它不起作用。

    <?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.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
$email_message.= "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($pdfdoc));
$email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" . "--{$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 .";
}
}

?>

我的方法有什么错误吗?

【问题讨论】:

  • 您是否注意到该示例代码中也存在语法错误。此外,在 die() 之后回显也没有任何好处。
  • 您能告诉我们您保存/写入 PDF 文件的代码吗?首先要检查的是文件是否创建成功。如果我们可以确认文件写入正确,那么问题就变成了电子邮件是否附加成功。

标签: php email pdf attachment


【解决方案1】:

试试这个代码在 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.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";         
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";

    $message = "--".$uid."\r\n"; 
    $message.= "Content-type:text/html; charset=iso-8859-1\r\n";
    $message .= "Content-type:image/jpeg; charset=iso-8859-1\r\n";
    $message  .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $message .= $message1."\r\n\r\n";
    $message  .= "--".$uid."\r\n";
    $message .= "Content-Type:  application/octet-stream; name=\"".$filename."\"\r\n";  
    $message .= "Content-Transfer-Encoding: base64\r\n";
    $message .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $message .= $content."\r\n\r\n";
    $message .= "--".$uid."--";

    if (mail($mailto, $subject, $message, $header)) {
        echo "your Mail sent.. " ;/
    }
    else 
    {
        echo "mail send ... ERROR!";

    }
}

【讨论】:

    【解决方案2】:

    亲爱的约翰,在线创建的 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.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
    $email_message.= "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
    $email_message .= "\n\n";
    $data = chunk_split(base64_encode($pdfdoc));
    $email_message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .$data . "\n\n" . "--{$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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 2014-06-21
      • 2014-03-24
      相关资源
      最近更新 更多