{"id":53,"date":"2013-08-01T15:24:08","date_gmt":"2013-08-01T09:24:08","guid":{"rendered":"http:\/\/sarojroy.com\/?p=53"},"modified":"2013-08-01T15:31:00","modified_gmt":"2013-08-01T09:31:00","slug":"problem-in-embedding-images-in-swiftmail-within-html-content-having-plain-text-alternate","status":"publish","type":"post","link":"http:\/\/sarojroy.com\/blog\/2013\/08\/01\/problem-in-embedding-images-in-swiftmail-within-html-content-having-plain-text-alternate\/","title":{"rendered":"Problem in embedding images in Swiftmailer within html content having plain-text alternate"},"content":{"rendered":"<p>Swiftmailer is a very good library for sending email from PHP. It has nice object oritented interfaces. However, in recent days I&#8217;ve spent a lot of time finding the reason why the content get messed up in Swiftmailer when<\/p>\n<ul>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">The email has both html and alternate text content<\/span><\/li>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">Embed image in html content<\/span><\/li>\n<\/ul>\n<p>Specification wise, mime structure for HTML with embedded images and plain text fallback is :<\/p>\n<div>\n<div>\n<pre>multipart\/alternative\r\n  text\/plain\r\n  multipart\/related\r\n    text\/html\r\n    image\/png\r\n    image\/png\r\n    image\/jpeg<\/pre>\n<\/div>\n<\/div>\n<p>Swiftmailer has a bug and fails to generate correct mime type entries. The<span style=\"line-height: 1.714285714; font-size: 1rem;\">\u00a0issue is reported <\/span><a style=\"line-height: 1.714285714; font-size: 1rem;\" href=\"https:\/\/github.com\/swiftmailer\/swiftmailer\/issues\/56\">here<\/a>\u00a0. Thankfully there is a workaround for that.<\/p>\n<p>My previous code was:<\/p>\n<pre class=\"brush: php; gutter: true\">$message = Swift_Message::newInstance();\r\n\r\n$message-&gt;setSubject(&#039;Your subject&#039;)\r\n        -&gt;setFrom(array(&#039;me@mydomain.com&#039; =&gt; &#039;Saroj Roy&#039;))\r\n        -&gt;setTo(array(&#039;saroj@saroj-HP-ProBook-4530s&#039;))\r\n        -&gt;setBody(&#039;This is a plain text Message&#039;,&#039;text\/plain&#039;)       \r\n        -&gt;addPart(&#039;Here is the &lt;b&gt;HTML message&lt;\/b&gt; with image : &lt;img src=&quot;&#039; .\r\n                  $message-&gt;embed(Swift_Image::fromPath(&#039;image.png&#039;)).\r\n                  &#039;&quot; alt=&quot;Image Missing&quot; \/&gt;&#039;, &#039;text\/html&#039;);\r\n\r\n$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());\r\n\r\n$result = $mailer-&gt;send($message);<\/pre>\n<p>I had to change the setBody() to addPart(). This change generates correct mime-type entries in message. However, this is still not 100% full-proof. Problem still occurs in MS Office 2003 and Thunderbird email client when content is viewed as text. These clients convert the html content to text by striping the html-tags out instead of showing the plain-text content. I suppose there are some bugs in these email clients.\u00a0Please let me know it I were wrong with my assumptions.<\/p>\n<p>By the way, Gmail and console based email client Mutt shows the content correctly.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swiftmailer is a very good library for sending email from PHP. It has nice object oritented interfaces. However, in recent days I&#8217;ve spent a lot of time finding the reason why the content get messed up in Swiftmailer when The email has both html and alternate text content Embed image in html content Specification wise, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,17],"tags":[19,24,18],"_links":{"self":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts\/53"}],"collection":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":9,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":57,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions\/57"}],"wp:attachment":[{"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sarojroy.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}