Page 1 of 1

sending email with jpeg attachemnt

Posted: Thu Sep 11, 2003 6:53 pm
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

Posted: Sun Sep 14, 2003 7:33 am
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.. :)...)

sending email with jpeg attachemnt

Posted: Mon Sep 15, 2003 8:35 am
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!!!!

Posted: Mon Sep 15, 2003 9:03 am
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

Posted: Mon Sep 15, 2003 9:08 am
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]

Posted: Mon Sep 15, 2003 9:16 am
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

Posted: Mon Sep 15, 2003 9:33 am
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]

Posted: Mon Sep 15, 2003 9:37 am
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]

Posted: Mon Sep 15, 2003 9:38 am
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