Jump to content

Sending HTML email with attachments not working in Gmail


brooksh

Recommended Posts

My code works, unless I send it to gmail, then the attachment doesn't show up, and the body shows: This is a multi-part message in MIME format.

 

I'm assuming my headers are incorrect. 

 

 
 $from = stripslashes($from_name)."<".stripslashes($from_email).">";
  $uniqid   = md5(uniqid(time()));
  $boundary = "--==_mimepart_".$uniqid;


  $headers = "From: ".$from."\n".
  'Subject: '.$subject."\n".
  'Reply-to: '.$from_email."\n".
  'Return-Path: '.$from_email."\n".
  'Message-ID: <'.$uniqid.'@'.$domain.">\n".
  'Date: '.date("r", strtotime("now"))."\n".
  'Mime-Version: 1.0'."\n".
  'Content-Type: text/html;'."\n".
  '  boundary=PHP-mixed-'.$boundary.";\n".
  '  charset=UTF-8'."\n".
  'X-Mailer: PHP/' . phpversion();
$headers .= 'Content-Type: multipart/mixed; boundary="'.$uniqid.'"\r\n\r\n';
$headers .= 'This is a multi-part message in MIME format.\r\n';
$headers .= '--'.$uniqid.'\r\n';
$headers .= 'Content-type:text/html; charset=iso-8859-1\r\n';
$headers .= 'Content-Transfer-Encoding: 7bit\r\n\r\n';


   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
$message = $html;
      $count_uploaded_files = count( $uploaded_files['attachments']['tmp_name'] );
 if($uploaded_files['attachments']['name'][0] != ""){


  $headers .= "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";    
   $message .= "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/html; charset=\"UTF-8\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";
for( $i = 0; $i < $count_uploaded_files; $i++ )
{
    $tmp_name = $uploaded_files['attachments']['tmp_name'][$i];
    $type     = $uploaded_files['attachments']['type'][$i];
    $name     = $uploaded_files['attachments']['name'][$i];
    $size     = $uploaded_files['attachments']['size'][$i];
      if (file_exists($tmp_name)){
         if(is_uploaded_file($tmp_name)){
            $file = fopen($tmp_name,'rb');
            $data = fread($file,filesize($tmp_name));
            fclose($file);
            $data = chunk_split(base64_encode($data));
         }
         $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
            "Content-Disposition: attachment;\n" .
            " filename=\"{$fileatt_name}\"\n" .
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
}  
 }
$headers .= "--".$uniqid."--";
   $message.="--{$mime_boundary}--\n";
   }//end files sent


mail($to_addresses, $subject, $message, $headers);
Link to comment
Share on other sites

Doing it as suggested with PHPMailer will make your life much more enjoyable.  By the time you (possibly) figure out how to do this with the php mail function, you could have downloaded it and installed the package and sent your email successfully.  Really.

Link to comment
Share on other sites

Then that's your problem, isn't it?

 

The code you've posted is wrong on every single level. Even if you manage to find somebody who's masochistic enough to debug this mess, that doesn't get you anywhere. You're just polishing a turd. That's why you've been offered a valid approach which actually solves the underlying problem. You don't want that? Then enjoy the code you have.

Link to comment
Share on other sites

Sorry master PHP script writer. Glad there are jerks like you on this forum. 

 

Then that's your problem, isn't it?

 

The code you've posted is wrong on every single level. Even if you manage to find somebody who's masochistic enough to debug this mess, that doesn't get you anywhere. You're just polishing a turd. That's why you've been offered a valid approach which actually solves the underlying problem. You don't want that? Then enjoy the code you have.

Link to comment
Share on other sites

I don't understand what you meant by your statement - "I'm doing something else".  What does that even mean??

 

And your snarky comment to Jacques is totally uncalled for.  You are being given heartfelt good advice to avoid the out-date and simplistic PHP mail() function for something that is a recognized leader in mail programs and you give back nothing.

 

If you don't want good advice and don't want to explain why you don't like it, then why do you even seek out advice from people like us?

 

I give you -1 for your attitude.

Link to comment
Share on other sites

If you want to do it yourself, fine. But you have to learn about the proper syntax for multipart messages in emails - I suggest the RFC. For real. Not just make it up as you go. Because I'm very surprised what you produced there works in any email client.

Link to comment
Share on other sites

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.