WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search


Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder



Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : send php mail with form data and attachment.
Categories : PHP, Email, Mail, Form Processing Click here to Update Your Picture
Selvakumar Thangavelu
Date : Mar 07th 2007
Grade : 3 of 5 (graded 20 times)
Viewed : 140128
File : No file for this code example.
Images : Image 1
Search : More code by Selvakumar Thangavelu
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
Like this code?
Show the author your appreciation.
 

send php mail with submitd form data and file attachment. Copy and paste these codes and change from and to mail address needed. Enjoy!!!!!!

careers.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>Untitled Document</title>
</head>

<body style="padding:3px; margin:0px;" bgcolor="#FFFFFF">
<table cellpadding="0" cellspacing="0" border="0" width="440">
   
    <tr><td style="height:10px"></td></tr>
    <tr>
      <td colspan="2" style="text-align:justify; line-height:15px;" class="body">
       
      <form name="frm" method="POST" action="careersuccess.php" enctype="multipart/form-data">
      <table cellpadding="0" cellspacing="0" border="0" width="100%">
          <tr>
            <td width="23%" class="body"> Name</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strname" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Address</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><textarea cols="16" name="straddress"></textarea></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> City</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strcity" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> State</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strstate" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Contact No</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="strno" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Email</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="text" name="stremail" class="textfield"></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Comments</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><textarea cols="16" name="strcomments"></textarea></td>
        </tr>
        <tr><td style="height:3px"></td></tr>
        <tr>
            <td width="23%" class="body"> Resume</td>
            <td width="3%" class="body">:</td>
            <td width="74%"><input type="file" name="strresume"></td>
        </tr>
        <tr><td style="height:10px"></td></tr>
        <tr>
            <td colspan="3" align="center">
                <input type="submit" value="Send" name="submit" onClick="return validate();"> <input type="reset" value="Reset" name="reset">
            </td>
        </tr>
       
      </table>   
     </form>

</td>
    </tr>
    <tr>
      <td colspan="2" align="center"> </td>
  </tr>
    </table>
</body>
</html>



careersuccess.php
<?php
     
                     
       $strname
=ucfirst($_REQUEST["strname"]);
   
$straddress=ucfirst($_REQUEST["straddress"]);
   
$strcity=ucfirst($_REQUEST["strcity"]);
   
$strstate=ucfirst($_REQUEST["strstate"]);       
       
       
$phone=$_REQUEST["strno"];
       if(
$phone != ""){ $strno=$phone; } else { $strno="-"; }   
       
   
$stremail=$_REQUEST["stremail"];   
   
$strcomments=ucfirst($_REQUEST["strcomments"]);

   
   
$filename=$_FILES["strresume"]["name"];
   
$filetype=$_FILES["strresume"]["type"];
   
$filesize=$_FILES["strresume"]["size"];
   
$filetemp=$_FILES["strresume"]["tmp_name"];


       
    if(
$filetype=="application/octet-stream" or $filetype=="text/plain" or $filetype=="application/msword")
    {
   
       
$message= '
   
   
            <table cellspacing="0" cellpadding="8" border="0" width="400">
            <tr>
                <td colspan="2"></td>
            </tr>
            <tr bgcolor="#eeeeee">
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td>
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strname.'</td>
            </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$straddress.'</td>
              </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>City</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strcity.'</td>
              </tr>
              <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>State</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strstate.'</td>
              </tr>
              <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Contact No.</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strno.'</td>
              </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Email</strong></td>
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$stremail.'</td>
            </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
           
            <tr bgcolor="#eeeeee">
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Comments</strong></td>
            </tr>               
            <tr bgcolor="#eeeeee">
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strcomments.'</td>
            </tr>               
                                   
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
         </table>


           

'
;

   
// MAIL SUBJECT

   
$subject = "Mail with doc file attachment";
   
   
// TO MAIL ADDRESS
   
   
   
$to="[email protected]";

/*
    // MAIL HEADERS
                       
    $headers  = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $headers .= "From: Name <[email protected]>\n";

*/
 


    // MAIL HEADERS with attachment

   
$fp = fopen($strresume, "rb");
   
$file = fread($fp, $strresume_size);

   
$file = chunk_split(base64_encode($file));
   
$num = md5(time());
   
       
//Normal headers

   
$headers  = "From: Name<[email protected]>\r\n";
       
$headers  .= "MIME-Version: 1.0\r\n";
       
$headers  .= "Content-Type: multipart/mixed; ";
       
$headers  .= "boundary=".$num."\r\n";
       
$headers  .= "--$num\r\n";

       
// This two steps to help avoid spam   

   
$headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
   
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

       
// With message
       
   
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
       
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
       
$headers .= "".$message."\n";
       
$headers .= "--".$num."\n"

       
// Attachment headers

   
$headers  .= "Content-Type:".$strresume_type." ";
       
$headers  .= "name=\"".$strresume_name."\"r\n";
       
$headers  .= "Content-Transfer-Encoding: base64\r\n";
       
$headers  .= "Content-Disposition: attachment; ";
       
$headers  .= "filename=\"".$strresume_name."\"\r\n\n";
       
$headers  .= "".$file."\r\n";
       
$headers  .= "--".$num."--";

   
   
   
// SEND MAIL
       
       
@mail($to, $subject, $message, $headers);
   

     
fclose($fp);

    echo
'<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>';
}
else
    {
        echo
'<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>';
       
//echo "<script>window.location.href='careers.html';</script>";
   
}

