I have the following script that is supposed to send an email; however, the email is being sent as a plain text rather than HTML. Am I missing a piece of code?
import smtplib, ssl, mimetypes
from email.message import EmailMessage
from email.utils import make_msgid
def send_email():
server = connect_server()
if server:
html = """\
<html>
<body>
<div style="width:60%;background-color:#193048;border:4px solid #3c71aa;padding:5px 10px">
Putting My Email Text Here!
</div>
</body>
</html>
"""
msg = EmailMessage()
msg["Subject"] = "Subject"
msg["From"] = "Support <support@example.xyz>"
msg["To"] = "{} <{}>".format("Test Human","<test.human@somewhere.xyz>")
msg.set_content('Plain Text Here!')
msg_image = make_msgid(domain="example.xyz")
msg.add_alternative(html.format(msg_image=msg_image[1:-1],subtype="html"))
with open("./resources/email-logo.png","rb") as fp:
maintype,subtype = mimetypes.guess_type(fp.name)[0].split('/')
msg.get_payload()[1]add_related(fp.read(),maintype=maintype,subtype=subtype,cid=msg_image)
server.sendmail("Support <support@example.xyz","{} <{}>".format(Test Human,test.human@somewhere.xyz),msg.as_string())
server.quit()
I am using Python3.9 on Ubuntu 18.04. Thanks All!