Hi,

I am uploading a file(text,doc) in php.its working fine.
I want to send this uploaded file as attached file to admin.my mail function is working fine.but i don,t know how to attach that uploaded file.
need help.

Thanks

Recommended Answers

All 3 Replies

Hi Jacob, the easiest way would be to use some mailing library like XPertMailer http://www.xpertmailer.com/. It would take care of all the boring stuff related to sending attachments or html e-mails or validating e-mail addresses, or even protecting yourself from e-mail injection attack.

If you however, insist on implementing it yourself, here's a good tutorial:
http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php
It's the #1 hit in Google when you search for "send attachment php".

Petr

Can anyone have code to send attached file in php.
Thanks

If you can't click the link provided and scroll down, here's the code it gives on emailing attachments(stripped of comments):

<?php
$to = 'youraddress@example.com';
$subject = 'Test email with attachment';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
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

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
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.