add inline image to a message sent with swiftmailer

后端 未结 5 1863
温柔的废话
温柔的废话 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:04

    The accepted answer doesn't work (version tested: 5.4.2). (Mine works but could be perfected)

    Instead, looking inside the "Original" (Gmail: Show Original) I've found that swiftmailer is omitting adding 2 headers to the attachment, namely:

    Content-ID: 
    X-Attachment-Id: ABC123
    

    ABC123 is the cid we have to put in the body where we want the inline to be showed:

    So thanks to this question: I found the way to fix it for swiftmailer (that is even against swiftmailer documentation, but it works while theirs do not)

    this is the final (ugly) code:

    $attachment = Swift_Attachment::fromPath('image.jpg')->setDisposition('inline');
    $attachment->getHeaders()->addTextHeader('Content-ID', '');
    $attachment->getHeaders()->addTextHeader('X-Attachment-Id', 'ABC123');
    $cid = $message->embed($attachment);
    $img = '';
    $html = "
    
    
    
    
    $img
    
    
    ";
    $message->setBody($html, 'text/html');
    

提交回复
热议问题