sending files in an email with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cooper3000
Forum Newbie
Posts: 8
Joined: Wed Jan 18, 2006 5:16 am
Location: London

sending files in an email with php

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Feyd is invariably correct

Post 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
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
cooper3000
Forum Newbie
Posts: 8
Joined: Wed Jan 18, 2006 5:16 am
Location: London

Post by cooper3000 »

Many thanks everyone, I have downloaded and now using swift, it is ace. Thanks again everyone
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Ah

Post 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.
Post Reply