Small Script Modification: Email with attachment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Small Script Modification: Email with attachment

Post by diseman »

I found this script for sending an attachment along with an e-mail. I've been hacking at it for a while now and can't seem to find a way to have it send more than 1 attachment. Would be grateful if someone could look at this and show me how to make it allow for THREE emails (PDFs only). This script works as is.

Code: Select all

$to = ''.$b_email.'';

$subject = 'Test email with attachment';

$random_hash = md5(date('r', time()));

$headers 		= "From: ".$webmaster."\r\nReply-To: ".$webmaster."\r\nbcc:" .$webmaster. "" ;

$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";

$attachment = chunk_split(base64_encode(file_get_contents('../PATH/TO/PDF/FILE.pdf')));  // THIS IS WHERE YOU PUT THE FILE PATH

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/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Email text goes here

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/pdf; name="Document_Requirements.pdf"    // THIS IS WHERE YOU PUT THE NAME OF THE FILE, BUT NOT THE PATH
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed"; 
TYIA, dM
Post Reply