?>



Customer feedback or simple emailer - A PHP script that enables your visitors to send you emails.
Categories : PHP, Email, Form Processing
cPanel Email Accounts Creator
Categories : PHP, PHP Classes, Email, Form Processing, Web Services
send_mail function to defeat Header Injection Hacking/Spamming
Categories : PHP, Email, Form Processing, Security
Using this script anyone can easily get a form result to his/her mailbox. You can use this script for any form 2 mail purpose.
Categories : PHP, Mail, Form Processing
Protect your email links from being spidered by spam email robots!
Categories : PHP, Security, Mail, Email
Solution to those 'tell-a-friend' type email issues
Categories : PHP, Email, Databases, MySQL
addslashes automatically to $_POST variables
Categories : PHP, Form Processing, Security
Class for sending mail with MIME attachments in multipart format using external sendmail, mimencode and zip
Categories : Email, Network, PHP, PHP Classes
PHP MultiList Mailing List Manager
Categories : Email, HTML and PHP, PHP
tinySendMail and tinySockMail functions for generating SMTP mail within PHP
Categories : PHP, Mail, SMTP
Form Elements Class
Categories : PHP, PHP Classes, Form Processing
Sending mail to a mailing list and showing progress
Categories : PHP, Mail, Beginner Guides
Dynamic form field
Categories : PHP, HTML and PHP, Form Processing
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
PHP3: Formmail. Just a cgi formmail, but than in PHP. It is easy to use!
Categories : HTML and PHP, Email, PHP, Perl, HTML and PHP
 vectorial pixel wrote : 1685
nice job... i like this thing!
 
 Cristi Carstea wrote : 1718
Hello!

