Re: mail() and pdf attachment

Re: mail() and pdf attachment

am 05.10.2007 07:14:41 von Michael

On Oct 4, 6:47 pm, damezumari wrote:
> I am trying to send a pdf file as an attachment with the mail()
> function. Unfortunately, the pdf file does not show up as an
> attachment. I get its coding as text along with this:
>
> MIME-Version: 1.0
> Content-Type: multipart/mixed;
> boundary="==Multipart_Boundary_x893cd7b85e299baba9cd7ab91338 4411x"
>
> --==Multipart_Boundary_x893cd7b85e299baba9cd7ab913384411x
> Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: 7bit
>
> this is the message
>
> --==Multipart_Boundary_x893cd7b85e299baba9cd7ab913384411x
> Content-Type: application/pdf;
> name="Test file for jmail.pdf"
> Content-Disposition: attachment;
> filename="Test file for jmail.pdf"
> Content-Transfer-Encoding: base64
>
> JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVy IC9GbGF0ZURlY29k
> ZT4+
>
> ... a lot omitted
>
> wMDU4NiAwMDAwMCBuIAowMDAwMDAxMDc1IDAw
> MDAwIG4gCjAwMDAwMDA3ODAgMDAwMDAgbiAKMDAwMDAwODg3MCAwMDAwMCBu IAp0cmFpbGVyCjw8
> IC9TaXplIDE1IC9Sb290IDEgMCBSIC9JbmZvIDIgMCBSCi9JRCBbPDhCNzc0 RDNGRkNGRDI2RjIw
> M0Q0REMyMjk5NjY4NTRDPjw4Qjc3NEQzRkZDRkQyNkYyMDNENERDMjI5OTY2 ODU0Qz5dCj4+CnN0
> YXJ0eHJlZgoxMDQ1MwolJUVPRgo=
>
> --==Multipart_Boundary_x893cd7b85e299baba9cd7ab913384411x--
>
> Can anyone tell me what is wrong?
>
> Regards,
>
> Jan Nordgreen

Uhmm I'm not sure, It's hard when you can't see the codes thats send
the email.
But here is PHP code to send a email with file attachment.

It's taken from my tutorial on wiki.greenquery.com


$to_email = "johndoe@hotmail.com";
$subject = "A file from my website";
$filename = $_SERVER["DOCUMENT_ROOT"] . "/files/
myzipfile.zip";
$filetype = "application/octet-stream";
$options = "From: janedoe@hotmail.com";
$text = "Hi John\n\nHere is the zip file i told
you about :-)";

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


//Read the file
$handle = fopen($filename, 'rb');
$filecontents = fread($handle, filesize($filename));
fclose($handle);

//base64 encode the file
$filecontents = @base64_encode($filecontents);

//Write the mime message
$options .= "\nMIME-Version: 1.0";
$options .= "\nContent-Type: multipart/mixed;\n
boundary=\"". $mime_boundary ."\"\n\n";

$content .= "This is a multi-part message in MIME
format.\n\n";
$content .= "--$mime_boundary\n";


// Adding the text
$content .= "Content-Type: text/html;
charset=iso-8859-1\n";
$content .= "Content-Transfer-Encoding: 8bit\n\n";
$content .= $text . "\n\n";

// Adding the file
$content .= "--". $mime_boundary . "\n";
$content .= "Content-Type: application/octet-stream;
name=\"" . basename($filename) . "\"\n";
$content .= "Content-Transfer-Encoding: base64\n\n";
$content .= chunk_split($filecontents) . "\n\n";
$content .= "--". $mime_boundary . "\n";


//Send the mail
mail($to_email,$subject,$content,$options);


I hope it was of some help to you

//Michael

Re: mail() and pdf attachment

am 05.10.2007 12:45:07 von damezumari

Hi Michael,

Thank you for answering!

$fromname = $_SESSION['myname'];
$fromaddress = $_SESSION['myemailaddress'];
$headers = "From: \"".$fromname."\" <".$fromaddress.">\n";
$bcc = $_SESSION['myemailaddress'];
$headers .= 'Bcc: '.$bcc. "\n";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

This was my offending code!

I had too many line shifts (\n) in $headers.

I changed the last line to:

$headers .= "MIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

and then it worked!

Regards,

Jan Nordgreen

Re: mail() and pdf attachment

am 05.10.2007 14:06:38 von Michael

On Oct 5, 12:45 pm, damezumari wrote:
> Hi Michael,
>
> Thank you for answering!
>
> $fromname = $_SESSION['myname'];
> $fromaddress = $_SESSION['myemailaddress'];
> $headers = "From: \"".$fromname."\" <".$fromaddress.">\n";
> $bcc = $_SESSION['myemailaddress'];
> $headers .= 'Bcc: '.$bcc. "\n";
> $headers .= "\nMIME-Version: 1.0\n" .
> "Content-Type: multipart/mixed;\n" .
> " boundary=\"{$mime_boundary}\"";
>
> This was my offending code!
>
> I had too many line shifts (\n) in $headers.
>
> I changed the last line to:
>
> $headers .= "MIME-Version: 1.0\n" .
> "Content-Type: multipart/mixed;\n" .
> " boundary=\"{$mime_boundary}\"";
>
> and then it worked!
>
> Regards,
>
> Jan Nordgreen

Ahh that explains a lot.
I'm glad to hear you got it to work ;-)

//Michael