Joko Wandiro
Joko Wandiro

Reputation: 1987

Email Problem while add attachment file

I have problem while i try to send email with attachment file, i just get message with blank attachment, how i can fix it. Here is my code:

$html2pdf = new HTML2PDF("P","A4", "fr");
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$to = "[email protected]"; 
$subject = "Voucher Mediskon";
$random_hash = md5(date("r", time()));
$headers = "From: Admin Mediskon<admin@" . $domain . ">\r\nReply-To: admin@" . $domain;
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode( $html2pdf->Output("", "S") ));
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Terimakasih telah membeli voucher kami. Mediskon</h2>
<p>Silakan <b>unduh ( download )</b> voucher pada <b>attachment</b> yang kami kirim.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/pdf; name="voucher.pdf" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php 
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );

Upvotes: 0

Views: 1561

Answers (2)

symcbean
symcbean

Reputation: 48357

i just get message with blank attachment

So the problem is not with the email, nor is with the process of attaching the file to the email.

Have you looked at the raw data in the message to see if the attachment is null length?

Have you tried writing the generated PDF to a file to check its contents?

Upvotes: 1

Treffynnon
Treffynnon

Reputation: 21553

Don't use PHP's mail function directly. It is insecure and inflexible. Plus you are reinventing the wheel. Here are two great email libraries that support HTML and file attachments:

Upvotes: 1

Related Questions