'Sending email with embedded image using SMTP

I'm trying to send an email using the SMTP library with an embedded image in the body. The email gets sent, but the resulting image is a textbox saying 'This image cannot be displayed, it was possibly removed or renamed...' etc. I know there is nothing wrong with the image itself as I can open the image locally just fine. The path and image both are valid, so I don't understand why it cannot be displayed within the email. I read somewhere that I may be because of the Content-ID being too short, but I have no idea how to find a solution for that.

This is the code I have for sending the email in the method send_email()

def send_email():
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Subject"
    msg['From'] = from_address
    msg['To'] = to_address

    text = MIMEText('<img src="cid:image1">', 'html')
    msg.attach(text)

    image = MIMEImage(open('pictures/download.png', 'rb').read())

    image.add_header('Content-ID', '<ímage1>')
    msg.attach(image)

    try:
        s = smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
        s.starttls()
        s.login(from_address, password)
        s.sendmail(from_address, to_address, msg.as_string())
        s.quit()
        print('Email sent!')

    except Exception as ex:
        print('Error:', ex)

So I'm guessing that there is a problem with the Content-ID, but I'm not sure. Has anyone else encountered this?



Solution 1:[1]

People have mixed results with Content-ID. Very few people use it these days.

(Base 64 is not widely supported either, for that matter: https://www.caniemail.com/features/image-base64/)

You should host the image on a public server and provide the full URL path to the image in the src attribute for it to work, i.e. <img src="https://www.path.com/to/image/image.jpg" width="100">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nathan