Y
Y
Yura Khlyan2017-05-16 14:57:26
Django
Yura Khlyan, 2017-05-16 14:57:26

How to remove an image from attached files?

Good day.
I create a letter on my project and send it using Python. In the body of the letter there is a company logo, so that Google would display pictures, then I add them as follows:

def send_email(self, supply_order, new_confirmation, extra_text):
    ...
    email = EmailMultiAlternatives(
            subject,
            email_body,
            from_email=company.email,
            to=emails_to,
            cc=[company.email_copy]
        )
    
    self.add_files(email, supply_order, products_table, logo_path)
    email.send(fail_silently=False)

def add_files(self, email, supply_order, products_table, logo):
    ...
    email.attach('order.csv', csvfile.getvalue(), 'text/csv')
    email.attach('order.pdf', pdf.dest.getvalue(), 'application/pdf')

    if logo:
        fp = open(urllib.unquote(logo), 'rb')
        img = MIMEImage(fp.read())
        fp.close()
        img.add_header('Content-ID', '<logo>')
        email.attach(img)

If I open the letter in a browser, then everything is OK, but if I open it on a MAC via Mail, then the picture (logo) is displayed as an attached file. It should not be. How to remove it from there? Or how else to attach a picture to the letter?
Thanks in advance for advice.
===========UPD===========
Found that in the client on the MAC in the source of the letter, unlike the web client, there is a base64 representation of the logo:
Web:
--===============2904978512032783707==
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <logo>

--===============2904978512032783707==--

MAC Mail:
--===============2904978512032783707==
Content-Type: image/jpeg
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-ID: <logo>

TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24J1dCBieSB0
...
LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGhc3VyZS4=
--===============2904978512032783707==--

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Dubrovin, 2017-05-16
@MAGistr_MTM

For inline images, it is necessary that:
1. the image headers contain Content-Disposition: inline
2. the image is located inside the multipart/related part, which includes this image and the HTML part of the letter
3. the HTML part contains a link to the image and MIME-Version: 1.0 is not should be in the header of the part, only in the main header of the letter. <img src=cid:....>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question