2

with Help of this Question Send attachments with PHP Mail()? I fixed many problems with my E-Mail. The Gmail shows the correct mail with the image viewed in the HTML, but in Thunderbird it only shows the image and no html. If I check the source-code of the mail it also looks correct.

what can I do?

$img_src = "km.png"; //about 79KB
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);

send("name@example.com", "Test Subject", $img_str);

function send($receiver, $subject, $content){

    $header  = "MIME-Version: 1.0" . "\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"aboundary\"" . "\n";

    $header .= "--aboundary". "\n";
    $header .= "Content-Type: multipart/alternative; boundary=\"xboundary\"". "\n\n";

    $header .= "--xboundary". "\n".
                "Content-Type: text/html; charset=utf-8". "\n".
                "Content-Transfer-Encoding: 8bit". "\n";

    $header .= "\n".    
    "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title></title></head><body>"."\n".
//  "<h1>Hello World</h1><img src=\"cid:0123456789\" alt=\"no img\" /><p>Some Testcontent with umlauts ÖÄÜß</p>". "\n". //this shell work in step 2 as well
    "<h1>Hello World</h1><img src=\"cid:0123456789\" alt=\"no img\" /><p>Some Testcontent without umlauts.</p>". "\n".
    "</body></html>"."\n\n";
    $header .= "--xboundary". "\n".
                "Content-Type: image/png; name=\"att.png\"". "\n".
                "Content-Disposition: inline; filename=\"att.png\"". "\n".
                "Content-Transfer-Encoding: base64". "\n".
                "Content-ID: <0123456789>". "\n".
                "Content-Location: att.png". "\n".
                "\n".
                chunk_split($content, 64).
                "\n".
                "--xboundary--";
    mail($receiver, $subject, "" , $header);
}
Community
  • 1
  • 1
helle
  • 11,183
  • 9
  • 56
  • 83
  • try replacing all `\n` with `\r\n`. its what the specs say. see here for info on that: http://stackoverflow.com/questions/4415654/which-line-break-in-php-mail-header-r-n-or-n – x4rf41 Jul 01 '13 at 14:22
  • Make your life easier by using an existing library for this eg [swiftmailer](http://swiftmailer.org/) or [Pear Mail_Mime](http://pear.php.net/package/Mail_Mime/redirected) – WayneC Jul 01 '13 at 14:24
  • @WayneC I know but I want to know how it works this way ;) – helle Jul 01 '13 at 14:28
  • @x4rf41 I tried that. No changes... – helle Jul 01 '13 at 14:35
  • @helle did you try `PHP_EOL` instead of `\r\n`? – Pragnesh Chauhan Jul 02 '13 at 10:00
  • yes I tried it. And I found out that also with swift-mailer, thunderbird is not able to show the image in the html ... – helle Jul 02 '13 at 10:07

1 Answers1

1
    $img_src = "km.png"; //about 79KB
    //$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
    //$img_str = base64_encode($imgbinary);

    $to= "name@example.com";
    $subject = "Test Subject";



        $headers = "From:  youremail@somewhere.com \r\n";
        $headers .= "Reply-To:  youremail@somewhere.com \r\n";
        $headers .= "MIME-Version: 1.0\r\n";

        $headers .= "Content-Type: text/html" . "\r\n";
        $headers .= "Content-Transfer-Encoding: base64" . "\r\n";

        $message ="<html><head><title></title></head><body><h1>Hello World</h1><img src='".$img_src."' alt='no img' /><p>Some Testcontent with umlauts ÖÄÜß</p>
        <h1>Hello World</h1><img src='".$img_src."' alt='no img' /><p>Some Testcontent without umlauts.</p>
        </body></html>";

        $base64contents = rtrim(chunk_split(base64_encode($message)));             
                     $success = mail($to, $subject, $base64contents, $headers);
                    if (!$success){
                        echo "Something Went Wrong";} 
Marinus
  • 441
  • 5
  • 11
  • can you send me an e-mail with that solution? I am pretty sure I tried this but I think I got problems with it... mailto: 3017556@gmail.com – helle Nov 19 '13 at 16:14
  • Hey Marinus, thanks for the E-Mail. As I thought, I get the: "show extern content" Button with this variant in Thunderbird ... – helle Nov 21 '13 at 10:59