by Hiroshi on June 17th, 2008
What this file does is uploads file in ‘temp’ folder then emails that attachment with message to a predefined email address and finally removes that file from temp folder. The idea is to send email in rich format but with file attachment.
Functionality
- Determine File Type to be attached
- Uploads file in temp directory and deletes file after email with attachment has been sent
- Emails uploaded file as an attachment
Form Code
<form name="filepost" method="post" action="file.php" enctype="multipart/form-data" id="file">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr valign="bottom">
<td height="20">Your Name:</td>
</tr>
<tr>
<td><input name="from" type="text" id="from" size="30"></td>
</tr>
<tr valign="bottom">
<td height="20">Your Email Address:</td>
</tr>
<tr>
<td class="frmtxt2"><input name="emaila" type="text" id="emaila" size="30"></td>
</tr>
<tr>
<td height="20" valign="bottom">RFQ File:</td>
</tr>
<tr valign="bottom">
<td valign="bottom"><input name="filea" type="file" id="filea" size="16"></td>
</tr>
<tr>
<td height="40" valign="middle"><input name="Reset2" type="reset" id="Reset2" value="Reset">
<input name="Submit2" type="submit" value="Submit" onClick="return CheckData45()"></td>
</tr>
</table>
</form>PHP Code
<?php // request variables $from=$_REQUEST["from"]; $emaila=$_REQUEST["emaila"]; $filea=$_REQUEST["filea"]; if ($filea) { function mail_attachment ($from , $to, $subject, $message, $attachment){ $fileatt = $attachment; // Path to the file $fileatt_type = "application/octet-stream"; // File Type $start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1; $fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment $email_from = $from; // Who the email is from $subject = "New Attachment Message"; $email_subject = $subject; // The Subject of the email $email_txt = $message; // Message that the email has in it $email_to = $to; // Who the email is to $headers = "From: ".$email_from; $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); $msg_txt="\n\n You have recieved a new attachment message from $from"; $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; $email_txt .= $msg_txt; $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_txt . "\n\n"; $data = chunk_split(base64_encode($data)); $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; $ok = mail($email_to, $email_subject, $email_message, $headers); if($ok) { echo "File Sent Successfully."; unlink($attachment); // delete a file after attachment sent. } else { die("Sorry but the email could not be sent. Please go back and try again!"); } } move_uploaded_file($_FILES["filea"]["tmp_name"],'temp/'.basename($_FILES['filea']['name'])); mail_attachment("$from", "youremailaddress@gmail.com", "subject", "message", ("temp/".$_FILES["filea"]["name"])); } ?>
Precautions
- CHMOD ‘temp’ folder to 777 if it is at online server
- Use enctype=”multipart/form-data” in form tag
Upload And Send Email With Attachment Script Download
Email Attachment, html email, mime types, upload
Author
13 Reviews
Leave a Review
Incoming Searches
upload send attachment with php, php upload file email send, how to add the attachments to mail in using both php and html desinging, html codre to attach and email a file, php upload file email send attachment, php upload file in form and send to email, php upload format file name, php variable sent as file attachment, send attached mail with php, send email attachment from php code, send email file attachment php form, send html form to mail wordpress, php upload file attachment, php upload file attach to e-mail, php email script with image attachment, Php image upload send as attachment, php magic book upload and email, php mail attach uploaded file, php mail sender html attachments, php rtf to html upload, php script email attachment file, php script send form with attachment, php script upload file and submit to mail, php send attach file without upload, php upload file and attach to email, send mail php attach file, Send mail with attachment via HTML form, upload file with php and sending via email, Upload File – Send HTML Format Email with Attachment Using html, upload files in php mysql with message sent back to email, upload form send email html, upload form with email, wordpress upload file send by email, upload send email attachment in php, upload text and email php, upload to server and mail attachments in php, Uploading Files with PHP and sent via email, upload file with attachment, upload file php to webpage and send it to email, send temporary attachment php, sending attachment but message does not sent in php, sending attachments form html no database, sending file attachment via html form, sending rtf or html by php mail, upload an image html code to an email address, upload and send to email script, upload file and send mail in php, upload file into email html, upload file php sending data, uploading premade html newsletter using gmail, attach books in html, form mail attachment, form send images upload mail attachemnts, form to send attachment via email globals off, format email php dengan html, FORMAT HTML FORM, format table to send in email php, free code for uploading text files and get the files in attachment of mail using php, free email send file attachment script, free html code for email form file attachment, how do i send attachment file in php, form html attach file, Form attachments html, attach file php email, attaching a file to a form and sending via email, attachments from PHP email from HTML form, code for sending email with image attachment in php, email attachment in php from html form, email form php script with file attachment, email format attach, file attachment html, file upload & send to email html ?, file uploaded as email attachment, how to attach a file in an email form with php, how to attach a picture in html, html upload file send to email, image file upload script send email, mail form uploaded file php, multiple file attached in html, php and html mail books, php attach uploaded file into email, php class to send email with HTML, php codes for sending attachment, PHP Create message attach file send message, php email an uploaded file, html php send attachment, html php attachment, how to send upload file by email in php, how to upload a file and send it by email php, how to upload any file and send as email attachment in a html form using php, how to upload photo msg says wrong format, html attach file, html code for uploading files to email, html code for uploading images as e-mail attachment, html email message how to upload, html file attach code, html file attachment, php email attach uploaded file



Its working fine…Thank you very much:)
I intend to place here just original and working scripts. Definitely these all are working. Happy coding. PHP Rocks.
OK, the pre-tag doenst work anymore.
Can you please give example on how to add fields like Name, State, City, Comments?
Thanks in advance
Everything works great but the email filed is not submitting or rather showing up in the email. I get the email, shows the name they typed and the file is attached to the email but nowhere does it show the email address they typed? What am I doing wrong?
This works but the only info from the form that gets sent to the email is the name and the file.
How Do I get the inputted email address included in the message and how would I get extra fields I’ve added to the form to be included in the email message?
How do I put it into my premade html contact page? Can’t seem to get it to work.. I guess the script + form should go into the html-file and the then refer to the php-file.. Am I doing this wrong?
//Dupree
nice, but some more issues there…
Really good!
didn’t work for me, it ways that email can not be sent. Why is that? I change the CHMOD in the dir and file??
hi,
thanks a lot, nice script.
but if there is no attachment it doesn’t work for me.
I don’t get the email.
Why is that?
Help plz.
Thx
@Bentot: @Ryan: @Kev Neal: @dupreelove: @asda: @Victor: I am sorry for late reply. There was a little glitch.
request variables part was missing. You have to request variables named as from, emaila and filea in the php script. filea does not need to be request probably.
This was working for Globals ON and was not working for Globals off environment of PHP which was default. I have changed the code and downloadable file. Please check this again. Now it is working.
To add more fields e.g. phone number, company, comments etc. Just add these input fields in form and request these in PHP file and finally include these in email_message variable.
Latest version of this script is coming soon.
I have tryed the script, and i’m not able to let it work. Finally, i have download the one you have and only changed the email address to check if it works, I submit and it does nothing… I don’t know what can happend. I have test local and in a server…