Jump to content

HTML Email with Attachment being received differently by mail programs


Calamity-Clare

Recommended Posts

Hi guys,

I'm trying to write a script that generates a multipart plaintext/HTML email with a pdf attachment. After much research and trial & error, I seem to have reached a wall.

 

At the moment, I am testing in gmail, hotmail and Outlook 2003. Gmail displays the HTML alternative of the message and attaches the pdf document, while hotmail & Outlook only attach the pdf without displaying either of the message alternatives.

 

Here is the code:

 // Error display
  ini_set ('display_errors', 1);
  error_reporting (E_ALL | E_STRICT);

// Setting a timestamp
  date_default_timezone_set('Australia/Perth');
  $timestamp = date("d/m/y H:i:s", time());
  
  // Create a boundary string. It must be unique, so we use the MD5 algorithm to generate a random hash 
  $random_hash = md5(time()); 
  
  // Read the attachment file contents into a string, encode it with MIME base64 & split it into smaller chunks
  $attachment = chunk_split(base64_encode(file_get_contents("success.pdf")));
  
  // Create the Plain text message to be sent in the email body
  $content_text = "Hello, World!! \nIs this a Plain Text alternative?";
  
  // Create the HTML message to be sent in the email body
  $content_html = "<html><body><h1>Hello, World!!</h1><p>This is <b>HTML</b> formatting.</p></body></html>";
  
  // Sending the email
  $to = "$email";
  $subject = "Test5-3.php :: $timestamp"; 
  
  $headers = "From: The Company <[email protected]>\n";
  $headers .= "MIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed; boundary=\"$random_hash\"\n";
  $headers .= "--$random_hash\n";
  $headers .= "Content-Type: application/pdf\n";
  $headers .= "Content-Transfer-Encoding: base64\n";
  $headers .= "Content-Disposition: attachment; filename=\"success.pdf\"\n\n";
  $headers .= "$attachment\n";
  
  $message = "Content-Type: multipart/alternative; boundary=\"$random_hash\"\n";
  $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
  $message .= "Content-Transfer-Encoding: 7bit\n"; 
  $message .= "$content_text\n";
  $message .= "--$random_hash\n";
  $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; 
  $message .= "Content-Transfer-Encoding: 7bit\n"; 
  $message .= "$content_html\n";
  $message .= "--$random_hash--";
  
  // Send the data in an email
  $mail_sent = @mail ($to, $subject, $message, $headers, "[email][email protected][/email]");

 

(That's not the full script, but it is the relevant part of it. The full script is attached.)

 

Other variations of this code I have tried out include:

    - surrounding all the variables with ". .", eg:

  $headers .= "--".$random_hash."\n";

    - sending the entire function as headers, instead of splitting it into message and headers, eg:

  $headers = "From: The Company <[email protected]>\n";
  $headers .= "MIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed; boundary=\"$random_hash\"\n";
  $headers .= "--$random_hash\n";
  $headers .= "Content-Type: application/pdf\n";
  $headers .= "Content-Transfer-Encoding: base64\n";
  $headers .= "Content-Disposition: attachment; filename=\"success.pdf\"\n\n";
  $headers .= "$attachment\n";
  $headers .= "Content-Type: multipart/alternative; boundary=\"$random_hash\"\n";
  $headers .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n"; 
  $headers .= "Content-Transfer-Encoding: 7bit\n"; 
  $headers .= "$content_text\n";
  $headers .= "--$random_hash\n";
  $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n"; 
  $headers .= "Content-Transfer-Encoding: 7bit\n"; 
  $headers .= "$content_html\n";
  $headers .= "--$random_hash--";

 

Does anyone know why I would be having this problem? It seems that nothing I've tried will work!!

 

Thanks

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.