Python 3 EmailMessage html message with Image
<p>I am trying to get images shown as part of the mail sent with Python. There is a example on Python docs which is not working.</p>
<pre><code>from datetime import datetime
import sys

import smtplib

from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid



from email.mime.image import MIMEImage

attachment = '/user/file/test.png'


import email.policy
msg = EmailMessage()
msg['To'] = Address(&quot;Srujan&quot;, 'myemail@example.com')
msg['From'] = Address(&quot;Srujan&quot;, 'myemail@example.com')
msg['Subject'] = &quot;Nice message goes with it &quot;+str(datetime.now())
id = make_msgid()
msg.set_type('text/html')
msg.set_content(&quot; This is the Data Message that we want to send&quot;)
html_msg = &quot;&lt;br&gt; &lt;b&gt;&lt;u&gt; This is the Text .... &lt;/u&gt;&lt;/b&gt;&lt;br&gt; &lt;img src='cid:{image_id}' /&gt;&quot;.format(image_id=id[1:-1])
msg.add_alternative(html_msg, subtype=&quot;html&quot;)
image_data = open(attachment, &quot;rb&quot;)
image_mime = MIMEImage(image_data.read())
image_data.close()
msg.add_attachment(image_mime,   cid=id, filename = &quot;myown.png&quot; ,)


try:
    with smtplib.SMTP('example.com') as s:
        s.ehlo()
        s.starttls()
        s.ehlo()
        
        s.send_message(msg)
        s.close()
    print(&quot;Email sent!&quot;)
except:
    print(&quot;Unable to send the email. Error: &quot;, sys.exc_info()[0])
    raise
</code></pre>
<p>I noticed that the last part is a <code>message/rfc822</code>, which then contains the <code>image/png</code>.</p>
<blockquote>
<pre class="lang-none prettyprint-override"><code>To: Srujan &lt;&quot;myemail@example.com&quot;&gt; From: Srujan
&lt;&quot;myemail@example.com&quot;&gt; Subject: Nice message goes with it 2016-01-21
17:39:23.642762 MIME-Version: 1.0 Content-Type: multipart/mixed;
boundary=&quot;===============3463325241650937591==&quot;

--===============3463325241650937591==
Content-Type: multipart/alternative; boundary=&quot;===============0739224896516732414==&quot;

--===============0739224896516732414==
Content-Type: text/plain; charset=&quot;utf-8&quot;
Content-Transfer-Encoding: 7bit

 This is the Data Message that we want to send

--===============0739224896516732414==
Content-Type: text/html; charset=&quot;utf-8&quot;
Content-Transfer-Encoding: 7bit MIME-Version: 1.0

&lt;br&gt; &lt;b&gt;&lt;u&gt; This is the Text .... &lt;/u&gt;&lt;/b&gt;&lt;br&gt; &lt;img
src='cid:20160122013923.64205.76661' /&gt;

--===============0739224896516732414==--

--===============3463325241650937591==
Content-Type: message/rfc822
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename=&quot;myown.png&quot;
Content-ID: &lt;20160122013923.64205.76661&gt;
MIME-Version: 1.0

Content-Type: image/png
MIME-Version: 1.0
Content-Transfer-Encoding: base64

iVBORw0KGgoAAAANSUhEUgAAAyAAAAJYCAIAAAAVFBUnAAAABmJLR0QA/wD/AP+gvaeTAAAgAElE
QVR4nOzdd2BUVfYH8HNfmZJAChAEQ1FAqoKgCAsWLEgTG4gKP11FQNG1AauIgGBXVhRUsCK4i6gI
</code></pre>
</blockquote>
<p>Now the attached message has two content-type values. Email comes with just text and with no image.</p>
<p>I have done it successfully with <code>MultiPart</code> class, but looking to achieve that with <code>EmailMessage</code>.</p>