PHP Magic Book – Free PHP Scripts, Tutorials and Downloads, PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips

Upload File – Send HTML Format Email with Attachment Using PHP

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
Tags: , , ,
Posted in: Email Systems, File Upload, Scripts
Post's RSS » RSS 2.0
Post's Comments RSS » RSS 2.0

Related Posts




13 Responses to “Upload File – Send HTML Format Email with Attachment Using PHP”

  1. Aug 6, 2008
    Programmer Says:

    Its working fine…Thank you very much:)

  2. Aug 7, 2008
    King Says:

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

  3. Apr 8, 2009
    Marco Says:

    OK, the pre-tag doenst work anymore.

  4. Jun 23, 2009
    Bentot Says:

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

    Thanks in advance

  5. Jul 7, 2009
    Ryan Says:

    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?

  6. Jul 9, 2009
    Kev Neal Says:

    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?

  7. Jul 23, 2009
    dupreelove Says:

    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

  8. Aug 6, 2009
    asda Says:

    nice, but some more issues there…

  9. Aug 6, 2009
    asda Says:

    Really good!

  10. Sep 22, 2009
    Victor Says:

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

  11. Sep 28, 2009
    dudarobi Says:

    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

  12. Sep 29, 2009
    King Says:

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

  13. Nov 27, 2009
    Diego Santamarta Says:

    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…

Post a Comment

  Subscribe Via RSS
  Subscribe Via Email