Welcome to Dream.In.Code
Become a PHP Expert!

Join 149,853 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,354 people online right now. Registration is fast and FREE... Join Now!




Php mail() attachment!

 
Reply to this topicStart new topic

Php mail() attachment!

eXceed69
20 Feb, 2007 - 09:24 PM
Post #1

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
CODE
$to = "you@me.com";
$subject = "Pogi mo Ronald!";
$message = "Wow, this is mail, HA";
$from = "alversado@my_service";
$headers = "From: $from";
mail($to,$subject,$message,$headers);  


How can I attach A file to that email (Simply...) ? I am talking about a PDF file??or any file
User is offlineProfile CardPM
+Quote Post

snoj
RE: Php Mail() Attachment!
20 Feb, 2007 - 09:52 PM
Post #2

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
I can't remember how to do it off the top of my head, but you're looking for "multipart" emails.
User is offlineProfile CardPM
+Quote Post

eXceed69
RE: Php Mail() Attachment!
20 Feb, 2007 - 09:56 PM
Post #3

"Super Sentai Knight Of DawN"
Group Icon

Joined: 12 Nov, 2006
Posts: 682



Thanked: 1 times
Dream Kudos: 675
My Contributions
Just attach file/s in a email
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Php Mail() Attachment!
20 Feb, 2007 - 09:58 PM
Post #4

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,267



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
CODE
<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
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

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

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

--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/zip; name="attachment.zip"
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";
?>


Code from http://www.webcheatsheet.com/php/send_emai..._attachment.php
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:27AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month