I've been at this for a few days. I can't figure out what's wrong with the code. I'm trying to send a PDF attachment with an auto-responder email. The text/html email shows up... with no attachment.
Code: Select all
$to = $_SESSION['email'];
$subject = "Subject";
$random_hash = md5(date('r', time()));
$headers = "From: from@email.com";
$headers .= "\r\nReply-To: reply@email.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('attachment.pdf')));
ob_start(); //Turn on output buffering
?>
--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
text email
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<center>
<p>HTML email
</center>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/octet-stream; name="attachment.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
$emailMessage = ob_get_clean();
$mail_sent = @mail($to,$subject,$emailMessage,$headers);