Web email form

15 pts.
Tags:
PHP
Web development
Web forms
I need to make an email form work with a file upload option. Basically, I want the user to input their information into fields, include a file, and then have all the information sent to me in an email (including the file). Is this possible?
ASKED: April 12, 2008  5:28 PM
UPDATED: April 14, 2008  2:27 PM

Answer Wiki

Thanks. We'll let you know when a new response is added.

You can use the <b>$_FILES</b> variable in PHP to access files uploaded with the HTML widget <b><input name=”<i>someVarName</i>” type=”file” /></b>. Once you do some basic checks on the file (that its mime type is what you’re expecting, that its file size isn’t too big, etc), you can move the file from the temporary dir PHP automatically puts it in to a permanent directory using move_uploaded_file(). See PHP.net’s page on <a href=”http://us.php.net/features.file-upload”>handling file uploads in PHP</a> for more info. Be sure to remember that you must never trust user data without verifying it! Even things like the file’s mime type are supplied by the client; PHP makes no check on the file. And watch out for tricks like injection attacks or file names that include relative path names. In short, since PHP doesn’t have built-in taint checking, the onus is on you to make sure your upload script is secure.

For the email part, you can use PHP’s built-in mail() function. If you want to include the file as an attachment in the email itself, you’ll need to send a multipart email with the file’s contents in base64 encoding; I’ve never done that myself, but there seem to be plenty of guides out there. Zend (which owns PHP) has some code snippets on its site, including <a href=”http://www.zend.com//code/codex.php?ozid=1631&single=1″>code for sending email with attachments in PHP</a>. If you have access to the server that the PHP script is being run on, you could also move the uploaded files to a designated directory that only you (and not the world at large) has access to, and then just email yourself the file’s location.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following