Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Help: Upload file to email

    Hello,

    I have been racking my brain trying to get my head around this php.
    I have a simple form that asks (Name, Email, Directions, & Upload)

    The issue I'm having is getting a PHP script that will email the form data to a desired email address. (Mainly the attachment)

    I have the form data assembled as follows:

    Code:
    <form name="upload" method="post" action="uploademail.php" enctype="multipart/form-data">
              <table cellpadding="3" cellspacing="0">
                <tbody><tr>
                  <td class="tablebody" align="right" valign="top"><label for="name">Name:</label></td>
                  <td><input name="name" class="textfield" id="name" size="30" type="text"><br />
    														<br />
    													</td>
                </tr>
                <tr>
                  <td class="tablebody" align="right" valign="top"><label for="email">Email:</label></td>
    
                  <td><input name="email" class="textfield" id="email" size="30" type="text"><br />
    														<br />
    													</td>
                </tr>
                <tr>
                  <td class="tablebody" align="right" valign="top"><label for="directions">Directions:</label></td>
                  <td><textarea name="directions" cols="30" rows="3" class="textfield" id="directions"></textarea><br />
    														<br />
    													</td>
                </tr>
                <tr>
                  <td class="tablebody" align="right" valign="top"><label for="uploaded_file">Filename:</label></td>
    
                  <td><input name="uploaded_file" id="uploaded_file" type="file"><br />
    														<br />
    													</td>
                </tr>
    												<tr height="65">
                  <td valign="top" height="65"><input name="KT_Insert1" id="KT_Insert1" value="UPLOAD" type="submit">
                  </td>
    													<td class="errortext" valign="top" height="65"><div id="upload_errorloc" class="error_strings">
    															<p class="errortext"></p>
    														
    															</td>
    												</tr>
    											</tbody></table></form>

    I need help getting the PHP script.
    I have looked around places on the net and I just don't fully understand them.

    Any help in this matter would be GREATLY appreciated.

    Here is what I have so far.

    Code:
    <?php
    if(!isset($_POST['submit']))
    {
    	//This page should not be accessed directly. Need to submit the form.
    	echo "error; you need to submit the form!";
    }
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['questionscomments'];
    
    
    $email_from = 'EMAIL@COMPANY.COM';//<== update the email address
    $email_subject = "New Contact Form From $name.\n";
    $email_body = "You have received a new message from the user $name.\n".
        "Here is the message:\n $message".
        
    $to = "EMAIL@COMPANY.COM";//<== update the email address
    $headers = "From: $email_from \r\n";
    $headers .= "Reply-To: $visitor_email \r\n";
    //Send the email!
    mail($to,$email_subject,$email_body,$headers);
    //done. redirect to thank-you page.
    header('Location: thank-you.html');
    
    
    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
      $injections = array('(\n+)',
                  '(\r+)',
                  '(\t+)',
                  '(%0A+)',
                  '(%0D+)',
                  '(%08+)',
                  '(%09+)'
                  );
      $inject = join('|', $injections);
      $inject = "/$inject/i";
      if(preg_match($inject,$str))
        {
        return true;
      }
      else
        {
        return false;
      }
    }
       
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is a little bit on the older side, but still perfectly valid for how to send attachments via email: http://www.webcheatsheet.com/PHP/sen...php#attachment

    The real big things here are the content-type is multipart/mixed and you'll see --PHRASE and --PHRASE-- around. Those are bounds marking for the email itself.
    This should do exactly what you're looking for, you'll just need to edit the text set for the message (looks like 1 part plain, 1 part html), and the filename in use. You can get all of that from you're form I would presume.

    Another article here: http://www.daniweb.com/forums/thread60795.html
    This one is a little shorter, but its a little more difficult to read.
    You can look through a few others on google as well, there are a couple good ones: http://www.google.com/search?hl=en&r...i=g10&aql=&oq=
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fou-Lu,

    Thank you for the response.
    I feel I may have written it wrong...

    What I am looking for is the user to upload a file via the form.
    And when he hits the submit button it emails that file to me.

    The two sources you linked are good, but they do the reverse.
    They email a file to the user.

    Any help in that area?

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You simply change the address for the email from the users to your own
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JAY6390,

    Ok. but I don't know how to get it from the submit field to a folder.. then the folder to me..

    I will continue to browse.
    Thanks for leading me in the correct direction tho.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •