Jump to content

email attachment in php?


jc624

Recommended Posts

Anyone know how to add an pdf attacment on a php email? the email functions well and getting it in outlook:

<?php$to = "me@me.com";$subj = "Test mail";$msg = "hello?";$from = "From: meto@me.com";$headers = "From: $from";ini_set("SMTP", "server.com");ini_set("smtp_port", 25);ini_set("sendmail_from","meto@me.com");mail($to, $subj, $msg, $from, $headers); echo "Mail Sent.";?>
Link to comment
Share on other sites

you seem to have all the answers jsg...yeah I google it and they all say to try doing it with MIME...I tried this::
$data	= file_get_contents("yo.jpg");$eol	= "\n";// Boundary$boundary = uniqid("me");// Basic Headers$headers  = 'From: $from'.$eol;$headers .= 'MIME-Version: 1.0'.$eol;$headers .= 'Content-Type: Multipart/Related; boundary='.$boundary.$eol;$headers .= $eol;// Text for non-MIME users$headers .= "This part of the E-mail should never be seen. If you are reading this, consider upgrading your e-mail client to a MIME-compatible client.".$eol;$headers .= $eol;// HTML$headers .= "--".$boundary.$eol;	$headers .= 'Content-Type: text/html; charset=ISO-8859-1'.$eol;$headers .= 'Content-Transfer-Encoding: base64'.$eol;$headers .= $eol;// Image$headers .= "--".$boundary.$eol;$headers .= 'Content-ID: <123456789>'.$eol;	$headers .= 'Content-Type: image/jpeg'.$eol;$headers .= 'Content-Transfer-Encoding: base64'.$eol;$headers .= $eol;$headers .= chunk_split(base64_encode($data));// EOF$headers .= "--".$boundary."--".$eol;

I still get the email with no errors at all but still get no attachments maybe I need the absolute path...I'll keep trying....always something.

Link to comment
Share on other sites

I figure it out!

<?php$to = 'you@you.com';$subject = 'Test email with attachment';$random_hash = md5(date('r', time()));$headers = "From: me@me.com\r\nReply-To: me@me.com";$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";$attachment = chunk_split(base64_encode(file_get_contents('my.pdf')));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: 7bitmy text--PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1"Content-Transfer-Encoding: 7bit<p>html</p>--PHP-alt-<?php echo $random_hash; ?>----PHP-mixed-<?php echo $random_hash; ?> Content-Type: application/pdf; name="my.pdf" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?>--PHP-mixed-<?php echo $random_hash; ?>--<?php$message = ob_get_clean();ini_set("SMTP", "server.com");ini_set("smtp_port", 25);ini_set("sendmail_from","me@me.com");$mail_sent = @mail( $to, $subject, $message, $headers ); }echo $mail_sent ? "sent to: $to" : "Mail failed";?>
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...