1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help ! Trying to adding email attachement in my contact mail form

Discussion in 'PHP' started by nura235, Sep 18, 2011.

  1. #1
    Hi,

    As the subject says i am implementing the file upload field in my current form.

    first i have successfully added the code in my html webpage to get the file upload box.

    The next thing is bit complicated for me as i am the php guy and i tried but cannot able to go well.

    Here is the current code of my contact form which work pretty well without file upload and have js validation.
    SEMrush
    <?php ob_start();
    $fromemail="No-Reply "; // change here if you want
    $toemail="info@xyz.com";   // change here if you want
    $sub="XXXX feeback";          // change here if you want
    $success_page_name="thanks.html";
    ////// do not change in following
    if($_SERVER['REQUEST_METHOD']=="POST")
    {
    $fieldnm_1=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_1']));  
    $fieldnm_2=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_2']));  
    $fieldnm_3=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_3']));  
    $fieldnm_4=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_4']));  
    $fieldnm_5=str_replace ( array("\n"), array("<br>"),trim($_REQUEST['fieldnm_5']));  
    
    
    $contentmsg=stripslashes("<br><b><font style=color:#CC3300>$sub</font></b><br>
    <table width=708 border=0 cellpadding=2 cellspacing=1 bgcolor=#CCCCCC>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Name *:</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_1</td>
    </tr>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Email *:</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_2</td>
    </tr>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>City *:</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_3</td>
    </tr>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Phone *:</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_4</td>
    </tr>
    
    <tr>
          <td width=165 align=right valign=top bgcolor=#FFFFFF><B>Message *:</b> </td>
          <td width=565 align=left valign=top bgcolor=#FFFFFF>$fieldnm_5</td>
    </tr>
    
    </table>
    ");
    
    ////
    $headers  = "MIME-Version: 1.0
    ";
    $headers .= "Content-type: text/html; charset=iso-8859-1
    ";
    				
    $from=$fromemail;
    				
    $headers .= "From: ".$from." 
    ";
    				
    @mail($toemail,$sub,$contentmsg,$headers);
    				
    				
    header("Location:$success_page_name");
    
    }
    ?>
    
    
    PHP:
    and the file upload code which i found at w3school.com which will save the uploaded file to server here.

    <?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      if ($_FILES["file"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["file"]["name"] . "<br />";
        echo "Type: " . $_FILES["file"]["type"] . "<br />";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
    
        if (file_exists("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?> 
    PHP:
    I would like to merge them so that the uploaded file will get in mail attached form with my current web form.

    Thanks in Advance.
     
    nura235, Sep 18, 2011 IP
    SEMrush
  2. AsHinE

    AsHinE Active Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    88
    #2
    AsHinE, Sep 18, 2011 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Your code won't send an attachment at all - there's more code to it than that (I stopped analyzing it when I saw that you're missing the MIME boundary). See http://www.emanueleferonato.com/2008/07/22/sending-email-with-multiple-attachments-with-php/ for code that works. (The second "file" is your web form file.)

    Or you could send a single file by concatenating the uploaded file and the form data. Or you could send the uploaded file and include the form data as the message in the email.
     
    Rukbat, Sep 18, 2011 IP
  4. workingsmart

    workingsmart Active Member

    Messages:
    411
    Likes Received:
    12
    Best Answers:
    9
    Trophy Points:
    70
    #4
    Why not upload file and store in folder with a link to the file in the email message?....
     
    workingsmart, Sep 18, 2011 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    25,533
    Likes Received:
    3,547
    Best Answers:
    103
    Trophy Points:
    665
    #5
    Use phpMail, it's easy to use and handles all the gotchas you don't really need to learn about.
     
    sarahk, Sep 18, 2011 IP
  6. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #6
    JohnnySchultz, Sep 19, 2011 IP
  7. akumar2

    akumar2 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes.. you can use phpmailer class..
     
    akumar2, Sep 20, 2011 IP