Вообщем понадобилось отправлять письмо ввиде текста + прикрепленный файл в формате pdf.
Задача простая, но немного пришлось поискать.
1) html -> pdf
Как оказалось все просто, берем готовую библиотеку mPDF и скармливаем ей html и получаем на выходе pdf.
1 2 3 4 | require_once(mpdf.php' ); $pdf=new mPDF('cs','A4',10,'DejaVuSans'); $pdf->WriteHTML($html); $pdfdoc = $pdf->Output('', "S"); |
тут главное указать какой язык будет юзаться по у молчанию(в моем примере ‘cs’ — Чешский)
2) attache to email
для того чтоб прицепить файл к письму нужно передать в хедере:
1 2 3 4 | $headers .= "Content-Type: application/octet-stream;name=\"".$filename."\"".$eol; $headers .= "Content-Transfer-Encoding: base64".$eol; $headers .= "Content-Disposition: attachment".$eol.$eol; $headers .= $attachment.$eol.$eol; |
3) текст цепляем путем указания в хедере:
1 2 3 | $headers .= "Content-Type: text/html;charset=UTF-8;boundary=\"".$separator."\"".$eol.$eol; $headers .= $body.$eol.$eol; $headers .= "--".$separator; |
ну вообщем готовый велосипед:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | require_once( 'mpdf.php' ); $pdf=new mPDF('cs','A4',10,'DejaVuSans'); $pdf->WriteHTML($html); $pdfdoc = $pdf->Output('', "S"); $attachment = chunk_split(base64_encode($pdfdoc)); $to = "to@to.net"; $from = "from@from.net"; $subject = "subject"; $separator = md5(time()); $eol = PHP_EOL; $filename = "proforma.pdf"; $headers = "From: ".$from.$eol; $headers .= "MIME-Version: 1.0".$eol; $headers .= "Content-Type: multipart/mixed; charset=UTF-8;boundary=\"".$separator."\"".$eol.$eol; $headers .= "Content-Transfer-Encoding: 7bit".$eol; $headers .= "This is a MIME encoded message.".$eol.$eol; $headers .= "--".$separator.$eol; $headers .= "Content-Type: application/octet-stream;name=\"".$filename."\"".$eol; $headers .= "Content-Transfer-Encoding: base64".$eol; $headers .= "Content-Disposition: attachment".$eol.$eol; $headers .= $attachment.$eol.$eol; $headers .= "--".$separator; $headers .= "Content-Type: text/html;charset=UTF-8;boundary=\"".$separator."\"".$eol.$eol; $headers .= $body.$eol.$eol; $headers .= "--".$separator; $headers .= "--".$separator."--"; mail($to, $subject, $body, $headers); |