This is an impressive piece of code!
Still, has it`s own bugs.

When I try to send email attachements (I mean word file 
= .doc) ,the email arrives as expected, but 2 unexpected 
things happen:

1) the name of the attachement is always changed (still this is acceptable)

2) worse, when you look at the attachement, you will see 
that it has lost it`s extension (doesn`t have any 
extension at all) and when you try to open it you see that 
the file is completely empty (Word sees nothing of what it 
used to be on that file)


Can you please fix those 2 bugs?
 
 Jenny Beaumont wrote : 1733
I'm having trouble adapting this to accomodate PDFs...any ideas ?
i've added $filetype=="application/pdf" and the attachment 
comes through, but it adds an "r" on the end of my file, 
plus, the whole email body layout get correupted. comes 
through as code instead of html...

 
 Amin Abbasian wrote : 1752
great code...

but i am receiving similar problem to that already mentioned.

1. attachment is received as a .DAT that when opened has no data.

and 2. how is it possible to alter this so users can send image files such as a jpeg or gif?

Please help me on this as I'm not too clued up on PHP!

Thanks!
 
 Sasa Aleksandrov wrote : 1801
This would be correct code, and it's working as it should 
be. Some variables wasn't declared or declared bad. Now 
attachment is OK and have it's original name.


<?php
     
                     
       $strname
=ucfirst($_REQUEST["strname"]);
    
$straddress=ucfirst($_REQUEST["straddress"]);
    
$strcity=ucfirst($_REQUEST["strcity"]);
    
$strstate=ucfirst($_REQUEST["strstate"]);  
         
       
       
$phone=$_REQUEST["strno"];
       if(
$phone != ""){ $strno=$phone; } else { $strno="-"; }   
       
    
$stremail=$_REQUEST["stremail"];   
    
$strcomments=ucfirst($_REQUEST["strcomments"]);

   
    
$strresume_name=$_FILES["strresume"]["name"];
    
$strresume_type=$_FILES["strresume"]["type"];
    
$strresume_size=$_FILES["strresume"]["size"];
    
$strresume_temp=$_FILES["strresume"]["tmp_name"];


       
    if(
$strresume_type=="application/octet-stream" or $strresume_type=="text/plain" or $strresume_type=="application/msword")
    {
   
        
$message'
   
   
            <table cellspacing="0" cellpadding="8" border="0" width="400">
            <tr>
                <td colspan="2"></td>
            </tr>
            <tr bgcolor="#eeeeee">
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td>
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strname.'</td>
            </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$straddress.'</td>
              </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>City</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strcity.'</td>
              </tr>
              <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>State</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strstate.'</td>
              </tr>
              <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Contact No.</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strno.'</td>
              </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
            <tr bgcolor="#eeeeee">
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Email</strong></td>
                <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$stremail.'</td>
            </tr>
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
           
            <tr bgcolor="#eeeeee">
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Comments</strong></td>
            </tr>               
            <tr bgcolor="#eeeeee">
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'
.$strcomments.'</td>
            </tr>               
                                   
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
         </table>


           

'
;

    
// MAIL SUBJECT

    
$subject "Mail with doc file attachment";
   
    
// TO MAIL ADDRESS
   
   
    
$to="some_mail@some_domain.com";

/*
    // MAIL HEADERS
                       
    $headers  = "MIME-Version: 1.0\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\n";
    $headers .= "From: Name <[email protected]>\n";

*/
 


    // MAIL HEADERS with attachment

    
$fp fopen($strresume_temp"rb");
    
$file fread($fp$strresume_size);

    
$file chunk_split(base64_encode($file));
    
$num md5(time());
   
        
//Normal headers

    
$headers  "From: yourname<[email protected]>\r\n";
       
$headers  .= "MIME-Version: 1.0\r\n";
       
$headers  .= "Content-Type: multipart/mixed; ";
       
$headers  .= "boundary=".$num."\r\n";
       
$headers  .= "--$num\r\n";

        
// This two steps to help avoid spam   

    
$headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
    
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

        
// With message
       
    
$headers .= "Content-Type: text/html; charset=iso-8859-1\r\n";
       
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
       
$headers .= "".$message."\n";
       
$headers .= "--".$num."\n"

        
// Attachment headers

    
$headers  .= "Content-Type:".$strresume_type." ";
       
$headers  .= "name=\"".$strresume_name."\"r\n";
       
$headers  .= "Content-Transfer-Encoding: base64\r\n";
       
$headers  .= "Content-Disposition: attachment; ";
       
$headers  .= "filename=\"".$strresume_name."\"\r\n\n";
       
$headers  .= "".$file."\r\n";
       
$headers  .= "--".$num."--";

   
   
    
// SEND MAIL
       
       
@mail($to$subject$message$headers);
   

     
fclose($fp);

    echo 
'<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>';
}
else
    {
        echo 
'<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>';
        
//echo "<script>window.location.href='careers.html';</script>";
    
}

?>

 
 dev gona wrote : 1864
hey, can u please show me where i want to change the FROM and TO address please...

i tried but its not excuting please help me ASAP
 
 nipen singha wrote : 1865
I could only submit if I have an attachment. Can you make it so the attachment is optional? Pl advise.
 
 Nick Follows wrote : 1876
Thanks for this code it really is great though with one small problem...

The E-mails come through with the attachment correctly named and opens fine (as original) but the message body is empty. None of the data from the form fields has been sent through.

Also is it possible to send multiple documents in this way?
 
 Eunice harkness wrote : 1891
Please can you contact me to do with changing the attachment to multiple attachment option in the form of jpg etc. My email address is [email protected] Many thanx, will be eternaly grateful for your communication.
 
 Dipesh Sejpal wrote : 1894
Hi,

It doesn't allow docx extension documents.
i want to inable doc,docx,text and pdf format.

Please help me.
Thank you for the script
 
 sapna shiva wrote :1908
hi,
 i am new to php. i was using your code but i am not getting attached file in mail