Log in

View Full Version : Send Basic E-mail with Attachment



Abbster22
07-27-2010, 10:16 PM
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:


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



<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/topic/28987-form-to-php-mail-attachment/

Any help is appreciated! :) Thank you.

fastsol1
07-27-2010, 10:37 PM
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.

$filedata = file_get_contents($tmp); //Get file contents

I'm assuming that the variable may need to be $attachment not $tmp.

Abbster22
07-27-2010, 11:42 PM
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.



//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:



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