• Home
  • About
  • Privacy Policy
  • Contact
PHP Magic Book – Free PHP Scripts, Tutorials and Downloads, PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips
  • All in One
  • Cheat Sheets
  • Reference
  • PHP Web Hosting
http://www.phpmagicbook.com/upload-file-attach-send-html-format-email/

Upload File – Send HTML Format Email with Attachment Using PHP

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

  • Share/Bookmark

Email Attachment, html email, mime types, upload

Google BuzzTwitterRedditFacebookStumbleuponDiggDel.icio.us

Author

Hiroshi (published 173 Posts on PHP Magic Book – Free PHP Scripts, Tutorials and Downloads)


13 Reviews

  1. Programmer says:
    August 6, 2008 at 5:05 pm

    Its working fine…Thank you very much:)

    Reply
  2. King says:
    August 7, 2008 at 2:26 am

    I intend to place here just original and working scripts. Definitely these all are working. Happy coding. PHP Rocks.

    Reply
  3. Marco says:
    April 8, 2009 at 8:57 pm

    OK, the pre-tag doenst work anymore.

    Reply
  4. Bentot says:
    June 23, 2009 at 11:15 pm

    Can you please give example on how to add fields like Name, State, City, Comments?

    Thanks in advance

    Reply
  5. Ryan says:
    July 7, 2009 at 11:16 pm

    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?

    Reply
  6. Kev Neal says:
    July 9, 2009 at 7:55 pm

    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?

    Reply
  7. dupreelove says:
    July 23, 2009 at 2:11 am

    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

    Reply
  8. asda says:
    August 6, 2009 at 9:42 pm

    nice, but some more issues there…

    Reply
  9. asda says:
    August 6, 2009 at 9:42 pm

    Really good!

    Reply
  10. Victor says:
    September 22, 2009 at 1:38 am

    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??

    Reply
  11. dudarobi says:
    September 28, 2009 at 11:40 pm

    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

    Reply
  12. King says:
    September 29, 2009 at 7:44 pm

    @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.

    Reply
  13. Diego Santamarta says:
    November 27, 2009 at 9:45 pm

    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…

    Reply

Leave a Review

Click here to cancel reply.

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
Join / Follow phpMagicBook
follow phpMagicBook at FaceBook Subscribe RSS Feeds Receive updates in Email
Submit your Email to become PHP Guru
Learn to create php mass email, php help desk software, php sql backup, php email marketing software, php bug tracking software, php project management system, php web content management system, draft php membership software, php intranet software, php ecommerce software, php email manager, php helpdesk system, php ticketing system, php survey software, php content management software, php email marketing and achieve mastery in php web programming with phpMAGICbook.

Fresh Posts

  • Free Social CMS like Digg, Mixx, Reddit
  • PHPMagicBook Reloaded
  • Change Your Blog or Forum Theme Color Scheme Online Live
  • Change Prosilver Header and Logo in PHPBB – Working Solution
  • Htaccess Mod Rewrite Practical Essential Important Solutions – Update
  • Protect Hotlink the Right Way Using htaccess
  • Hide Ads from Admin at Blog
  • Change Default WordPress Gravatar in Comments
  • Only Allow Your IP to Access Website or Blog Admin
  • Hide WordPress WP-Admin Login Page
  • Enable Gravatar/Avatar at Your Website – Gravatar Image PHP Script
  • Allow htaccess and Mod Rewrite in Wamp
  • PHP Script Not Working – Solution
  • Customizing WordPress Tags Cloud – A Guide
  • .htaccess Essential Tricks For Blog/Website Performance – Guide

Sections

  • Ajax & PHP
  • Arrays
  • Capitalization
  • Captcha
  • Checks
  • CMS
  • Comment
  • Concatenation
  • Conditions
  • connection
  • Content Management
  • Cookies
  • Data Types
  • database
  • Date & Time
  • Decrement
  • Directory
  • Do Loop
  • Do While Loop
  • Downloads
  • Echo
  • Else
  • Elseif
  • Email Attachment
  • Email Systems
  • Explode
  • File
  • File Manipulation
  • File Upload
  • Flash & PHP
  • For Each Loop
  • For Loop
  • Form
  • Form Validation
  • Forums
  • Function Exists
  • General
  • Greeting Cards II
  • Header
  • Hit Counter II
  • htaccess
  • if
  • Image Galleries
  • Image Handling
  • Images Slide Show
  • Implode
  • Include
  • Increment
  • Info
  • Input
  • IP Address
  • isset
  • Last Updated
  • Libraries
  • Loops
  • Mail
  • Miscellaneous
  • Mod Rewrite
  • MySQL
  • No Database
    • CMS II
    • Guestbook II
    • Image Galleries II
    • Images Slide Show II
    • Login System II
    • Poll & Vote II
  • Open Source
  • Operators
  • PHP Basics
  • PHP Functions
  • php mySQL
  • PHP Search
  • Post & Get
  • Prefix
  • Print
  • Quiz
  • Rand
  • Randomizing
  • Redirection
  • Referrer
  • Require
  • Rotating Content
  • RSS Parsing
  • Scripts
  • Security
  • Select
  • servers
  • Sessions
  • Softwares
  • Spam Control
  • Statistics
  • String Case
  • Strings
  • substr
  • Switch
  • Thumbnail
  • Tricks
  • URL
  • Useful Scripts
  • User Authentication
  • Variable
  • Variable Scope
  • While Loop
  • WordPress
    • Hacks

Must Visit

  • Code Igniter
  • Hafeez Centre
  • PHP Classes
  • PHP IDS
  • PHP MySQL Tutorials
  • PHP Security
  • PHP.net
  • RecipeApart
  • Shahinshah
  • Smashing Solution
  • TechMynd
  • Ultimate Remedy
  • Web Development / Designing
  • WebStylePress
  • Zend
download PMB
Download php photo album
Copyright © 2010 phpMagicBook PHP A to Z - All Rights Reserved. RSS - Comments RSS - RSS 0.92 Feed - Atom Feed - RDF/RSS 1.0 Feed - WordPress Theme Design by TechMynd