Как мне отправить электронное письмо с текстовым и HTML-форматом в одном и том же теле? Какая польза от MIMEmultipart
?
MIMEMultipart([MIMEText(msg, 'text'),MIMEtext(html,'html')])
Я смог получить электронное письмо, используя это, но с пустым телом
PS: я пытаюсь отправить текст и прикрепить таблицу в том же теле. Я не хочу отправлять таблицу в виде вложения.
html = """
<html>
<head>
<style>
table, th, td {{ border: 1px solid black; border-collapse: collapse; }} th, td {{ padding: 5px; }}
</style>
</head>
<body><p>Hello, Friend This data is from a data frame.</p>
<p>Here is your data:</p>
{table}
<p>Regards,</p>
<p>Me</p>
</body>
</html> """
text = """
Hello, Friend.
Here is your data:
{table}
Regards,
Me"""
text = text.format(table=tabulate(df, headers=list(df.columns), tablefmt="grid"))
html = html.format(table=tabulate(df, headers=list(df.columns), tablefmt="html"))
if(df['date'][0].year==1900 and df['date'][0].month==datetime.date.today().month and df['date'][0].day==datetime.date.today().day):
a2=smtplib.SMTP(host='smtp-mail.outlook.com', port=587)
a2.starttls()
myadd='abc@gmail.com'
passwd=getpass.getpass(prompt='Password: ')
try :
a2.login(myadd,passwd)
except Exception :
print("login unsuccessful")
def get_contacts(filename):
name=[]
email=[]
with open('email.txt','r') as fl:
l=fl.readlines()
print(l)
print(type(l))
for i in l:
try:
name.append(i.split('\n')[0].split()[0])
email.append(i.split('\n')[0].split()[1])
except Exception:
break
fl.close()
return (name,email)
def temp_message(filename):
with open(filename,'r') as fl1:
l2=fl1.read()
return(Template(l2))
name,email=get_contacts('email.txt')
tmp1=temp_message('temp1.txt')
for name,eml in zip(name,email):
msg=MIMEMultipart([MIMEText(msg, 'text'),MIMEtext(html,'html')])
message=tmp1.substitute(USER_NAME=name.title())
print(message)
msg['FROM']=myadd
msg['TO']=eml
msg['Subject']="This is TEST"
msg.attach(MIMEText(message, 'plain'))
# msg.set_payload([MIMEText(message, 'plain'),MIMEText(html, 'html')])
# send the message via the server set up earlier.
a2.send_message(msg)
del msg
a2.quit()