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 <webmaster@example.com>\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]-froot@clarebyrnedesign.com.au[/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 <webmaster@example.com>\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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.