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

时间:2015-11-18 03:17:48

标签: php email pdf attachment

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

etcdctl --version
etcdctl version 2.2.1

我的方法有错误吗?

2 个答案:

答案 0 :(得分:0)

在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!";

    }
}

答案 1 :(得分:0)

亲爱的约翰在线创建的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