sending email with jpeg attachemnt

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
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

sending email with jpeg attachemnt

Post by kendall »

HEllo,

I have been trying to send a jpeg as an attachement using mail()

the following is the code

Code: Select all

//get file
			$fp = fopen($DOCUMENT_ROOT.'/community/clubliquid/images/invis/invi.jpg',"r");
			$filename = fread($fp, filesize($DOCUMENT_ROOT.'/community/clubliquid/images/invis/invi.jpg'));
			$filename = chunk_split(base64_encode($filename));
			fclose($fp);
			$mime_boundary = "<<<--==+X&#1111;".md5(time())."]";
			$headers .= "From: <".$SenderMail."> \r\n";
			$headers .= "To: <".$Mail."> \r\n";
			$headers .= "MIME-Version: 1.0 \r\n";
			$headers .= "Content-Type: multipart/mixed; \r\n";
			$headers .= "X-attachments: invi.jpg";
			$headers .= " boundary="".$mime_boundary."""; 
			$headers .= "Content-Transfer-Encoding: 7bit";
			$headers .= "This is a MIME Encoded message \r\n";
			$message .= "--".$mime_boundary." \r\n";
			$message .= "Content-Type text/plain; charset=us-ascii \r\n";
			$message .= wordwrap($Message, 72)." \r\n";
			$message .= "--".$mime_boundary." \r\n";
			$message .= "Content-Type: image/jpeg; ";
			$message .= "name="invi.jpg" \r\n";
			//$message .= "Content-Type: application/x-image-jpeg; \r\n";
			$message .= "Content-Transfer-Encoding: base64 \r\n";
			$message .= "Content-Disposition: attachment; ";
			$message .= "filename="invi.jpg" \r\n"; 
			$message .= $filename.' \r\n';
			$message .= "--".$mime_boundary."-- \r\n";
			mail($Mail,$Subject,$message,$headers);
However in trying to open the attachment with frieworks mx i get a 'file unknown type'

what is done wrong here

Kendall
User avatar
igoy
Forum Contributor
Posts: 203
Joined: Fri May 02, 2003 11:57 pm
Location: India
Contact:

Post by igoy »

have you tried opening the image with ay other application... like Photoshop or ACDSee .... for easiest way in Internet Explorer or Netscape (since both the brwosers can show Images.. :)...)
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

sending email with jpeg attachemnt

Post by kendall »

Igoy,

No thats not the problem...the damn thing was created with the program to start with...its something to do with the code but im not familiar with emailing attachments so i don't no where to begin....

Kendall
For the record i have tried to open the attachment using all available software including browsers...nothing!!!!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

is the filesize of the JPG you receive on the email, the same size as the one you are sending from the script?

Mark
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Bech100

No...At a time is was...but since i have been tampering with the code....No :(

Kendall

just to note: the file was uploaded to the server...the script reads the file from the server
[/quote]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Try this code, works for me. Just change the paths and filenames to what suits your setup.

Code: Select all

$file = "newname.jpg";
$file_source = "C:/Apache/HtDocs/test/oldname.jpg";

$to = "myname@myserver.nl"; 
$subject = "test mail #1"; 
$message = "test"; 
$long_date = date("r",time()); 
$headers .= "Date: $long_date\r\n"; 
$headers .= "From: Birthday Reminder <birthday@example.com>\r\n"; 
$headers .= "To: $to\r\n"; 
$headers .= "X-Mailer: SOME MAIL SERVER\r\n"; 
$headers .= "MIME-Version: 1.0\r\n"; 
$headers .= "Content-Type: multipart/mixed; boundary="CCM-12345"\r\n\r\n"; 
$headers .= "This is a MIME encoded message\r\n\r\n"; 
$headers .= "--CCM-12345\r\n"; 
$headers .= "Content-Type: text/plain; charset="us-ascii"\r\n"; 
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
$headers .= "Please find attached file\r\n\r\n"; 
$headers .= "$message\r\n\r\n"; 
$headers .= "--CCM-12345\r\n"; 
$headers .= "Content-Type: image/jpeg; name="$file"\r\n"; 
$headers .= "Content-Transfer-Encoding: base64\r\n"; 
$headers .= "Content-Disposition: attachment; filename="$file"\r\n\r\n"; 
set_magic_quotes_runtime(0);
$fp = fopen($file_source, "rb");
$fstr = fread($fp, filesize($file_source)); 
$headers .= chunk_split(base64_encode($fstr),72); 
fclose($fp);
set_magic_quotes_runtime(get_magic_quotes_gpc());
$headers .= "\r\n"; 
$headers .= "--CCM-12345--\r\n\r\n\r\n"; 

echo "<pre>$headers</pre>";
//exit();

/* and now mail it */
if ( mail( $to, $subject, "", $headers ) ) {
    echo "sent";
}
else {
    echo "failed";
}
Mark
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Bech100,

WTF....it worked.....but what how can this be...it almost the same methodology...Y did it work and mines didn't....

I noted you did the following...please xplain...
set_magic_quotes_runtime(0); //why did you use this? are you suppose to?
$fp = fopen($file_source, "rb"); // whats with the 'rb'...are you suppose to read it like that..i used 'r'...what is the difference in this scenario.
$headers .= "Content-Type: image/jpeg; name=\"$file\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"$file\"\r\n\r\n";
// you have this as headers and not messages....y...was it suppose to go in the headers???
This is F&*king incredible...i have spent a week bangin my head against this.. i was about to give up hope....Bech1000 you are my saviour...thanks a lot...i mean it...thanks a very alot alot....

Your disciple...Kendall
[/quote]
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

set_magic_quotes_runtime(0); //why did you use this? are you suppose to?
get_magic_quotes runtime and set_magic_quotes_runtime are useful when you want to read some data in a binary file using fread() and some bytes in such file may be interpreted as \ (backslash), " (double quotes), ' (simple quote) or any "special" character that has a meaning for string processing.

$fp = fopen($file_source, "rb"); // whats with the 'rb'...are you suppose to read it like that..i used 'r'...what is the difference in this scenario.
From the manual - Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

$headers .= "Content-Type: image/jpeg; name=\"$file\"\r\n";
$headers .= "Content-Transfer-Encoding: base64\r\n";
$headers .= "Content-Disposition: attachment; filename=\"$file\"\r\n\r\n";
// you have this as headers and not messages....y...was it suppose to go in the headers???
Yup, that is header stuff

Glad to help.

Mark

[/quote]
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

kendall wrote:Bech100,

This is F&*king incredible...i have spent a week bangin my head against this.. i was about to give up hope....Bech1000 you are my saviour...thanks a lot...i mean it...thanks a very alot alot....

Your disciple...Kendall
might as well get your master right. It's bech100 :D.

-Nay
Post Reply