Page 1 of 1

sending files in an email with php

Posted: Sat Sep 16, 2006 6:43 am
by cooper3000
I have a added a form to my site that sends its contents to my email, I want to add an extra part on the form where the user can add a MS Word document, and then this document be included on the email that is sent to me.

Is this possible with PHP, and if so, does anyone know of a tutorial I can look at?

Thanks

Posted: Sat Sep 16, 2006 7:52 am
by feyd
It's possible, yes. I wouldn't recommend coding it yourself when there are several libraries available that can perform such a task.

Swift and phpMailer are the oft recommended ones.

Feyd is invariably correct

Posted: Sat Sep 16, 2006 11:04 am
by akimm
However, if you do not know how to work with the libaries yet, here is a mailing script i wrote for my website.

Code: Select all

<?php 
#put conditions for $_POST, $_GET here
if ($_POST['ms_doc']) {
$msg = "E-MAIL SENT FROM YOUR WEBSITE\r\n"; 
$msg .= "Sender's NAME: \t{$_POST['name']}\r\n"; 
$msg .= "Sender's EMAIL:\t{$_POST['sender_email']}\r\n"; 
$msg .= stripslashes("ms_doc:\t{$_POST['ms_doc']}\r\n"); 
$to = "you@localhost.com\r\n"; 
$subject = "MS_DOC\r\n"; 
$mailtoheaders = "From: You <you@localhost.com>\r\n"; 
$mailheaders .= "Reply-To: {$_POST['sender_email']}\r\n"; 
mail($to, $subject, $msg, $mailheaders); 
## put extra conditions here, like post the rest of the info to sample DB, or
## sample file
} else {
## put extra conditions here, like post the rest of the info to sample DB, or
## sample file
?>

Posted: Sat Sep 16, 2006 11:14 am
by Luke
Akimm... have you heard of Email header injection? Your script is VERY insecure. I'd read up on that stuff.

Cooper... I'd just go with one of those libraries... swift is extremely easy to get support for since the creator is a regular poster here. Both of the libraries are pretty easy to work with anyway... so I'd just use one of those. You will also want to read up on email header injection if you aren't aware of it already.

Posted: Sat Sep 16, 2006 11:30 am
by cooper3000
Many thanks everyone, I have downloaded and now using swift, it is ace. Thanks again everyone

Posted: Sat Sep 16, 2006 11:30 am
by Chris Corbyn
The Ninja Space Goat wrote:You will also want to read up on email header injection if you aren't aware of it already.
*cough* Swift deals with this for you. In fact, perhaps a little strongly though it wouldn't block anything it would just encode over the top of it.

Posted: Sat Sep 16, 2006 12:22 pm
by Luke
d11wtq wrote:
The Ninja Space Goat wrote:You will also want to read up on email header injection if you aren't aware of it already.
*cough* Swift deals with this for you. In fact, perhaps a little strongly though it wouldn't block anything it would just encode over the top of it.
which, by the way, is fantastic my good man... but he still needs to read up on it. :D

Ah

Posted: Sat Sep 16, 2006 4:24 pm
by akimm
Ninja, thanks for the warning. My scripts are usually* very lowly trafficked, but I should learn the correct practices. I will most certainly do the reading you've suggested.