views:

56

answers:

4

Hi all,

I have a basic form consisting of input fields as well as a file field. I have a few things that I want the form to do. Collect the information (obviously). There's also an option to upload a file, (probably .doc,.pdf,.docx), so I want to restrict the attached file only to those extensions and under 2MB. All I know is that I have to have my form "enctype=multipart", but that's all I know.

I have the following PHP code:

<?

$to = '[email protected]';
$subject = 'Contact from your website';
$message = 'From: ' . "\n\n" . 'Name: ' . $_REQUEST['name'] . "\n\n" . 'E-mail: ' . $_REQUEST['email'] . "\n\n" . 'Comments: ' . $_REQUEST['comments'];
$email = $_REQUEST['email'];
$headers = 'From: ' . $email . "\r\n" .
            'Reply-To: ' . $email . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";    



$str = $_REQUEST['email'];

if( $str == "E-mail address" || $str == "" )
{
 echo "Please use your browser's back button enter a valid E-mail address";

} else {
 mail ($to, $subject, $message, $headers);
 header("Location: thankyou.html");
}  

?>

How can I tweak it to allow it to send the attached file?

If possible, I'd love to see examples. Please understand that I'm extremely new to PHP, and until now have not needed to send PHP web forms with an attachment. So all that I ask of you is that if you post an example, please try to clarify the respective HTML form code that will work with the example.

Any help would be greatly appreciated!

Thanks a lot, Amit

A: 

If you've access to the PEAR library, this'd make adding attachments a cinch. Have a look here.

Will A
I have a few questions here:1) How do I get access to the PEAR library / install it on the server (or is it even up to me?!)2) In the example link that you show, it says how to attach a predetermined file such as an image. I want the file attached to be a file the user uploads from their computer (just to make things clearer, I'm making a resume database, so I want my users to send their resume through the form). Will I then replace where it says "img here" with $file-attached or something like that?!
Amit
Getting access - if you own the server and administer it yourself you should have no problems getting it installed - if you don't then ask your hosting company whether PEAR is available (and if it can be made available to you if not). You're not limited to what you can attach - any file will be fine, image or not.
Will A
Thanks for that. But how do i do something like $_REQUEST['file'] in place of <img src="img path" />...you know? the file sent to me is a file that is on their computer, not already on my server
Amit
Only part of this will be relevant (the uploading), but take a look - I'm sure you can figure out the needed parts. http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/uploading-files-to-mysql-database.aspx
Will A
+1  A: 

To mail an attachment, you can either use framework methods, or write the method yourself. Some sample code can be a good point to start.

To get a file sent from the browser, you have to use FILE variable. PHP documentation explains quite well how to do it.

To restrict file types to .doc and .pdf, you have to read the beginning of the file and compare to a "real" .doc or .pdf. An easier way would be to compare file extension, but the user of your website can always rename virus.exe to document.pdf.

MainMa
+1  A: 

haven't tested but it should work in theory.

Use this code which uses buffers. (Code Source: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php)

<?php
//define the receiver of the email
$to = '[email protected]';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: [email protected]\r\nReply-To: [email protected]";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/zip; name="attachment.zip" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?> 
Codex73
There are a few flaws with the code. Either way, I tried it, it didn't work. Thanks for trying though.
Amit
+1  A: 

Use PHPMailer. It's free, works well, and makes using attachments a snap. I've seen Swiftmailer mentioned a lot here on SO as well, but haven't used it myself so can't say much about it.

Marc B
I don't quite understand how to use PHPMailer. Are there any example files that show what to do?!
Amit
On the PHPMailer site, on the menu bar, select Products -> PHP Mailer -> PHPMailer Examples. There's a lot there, from basic stuff to specific SMTP server configurations
Marc B