Page 1 of 1

Arrrrgh email encoding please help.

Posted: Thu Dec 04, 2003 5:52 pm
by leoden
Hello everyone having a bit of problem!

I recently created my comapny website which, with a membership system, sends out a welcome HTML message when you register and also ( if requested ) sends out a monthly sale catalogue ( a word .doc file).

So I created all the code for this specifying all the headers & using the mail() function etc etc. After a while a couple of complaints came back from people saying the .doc file was unencoded garble. This confused me as testing it to my own email address it worked fine. After checking the file encoding method which was ("application/msword") and generally asking around I couldnt see any problem. So I changed the system to use the PEAR classes think it would create less room for error with all those '/r/n' gone.

After implementation I'm still having problems below is an example of how I would send out a catalogue using the PEAR Classes.

Note/ for readability two arrays; $row, containing customer details and $saledetails, containing sale detals

Code: Select all

$mail_to = $row['email'];										
$mail_subject =  $saledetails['SaleType'] . " Sale catalogue " . $saledetails['SaleNumber']; 							
$mail_body = "Dear " . $row['title'] . " " . $row['surname'] . ",\r\n\r\n";
$mail_body .= "As requested, attached is the catalogue for our upcoming " . $saledetails['SaleType'] . " sale to be held on " . date("l dS of F Y",$SaleDate) . "\r\n\r\n";			
$mail_body .= "Yours Sincerely\r\n\r\n";
$mail_body .= "Online Services\r\n\r\r";			
$catalogue_filename = "../Catalogues/" . $_POST['SaleNumber'] . ".doc";
$hdrs = array('From' => 'online.services@denhams.com','Subject' => $mail_subject);
// Declare Mime Object
$crlf = "\r\n";
$mime = new Mail_mime($crlf);    			
$mime->setTXTBody($mail_body);
$mime->addAttachment($catalogue_filename,"application/msword");
		
$body = $mime->get();
$hdrs = $mime->headers($hdrs);
$mail =& Mail::factory('mail');
 
$mail_result = $mail->send($_POST['email_override'], $hdrs, $body);
Any ideas would be greatly appreciated!!!

Posted: Fri Dec 05, 2003 3:16 am
by JayBird
if you are only getting complaints from a few people, it may just be THEIR mail client and nothing to do with your script.

Mark

Posted: Fri Dec 05, 2003 3:46 am
by leoden
I appreciate that mate, I thought the same thing but I was wondering if there is any other option, can you confirm the two other options I've herd/read

1) For the welcome letter that is pure HTML, I don't specify a text body alternative, If i did this does the email client revert to the text version if it cant decipher the encoded HTML?

2) Is an encoding type of 'Octet-Stream' a viable alternative?

Oh, on checking one of the replies from my clients he stated that he uses the full version of outlook

Thanks

Posted: Fri Dec 05, 2003 3:52 am
by JayBird
dunno if this help, but this is how i send file attachements

Code: Select all

<?php
// Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  // Generate a boundary string
  $semi_rand = md5(time());
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
  
  // Add the headers for a file attachment
  $headers .= "\nMIME-Version: 1.0\n" .
              "Content-Type: multipart/mixed;\n" .
              " boundary="{$mime_boundary}"";

  // Add a multipart boundary above the plain message
  $message = "This is a multi-part message in MIME format.\n\n" .
             "--{$mime_boundary}\n" .
             "Content-Type: text/plain; charset="iso-8859-1"\n" .
             "Content-Transfer-Encoding: 7bit\n\n" .
             $message . "\n\n";

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name="{$fileatt_name}"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename="{$fileatt_name}"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\n\n" .
              "--{$mime_boundary}--\n";
}

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
  echo "<p>Mail sent! Yay PHP!</p>";
} else {
  echo "<p>Mail could not be sent. Sorry!</p>";
}
?>

Posted: Fri Dec 05, 2003 4:01 am
by leoden
Cheers for that, but thats exactly what I was doing before using the PEAR classes, oh well I sure Ill come up with something!!!

Posted: Fri Dec 05, 2003 12:59 pm
by m3mn0n
Let's hope. :)