O
O
onepunchman4042020-05-21 00:06:57
Python
onepunchman404, 2020-05-21 00:06:57

How to send email via HTML code in Python?

Greetings, I encountered such a problem, there is an html code - a letter template, there is a Python code that sends messages. So Here's how I can paste the code from this file so that the html email template is sent.

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()

message = 'Test_01'
password = "password"
msg['From'] = "mail"
msg['Subject'] = "Test_02"
msg['To'] = 'adress'

msg.attach(MIMEText(message, 'plain')
server = smtplib.SMTP('smtp.yandex.ru: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Sobolev, 2020-05-21
@Sobolev5

import email.message
import smtplib

msg = email.message.Message()
msg['Subject'] = 'foo'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg.add_header('Content-Type','text/html')
msg.set_payload('Body of <b>message</b>')

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
s.starttls()
s.login(email_login,
        email_passwd)
s.sendmail(msg['From'], [msg['To']], msg.as_string())
s.quit()

https://stackoverflow.com/questions/882712/sending...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question