简体   繁体   中英

Mail attachment pdf file

I'm using html2pdf to generate a pdf file, and I will send it by mail attachement.

1- I've created a pdf file: works fine

2- I've saved it: works fine.

3- I've attached it for send mail but it doesn't work: I've received a mail with pdf attached but impossible to open it! (filesize < normal filesize). And when I resend a mail again, works fine !

Do you have any suggestions please?

My php code:

$filename='facture.pdf';
$mail_to = $email;
$subject = "Facture";
$random_hash = md5(time());
$headers = "From:" .$mailnotif." \r\nReply-To: mondmain.fr";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$path = 'http://mondmain.fr/Factures/'.$id.'/'.$filename.'';
$attachment = @chunk_split(base64_encode(file_get_contents($path)));
$message = "--PHP-mixed-$random_hash\r\n"
."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n"
."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the plain text message.
$message .= strip_tags($subject);
$message .= "\r\n\r\n--PHP-alt-$random_hash\r\n"
."Content-Type: text/html; charset=\"utf-8\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= 'Bonjour,
Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr.'
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"
."Content-Type: application/doc; name=\"$filename\"\r\n"
."Content-Type: application/pdf; name=\"$filename\"\r\n"
."Content-Type: application/docx; name=\"$filename\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment\r\n\r\n";

$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
mail( $mail_to, $subject , $message, $headers );

(Tested and attachment received on a SINGLE run)

You had some single quotes which should have been double quotes and did not close the text body with a semi-colon.

$message .= 'Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr.'

Has been changed to: (with closing semi-colon)

$message .= "Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain.fr";

If what I posted below doesn't work, try replacing this line:

$attachment = @chunk_split(base64_encode(file_get_contents($path)));

with:

$attachment = @chunk_split(base64_encode(file_get_contents($filename)));

which is what I used to test it with, since I do not have the same setup as you do, using an $id variable.

Rewrite: (Successfully tested with one of my PDF files)

<?php

$filename='facture.pdf';
$mail_to = $email;
$subject = "Facture";
$random_hash = md5(time());
$headers = "From:" .$mailnotif." \r\nReply-To: mondmain.fr";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$path = 'http://mondmain.fr/Factures/'.$id.'/'.$filename.'';

$attachment = @chunk_split(base64_encode(file_get_contents($path)));
$message = "--PHP-mixed-$random_hash\r\n" ."Content-Type: multipart/alternative; boundary=\"PHP-alt-$random_hash\"\r\n\r\n";
$message .= "--PHP-alt-$random_hash\r\n" ."Content-Type: text/plain; charset=\"iso-8859-1\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the plain text message.
$message .= strip_tags($subject);
$message .= "\r\n\r\n--PHP-alt-$random_hash\r\n"
."Content-Type: text/html; charset=\"utf-8\"\r\n" ."Content-Transfer-Encoding: 7bit\r\n\r\n";

//Insert the html message.
$message .= "Bonjour, Veuillez trouver ci-joint la facture correspondant à votre abonnement sur mondmain";
$message .="\r\n\r\n--PHP-alt-$random_hash--\r\n\r\n";

//include attachment
$message .= "--PHP-mixed-$random_hash\r\n"
."Content-Type: application/doc; name=\"$filename\"\r\n"
."Content-Type: application/pdf; name=\"$filename\"\r\n"
."Content-Type: application/docx; name=\"$filename\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment\r\n\r\n";

$message .= $attachment;
$message .= "/r/n--PHP-mixed-$random_hash--";

//send the email
mail( $mail_to, $subject , $message, $headers );
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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