Page 1 of 1

Send Mail with file attachments

Posted: Wed Jul 22, 2009 6:01 am
by revathy
Hai , I want to send a mail with file attachment. To add a file using browse button i used the <input type=file> in html. After the selection a file, i have been pass that selected file path to another php file. Using GET method i can able to retrive the file name only, not full path. How can i pass full path of that selected file to php file?.... :roll:

Thanks in advance,
Revathy.

Re: Send Mail with file attachments

Posted: Wed Jul 22, 2009 6:07 am
by jackpf
You mean $_FILES['filename']['tmp_name']?

Re: Send Mail with file attachments

Posted: Wed Jul 22, 2009 6:24 am
by revathy
Hai, my Html code is,

<html>
<body>
<form name="form1" action="new.php" method="GET">
<input type="file" name="files" value="Select"><br>
<input type="Submit" value="Submit"/>
</form>
</body>
</html>

and my new.php code is,

<?php
$name = $_GET["files"];
echo $name;
?>

By using this code i can able to retrive only the attached file name. (for eg: pink.jpg).
But my need is i want to retrive the full path the file like, D:\Revathy\MyFiles\pink.jpg (whatever the path of the file).

Re: Send Mail with file attachments

Posted: Wed Jul 22, 2009 7:40 am
by jackpf
Yeah, that's not how you upload files.

You need to provide the right form enctype, and use the $_FILES array. I suggest you look up a tutorial.

Re: Send Mail with file attachments

Posted: Thu Jul 23, 2009 12:29 am
by revathy
Thanks a lot jack. Now i used $_FILES array to get the full path.
Now i can able to get the file and it uploaded to my server too. Using the target_path i tried to attach my file to send mail. But it has no content while i tried to open it after downloaded it from mail.

I attached a small text file, and i tried to open it, some content is missed there. My original text file size is 156bytes, but my downloded file from mail is 99bytes.

And again i tried to send a mail with jpg file attachment. It downloads the same file but, while i tried to open, it shows no preview available.

Can you understand my problem?

Re: Send Mail with file attachments

Posted: Thu Jul 23, 2009 6:46 am
by jackpf
I'm not too familiar with mail attachments, but if you post your code I can have a look...

Re: Send Mail with file attachments

Posted: Thu Jul 23, 2009 7:13 am
by revathy
Hi, Now i can able to send a mail with attachment and i can able to retrieve too. But now my problem is i can't able to send a mail with word document file as attachment. By using the following code i can able to send mail with only image files(jpg, gif).

<?PHP

$target_path = basename( $_FILES['uploadedfile']['name']);
echo $target_path;


if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}

$to = 'revathy@yahoo.com';

$subject = 'PHP Mail Attachment Test';

$bound_text = "jimmyP123";

$bound = "--".$bound_text."\r\n";

$bound_last = "--".$bound_text."--\r\n";

$headers = "From: XX@yahoo.com\r\n";
$headers .= "MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"$bound_text\"";

$message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
.$bound;

$message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
."Content-Transfer-Encoding: 7bit\r\n\r\n"
.$bound;
//."hey my <b>good</b> friend here is a picture of regal beagle\r\n"

$file = file_get_contents($target_path);

$message .= "Content-Type: image/jpg; name=\"attached.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-disposition: attachment; file=\"attached.jpg\"\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;


if(mail($to, $subject, $message, $headers))
{
echo 'MAIL SENT';
} else {
echo 'MAIL FAILED';
}

?>

Can you please correct the code for attaching word document?

Re: Send Mail with file attachments

Posted: Thu Jul 23, 2009 8:15 am
by jackpf
That's because you're sending the headers of a jpeg image. You'll need to modify them to send the right headers for whatever file type you're sending, on this line:
$message .= "Content-Type: image/jpg; name=\"attached.jpg\"\r\n"

Re: Send Mail with file attachments

Posted: Thu Jul 23, 2009 8:19 am
by revathy
Ya the problem was solved now. Thank you very much jack... Thanks a lot... :)