2

I have a function which send images to the email

The requirements is, i have only two images the first one i need to send as a attachments and another one in the body .

Using alternative in the MIMEmultipart it sending the both images as a documents and i have tried using two multipart that is also not helping. let me know how to approach the issue and also let me know whether it is possible or not

Any idea would be appreciated

Jerry Vfc
  • 75
  • 7

2 Answers2

3

According to HTML-Email with inline attachments and non-inline attachments, the MIME way is to build an inner mime/related message containing both the HTML text and the inline image(s), and an outer one containing the mime/related message and the other attachment(s).

Your code could become

...
message_body = """<html>
<body><p>Please keep in touch and reach out to us for any help needed.</p>
<image src="cid:image"/></body></html>"""
msg = MIMEMultipart("mixed")

message_body = """<html>
<body><p>Please keep in touch and reach out to us for any help needed.</p>
<image src="cid:image"/></body></html>"""
msg = MIMEMultipart("mixed")
msg['From'] = username
msg['To'] = ','.join(to)
msg['Subject'] = subject

body = MIMEText(message_body, \
                'html', 'utf-8')

inner = MIMEMultipart("related")
inner.attach(body)
msg.attach(inner)
...
    image = MIMEImage(img_data, name=os.path.basename(x))
    image.add_header('Content-Id', 'image')
    inner.attach(image)
    
    image_1 = MIMEImage(img_data_1, name=os.path.basename(y))
    msg.attach(image_1)
    ...

After @triplee's comment, I gave a try to the EmailMessage API. It comes with far more black magic, so things are much simpler if less explicit:

from email.message import EmailMessage
from imghdr import what
...
message_body = """<html>
<body><p>Please keep in touch and reach out to us for any help needed.</p>
<image src="cid:image"/></body></html>"""
msg = EmailMessage()

message_body = """<html>
<body><p>Please keep in touch and reach out to us for any help needed.</p>
<image src="cid:image"/></body></html>"""
msg = MIMEMultipart("mixed")
msg['From'] = username
msg['To'] = ','.join(to)
msg['Subject'] = subject

msg.set_content(message_body, subtype='html')
...
    msg.add_related(img_data, 'image', what(x), cid='image')
    
    msg.add_attachment(img_data_1, 'image', what(y),
                       filename=os.path.basename(y))
    ...
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • As an aside, your code seems to be written for Python 3.5 or earlier. The `email` library was overhauled in 3.6 and is now quite a bit more versatile and logical. Probably throw away what you have and start over with the [examples from the `email` documentation.](https://docs.python.org/3/library/email.examples.html) – tripleee Aug 18 '22 at 09:16
  • @triplee: You are right, but the documentation of the new library is by far less exhaustive... I shall give it try, and add how it could be done using `EmailMessage`... – Serge Ballesta Aug 18 '22 at 11:28
0

You can use HTML to embed the wanted image in the body.

Some e-mail clients interpret this still as an attachment. Maybe check, how other clients interpret your solution.

benicamera
  • 750
  • 1
  • 7
  • 24