Results 1 to 3 of 3

Thread: Send Basic E-mail with Attachment

  1. #1
    Join Date
    Aug 2009
    Posts
    39
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Post Send Basic E-mail with Attachment

    What I'm trying to do is on the website, create a form which will get their name (first and last) along with why they're sending the message and having them have the option of uploading a file from the computer and attaching it to the e-mail.

    Here is the HTML:
    Code:
    <h1>Contact</h1>
    <form enctype="multipart/form-data" action="thankyou.php" method="post">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname" /><br />
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname" /><br />
    <label for="reason">Reason For Contacting Us:</label>  <select name="Reason For Contacting Us:">
        <option id="reason" value="donate">Donate</option>
        <option id="reason" value="internship">Internship</option>
        <option id="reason" value="volunteer">Volunteer</option>
        <option id="reason" value="comments">Comments/Suggestions</option>
        <option id="reason" value="other">Other</option>
        </select>
        <br />
    <textarea name="message" style="width: 100%; height: 150px;"></textarea>
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <label for="attachment">Attachment:</label> <input name="uploadedfile" type="file" /><br />
    <input name="Submit" type="submit" value="Send"  />
    <input name="Reset" type="reset" value="Reset"  /> 
    </form>
    Here is part of the thankyou.php file:

    Code:
    <h1>Thank You!</h1>
    <?php
    $to = 'a.shipley22@gmail.com';
    $email = $_POST['email'];
    $first_name = $_POST['firstname'];
    $last_name = $_POST['lastname'];
    $reason = $_POST['reason'];
    $message = $_POST['message'];
        
    //Gather file data and other things
    $attachment = $_FILES['sample']['tmp_name'];
    $sep = md5(time());
    $filename = $_FILES['sample']['name'];
    
    $filedata = file_get_contents($tmp); //Get file contents
    $fdata = chunk_split(base64_encode($filedata)); //Encode data into text form
    
    //Determine mime type
    $ext = explode('.', $filename);
    $ext = $ext[1];
    
    if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") {
    $mime_type = "image/jpeg";
    }
    elseif($ext == "gif" || $ext == "GIF") {
    $mime_type = "image/gif";
    }
    elseif($ext == "png" || $ext =="PNG") {
    	$mime_type = "image/png";
    }
    elseif($ext == "pdf" || $ext == "PDF") {
    	$mime_type == "application/pdf";
    }
    elseif($ext == "doc" || $ext == "DOC" || $ext == "docx" || $ext == "DOCX") {
    	$mime_type = "application/msword";
    }
    else {
    exit("Error: Wrong file type!");
    }
    
    //Begin the headers
    $headers = "MIME-Version: 1.0
    Content-Type: Multipart/Mixed;
      boundary=\"$sep\"
    
    charset=\"iso-8859-1\"
    Content-Transfer-Encoding: 7bit
    
    --$sep
    Content-Type: $mime_type;
      name=\"$filename\"
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
            filename=\"$filename\"
    
    $fdata
    --$sep";
        
    mail($to, 'SmileHealthy Form: ' . $reason, $message, $headers);
        
    echo 'Thank you ' . $firstname . ' ' . $lastname . ' for submitting this form.<br />';
    echo 'Your e-mail address is ' . $email;
    ?>
    I got this code from:
    http://www.knowledgesutra.com/forums...il-attachment/

    Any help is appreciated! Thank you.

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    So what is the issue you're having? does the email send but no attachment is sent? Quickly looking at your code your reference a variable $tmp - but from what you show you aren't defining the variable beforehand and it has to do with the file contents.
    PHP Code:
    $filedata file_get_contents($tmp); //Get file contents 
    I'm assuming that the variable may need to be $attachment not $tmp.

  3. #3
    Join Date
    Aug 2009
    Posts
    39
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    I thought I had sent a reply earlier but it didn't go through for some reason. But I did change what you suggested and another part of the code.

    Code:
    //Begin the headers
    $headers = "From: ' . $first_name . ' ' . $last_name . ' <{$email}>
    MIME-Version: 1.0
    Content-Type: Multipart/Mixed;
      boundary=\'$sep\'
    
    charset=\'iso-8859-1\'
    Content-Transfer-Encoding: 7bit
    
    --$sep
    Content-Type: $mime_type;
      name=\'$filename\'
    Content-Transfer-Encoding: base64
    Content-Disposition: attachment;
            filename=\"$filename\'
    
    $fdata
    --$sep";
    Now that I changed it, it not only doesn't send the e-mail but shows this in the browser:

    Code:
    MIME-Version: 1.0 Content-Type: Multipart/Mixed; boundary=\'$sep\' charset=\'iso-8859-1\' Content-Transfer-Encoding: 7bit --$sep Content-Type: $mime_type; name=\'$filename\' Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename=\"$filename\' $fdata --$sep"; mail($to, 'SmileHealthy Form: ' . $reason, $message, $headers); ?>

Bookmarks

Posting Permissions

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