I am trying to send an email with html code. In it I place my image. But when receiving a letter in the mail, the image is not displayed.
<div >
<img src="mysite/logo.svg" alt="logo" >
</div>
However, it does not appear in the email.
<img src="https://ci6.googleusercontent.com/proxy/c33cuVIg8CI8ogTZFezJa6bgoZ97KqPgefyR9YPF6vSGfi1zqQFXkx3AMyB0h8hD338LoBPvMR7JTyIt3F0Y=s0-d-e1-ft#https://sherf201.pythonanywhere.com/logo.svg" alt="logo" data-bit="iit" jslog="138226; u014N:xr6bB; 53:W2ZhbHNlLDJd">
And I get this line in the Img tag. My email sending code
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
try:
server.login(EMAIL_BOT, PASSWORD_BOT)
msg = MIMEMultipart('alternative')
part1 = MIMEText(message, 'html')
msg.attach(part1)
msg['From'] = EMAIL_BOT
msg['To'] = email
msg["Subject"] = name
server.sendmail(EMAIL_BOT, email, msg.as_string())
except Exception as ex:
print(ex)
How do I send an image in my html code?
CodePudding user response:
SVG is not supported in many email clients (Outlook as well). If you try to insert SVG files in Outlook manually you may find that they are converted to JPEG images.
Another aspect is that internet based images (hosted on the server anywhere) are blocked by default in most emails clients. To avoid that you need to add an image as an attachment and then set its Content-ID
MIME header. After that you can refer to the attached image in the message body (HTML) using the following markup:
<img src="cid:MyContenttId">
CodePudding user response:
I wanted to add a code to the answer that allows you to implement the ability to send an image in a letter.
server.login(EMAIL_BOT, PASSWORD_BOT)
msg = MIMEMultipart('alternative')
part1 = MIMEText(message, 'html')
msg.attach(part1)
img_data = open('filename', 'rb').read()
img = MIMEImage(img_data, name = os.path.basename('filename'))
img.add_header('Content-ID', '<image>'.format('filename'))
msg.attach(img)
msg['From'] = EMAIL_BOT
msg['To'] = email
msg["Subject"] = name
server.sendmail(EMAIL_BOT, email, msg.as_string())
Then in html:
<div >
<img src="cid:image" alt="logo" >
</div>