...

View Full Version : POST files through a mail form using PHP



bradyj
04-21-2004, 11:14 PM
I've attempted to add to my php form (created with codingforums support :thumbsup:) to email files -- this is for people to send us their resume and coverletter -- by using the PHP method:
http://us4.php.net/manual/en/features.file-upload.php

However, this uploads to a server, not necessarily sending to an email. Can this be done? If so, what variables can I use to add this to my code?

The html file inputs are basic enough:


<input type="hidden" name="MAX_FILE_SIZE" value="50000" />
<input name="coverletter" type="file" size="15" />
<input name="resume" type="file" size="15" />


And the full php form is pretty straight forward:


<?php
$first_name= $_POST['first_name'];
$last_name= $_POST['last_name'];
$title= $_POST['title'];
$company= $_POST['company'];
$address1= $_POST['address1'];
$address2= $_POST['address2'];
$city= $_POST['city'];
$state= $_POST['state'];
$zip= $_POST['zip'];
$country= $_POST['country'];
$email= $_POST['email'];
$phone= $_POST['phone'];
$fax= $_POST['fax'];
$category= $_POST['category'];
$request_category= $_POST['request_category'];
$request= $_POST['request'];
$message= $_POST['message'];

$body = "
First Name: $first_name
Last Name: $last_name
Title: $title
Company: $company
Address:
$address1
$address2
$city
$state
$zip
$country

Email: $email
Phone: $phone
Fax: $fax
Request Category: $request_category
Additional Info: $request
$message
";

if (!isset($email) || !isset($first_name)) {
header("Location: http://www.academystudios.com/error.php ");
}
elseif (empty($email) || empty($first_name)) {
header("Location: http://www.academystudios.com/error.php ");
}
else {
mail("sales@academystudios.com","From: academystudios.com visitor",$body);
header("Location: http://www.academystudios.com/thankyou.php ");
}
?>


Any ideas how to adjust, if I can even accomplish it? I'm trying to keep away from CGI, i've been successful at that for years now :)

sad69
04-22-2004, 01:07 AM
Yes you can accomplish this!

I just checked out the php manual:
http://www.php.net/manual/en/ref.mail.php

Search for the following user comment:
nico at cpp-club dot net
30-Jan-2004 03:33

And you'll have your answer!

Basically you use the file upload method you posted and upload the file into a directory on the server. Then you use nico's method to send the message. You store the resume and coverletter in the attachment array.

Read the first user comment about magic_quotes.. it may save you some grief in the future..

Hope that helps,
Sadiq.

bradyj
04-22-2004, 09:52 PM
There is no other way to do it without saving it to my server AND then emailing it off? Why not a direct send?

sad69
04-22-2004, 10:11 PM
Short answer:
Because you can't.

Long answer:
Let's break it down. Who's sending the email? The server or the client? If your 'action' in the form tag does not begin with mailto: then it's the server sending the email.

Let's pretend you're the server. You want to send an email. You type it all up and now you want to attach a file to the email. But that file is not on your computer. How are you going to attach it? If it's on the web, you can include a link to it, but that's not the same as an attachment, as the reader will have to click on the link and then save it to their harddrive. But in your case, the file is not on the web.

Does that make more sense?

I assume you're having an issue with uploading the file because of server space. Well once the file gets uploaded, it will be stored in a temporary file by default. Usually you would need to move that temporary file someplace, but in this case you don't need to. You just attach that temporary file to the email and you're done.

I'm not sure how you clear those temporary files, but I assume that gets taken care of on its own.

If you have any further questions, or if anyone wants to correct me, just post.

Hope that helps,
Sadiq.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum