Loading...


bookmark - Form To Php Mail. Attachment

Form To Php Mail. Attachment

 
 Discussion by DogEater008 with 16 Replies.
 Last Update: November 22, 2010, 3:48 am ( View Rated (3) )
 
bookmark - Form To Php Mail. Attachment  
Quickly Post to Form To Php Mail. Attachment  w/o signup Share Info about Form To Php Mail. Attachment  using Facebook, Twitter etc. email your friend about Form To Php Mail. Attachment Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

i know there are a few topics talk about attachment problem, but i want to know if anyone could show me a basic code for attaching files like pictures with the message.

I made a html form, and redirect the information to my mail.php file to process the information and send it to my email. I want to know what do i have to do to attach files like pictures. I tried to search on google, and the codes are so complicated (i'm an amateur at this). Would it be possilbe if you could show me the code and explain to me what it does and how i could customize it to fit my needs?

thankyou







   Thu Oct 20, 2005    Reply         

CODE

// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
// $_FILES.  In PHP earlier then 4.0.3, use copy() and is_uploaded_file()
// instead of move_uploaded_file

$uploaddir = '/public_html/site/html/uploads/';
$uploadfile = $uploaddir. $_FILES['userfile']['name'];

print "<pre>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  print "File is valid, and was successfully uploaded. ";
  print "Here's some more debugging info:\n";
  print_r($_FILES);
} else {
  print "Possible file upload attack!  Here's some debugging info:\n";
  print_r($_FILES);
}
print "</pre>";


i found that code and when i tried to exacute the php file, it said

Possible file upload attack! Here's some debugging info:
Array
(
[attachment] => Array
(
[name] => logo.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpmlM3po
[error] => 0
=> 20655
)

)

i tried to give my folders full permission and it still doesn't work.


and how can I attach the uploaded file(s) to an email

   Sat Oct 22, 2005    Reply         

ok.. i think i got the file to upload to my tmp folder ( i think because when i exacute the .php file , i got the message

CODE


File is valid, and was successfully uploaded.
Here is some more debugging info:Array
(
   [attachment] => Array
       (
           [name] => logo.jpg
           [type] => image/jpeg
           [tmp_name] => /tmp/phpvzOMho
           [error] => 0
           [size] => 20655
       )

).  [/CODE


So i'm guessing it works. NOw.. my question is.. how am i suppose to embed that into my mail.

[CODE]
<?
$name=$_POST['name'];
$email=$_POST['email'];
$attachment=
$to="example@gmail.com";
$message="Terms and Conditions: $agree
\nName: $name Middle:$middle
\nAge: $AGE
\nDate of Birth: $month $date, $year
\nWeight: $weight Pounds
\nHeight: $height Inches
\nAddress: $address $city, $state $code
\nTelephone: $phone
\nEmail: $email
\nSchool: $school
\nMajor: $major
\nAbout $name
\n $reason";
if(mail($to,"Application from $name",$message,"From: $email\n")) {
echo "Thankyou for registeringt";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form

correctly. Please Click Back";
}


i dont' really know what i'm doing exactly. So please help me out. It seems like no one is bothering to help. =(

   Sat Oct 22, 2005    Reply         


Hi dog eater, I find myself in the same problem as you. I tried looking a little bit in google and php dot net but Im also an amateur...

I have a working php sendmail script, which I adapted from different stuff I found here and there, I didnt write the whole thing but I made the final mix...

I know (guess you already know this too) how to send mail, the most basic line to achieve so is this:

mail($admin_mail, $subject, $content, $headers)

Where admin mail is the mail you want the message to be sent, and the headers are additional information which is optional. Subject and Content I think that are obvious. But the command to attach a file is what is missing, and I dont know either how to do it...

Hope someone could help us both.. thanks in advance Ill keep browsing the web for it but would apreciate a lot yer help...

   Tue Feb 7, 2006    Reply         

Make sure you're cleaning any input before sending it to your mail function.

   Tue Feb 7, 2006    Reply         

How is that? cleaning any input? You mean from the html form itself? Could you teach us how to do so? Thanx a lot Tyssen, but Im not sure if this is a tip for sending secure mail through php or actually about the thread's topic... I mean no offense...

   Tue Feb 7, 2006    Reply         


Well it doesn't necessarily relate to the topic of adding email attachments, but whenever you're using a HTML form to transmit info to your mail program via PHP, you need to be aware of the danger of 'email header injection'. A Google search on that will provide you with more and better info than I could give here.

   Tue Feb 7, 2006    Reply         

Here you go. Use this opensource code to send your mails.

http://phpmailer.sourceforge.net/

   Wed Feb 8, 2006    Reply         

Thanx Tyssen its a nice tip ill google it...

And kvkv thanx for that link, ill give it a try, i havent tested it yet but the main page displays this as a feature:

Multiple fs, string, and binary attachments (those from database, string, etc)

Im not sure if it is what i need, do you know something about it? I dont want to upload files to a database but to send them right through the form... thanks a lot!

   Wed Feb 8, 2006    Reply         

I'm a little late with this, but I can show you how I did it (after doing a lot of searches from Google). It's a little more simpler. However, one limitation of the code I'm going to give, is that it supports only gif or jpg images. Should you want to add more, it's easy to do, but you need to know the mime type for the file.

CODE

//Gather file data and other things
$tmp = $_FILES['sample']['tmp_name'];
$sep = md5(time());
$filename = $_FILES['sample']['name'];

$filedata = file_get_contents($tmp); //Get file contents
$fdata = chunk_split(base64_encode($filedata)); //Encode data into text form

$to = "you@domain.com"; //Email address to send this to

//Determine mime type
$ext = explode('.', $filename);
$ext = $ext[1];

if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") {
$mime_type = "image/jpeg";
}
elseif($ext == "gif" || $ext == "GIF") {
$mime_type = "image/gif";
}
else {
exit("Error: Wrong file type!");
}

//Begin the message. Be sure to change this how you want it.
$message = "Email message here";

//Begin the headers
$headers = "From: \"From Name\" <{$email}>
MIME-Version: 1.0
Content-Type: Multipart/Mixed;
boundary=\"$sep\"

charset=\"iso-8859-1\"
Content-Transfer-Encoding: 7bit

--$sep
Content-Type: $mime_type;
name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=\"$filename\"

$fdata
--$sep";

$subject = "Email Subject Here";
mail($to, $subject, $message, $headers);


One last limitation: Files with more than one . in them may not be processed completely right (I've not tested it that much). I don't know exactly how to get the extension. I don't know just how many files you'll get with more than one in it, though.

I will now explain it to you. The file sending is actually done using the mail headers. However, the message has to be converted to base64 encoding (which will send it as text). This is done by first getting the contents of the file using file_get_contents() then chunk_split() and base64_encode(). The chunk_split() converts the text to match RFC 2045 semantics (that way the email client will be able to read the base64 encoded file).

Oh, and remember where it says $_FILES['sample'] change 'sample' to the name of the file upload field (just reminding you). Also, the $email in the headers should be changed to whatever you want the From part to be.

I have tested this code many times, and it works the way it should. However, you still need to include your form, and add whatever data you're going to send in the message. If you want to send other types of files, you just add more after the elseif. Such as:

CODE

if($ext == "JPG" || $ext == "jpg" || $ext == "JPEG" || $ext == "jpeg") {
$mime_type = "image/jpeg";
}
elseif($ext == "gif" || $ext == "GIF") {
$mime_type = "image/gif";
}
elseif($ext == "bmp" || $ext == "BMP") {
$mime_type = "image/bmp"; //I think that's the mime type for BMP files
}
elseif($ext == "txt" || $ext == "TXT") {
$mime_type = "text/plain";
}
else {
exit("Error: Wrong file type!");
}


One last note: I know the mime_content_type() function gives me the mime type, but I had some problems with it. Anyway, this method also ensures that you get only the file types that you want. Also, I used the file extension in both lowercase and caps to ensure that it would be sent. I know that sometimes the file extensions get capitalized automatically for some reason in Windows. I hope this helped you. If I need to explain anything more, please let me know.

   Fri Feb 24, 2006    Reply         

Bulk Mail With attachment
Form To Php Mail. Attachment

I want to sent bulk mail with attachments, the emailids from mysql database and the messages and subjects given by user. Please give me
Suggetion to me or send me sample scripts. I am new to php scripts

-question by Jeganathan

   Tue Jan 29, 2008    Reply         

response, its easy
Form To Php Mail. Attachment

Just so you guys know if you send the file through a form submit, the $_FILES contains the MIME type information, just send that variable to the header for a quick solution... Encoding is different though... There might be a good encoding format that will work for just about anything your sending... Not sure though

   Sat Apr 5, 2008    Reply         

Thanks for a beautiful site! I have added you in elected! http://cbnch.Icr38.Net


-reply by Anne

   Thu May 15, 2008    Reply         

Thank for the articles of (PHP Mail with Attachment)
Form To Php Mail. Attachment

This articles help me a lot. Thank for it.

-feedback by smithveg

   Fri Jun 20, 2008    Reply         

please can u give me the code for sending an file attachment in send mail function
Form To Php Mail. Attachment

I tried very much for the specific code but in vain. Please provide me the exact code to do so.
Thank you,
Pritish

-reply by pritish

   Tue Jul 15, 2008    Reply         

Sending attachment optionalForm To Php Mail. Attachment

Hello,

I'm attempting to cobble together a Formail that sends contact form information (from the form on the site) and an image attachment directly to the email address I've designated, rather than upload the image to my server for me to find and download.  I've tried the script above and it works perfectly for the image attachment part, except that it makes sending an image mandatory because there is no "null," if you will, file extension provided for.  So when I try to send the form without attaching an image, it errors out.  Do you happen to know the code I could add so that it will allow for no file attachment and still process?

Thank you.

   Sun Mar 29, 2009    Reply         

I used the same code to send mail... Sending mail is perfect... But i can't get the contents in attachment. All the contents are attached inside the body of the mail as a encoded format... Thanks in advance for any good solution...?

   Mon Nov 22, 2010    Reply         

Quickly Post to Form To Php Mail. Attachment  w/o signup Share Info about Form To Php Mail. Attachment  using Facebook, Twitter etc. email your friend about Form To Php Mail. Attachment Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Email Script/form With Php

Today, I'm going to show you how to make an email script using PHP and HTML. Most people usually put <a href="mailto:blahblah@blah.blah">Email me</a> if they want an email link on their site, but there are several cons to this. First, it& ...more

   18-Apr-2005    Reply         

Php Formmail Generator

PHP FormMail Generator QUOTEPHP FormMail Generator - A tool to create ready-to-use web forms in a flash. Once the form has been generated, you have a full functional web form. Including error checking of required fields, email address validation, credit card number & expiry date checking ...more

   22-Apr-2005    Reply         

A Question About Php Mail

I have code my website with send mail feature, I just notice that everytime i send out email, it uses gamma.xisto.com as the sender, but the fact is I want to use my own email account that exist in trap17, suppose I create email account name webmaster@innosia.trap17.com then how exactly can I send a ...more

   11-Sep-2008    Reply         

Encryption System    Encryption System (8) (9) Loop Through Form How do you do it?  Loop Through Form How do you do it?