add inline image to a message sent with swiftmailer

后端 未结 5 1873
温柔的废话
温柔的废话 2021-02-08 02:46

Please excuse my php but, Im using Swiftmailer to send emails from a clients website. They\'ve requested to add an image or two as a signature etc and so looking at the swiftmai

5条回答
  •  一整个雨季
    2021-02-08 03:12

    None of the answers really worked for me. I had to include inline images, using CID. What I had to do, to make it work:

    $attachment = Swift_Image::newInstance($data, $filename, $mimeType)
        ->setDisposition('inline');
    
    $cid = $message->embed($attachment); // Generates "cid:something"
    

    Important part is using Swift_Image class. Then the image in html should be:

    
    

    I think this solution works without doing something hacky with swiftmailer (ver. 5.4.2). It is exactly how the documentation says.

    Remember to test multiple email clients (gmail, thunderbird, apple mail, web clients...) if the inline images work. For example using Swift_Attachment, inline images showed in gmail, but not in some web clients. Using Swift_Image, it worked everywhere.

提交回复
热议问题