Question

I'm sending HTML mails with swiftmailer. It all works pretty great, my HTML mails are showing up just fine in all clients. I have quite the knowledge of building email templates. Sending them myself not so much...

The problem exists in gmail and hotmail. The image is not showing up. It shows just fine in Thunderbird for instance... gmail is leaving my image blank and adds it as an attachment (at the bottom of the mail, like i'm sending birthday pictures..).

Any ideas?

I've got the following code:

function deliverMail($subject, $data, $from, $to, $template){
    if($_SERVER['SERVER_ADDR'] !== '::1'){

        if (!is_array($to)){
            $to = array($to);       
        }           
        $from = explode(",", $from);        

        //Create the Transport
        $transport = Swift_SmtpTransport::newInstance('mail.xxx.nl', 25)
          ->setUsername('xxx')
          ->setPassword('xxx');           

        $mailer = Swift_Mailer::newInstance($transport);
        $message = Swift_Message::newInstance($subject);
        $image = $message->embed(Swift_Image::fromPath($_SERVER['DOCUMENT_ROOT'].'/templates/emails/xxx.jpg'));


        // open the file
        $sourcefile     = $_SERVER['DOCUMENT_ROOT'].'/templates/emails/'.$template.'.html.tpl';
        $fh             = fopen($sourcefile, 'r');
        $htmltemplate       = fread($fh, filesize($sourcefile));
        fclose($fh);

        // set the content  
        if($template == 'confirm'){$replaceOld = array("[*code*]", "[*imgsrc*]");}          
        if($template == 'notify'){$replaceOld = array("[*url*]", "[*imgsrc*]");}

        $replaceNew     = array($data, $image);         
        $body           = str_replace($replaceOld, $replaceNew, $htmltemplate);         



        $message->setFrom(array($from[0] => $from[1]))
        ->setTo($to)
        ->setBody($body, 'text/html');


        if (!$mailer->send($message, $failures)){
          return "Failures:";
          return print_r($failures);
        }else{
            return 'success';
        }

    }
}

Which will render the image in my template here:

<td id="head" height="80" width="640" colspan="3" valign="top" align="left">
<image src="[*imgsrc*]" width="140" height="80" alt="xxx"/>         
</td>

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top