我应该如何在同一正文中发送文本格式和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()
答案 0 :(得分:1)
您需要将消息创建为
def assignment():
x = input('Type anything')
random_list = []
while x != '':
x = input("Type anything")
random_list.append(x)
print(random_list)
x=x+1
for i in random_list:
if i not in random_list:
random_list.append(i)
assignment()
,然后附加两个MIMEText部分。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in assignment
TypeError: can only concatenate str (not "int") to str
已收到:
import numpy as np
from numpy import gradient
import h5py
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator
f = h5py.File('k.h5', 'r')
list(f.keys())
dset = f[u'data']
dset.shap
dset.value.shape
dset[0:64, 0:64, 0:64]
x = np.linspace(-160, 160, 64)
y = np.linspace(-160, 160, 64)
z = np.linspace(-160, 160, 64)
my_interpolating_function = RegularGridInterpolator((x, y, z), dset.value)
pts = np.array([100, 5, -10])
my_interpolating_function(pts)
# Apply gradient function
gradx, grady, gradz = np.gradient(dset.value)
gradx.shape
# To find the gradient at any point
gradx_interpol = RegularGridInterpolator((x, y, z), gradx)
grady_interpol = RegularGridInterpolator((x, y, z), grady)
gradz_interpol = RegularGridInterpolator((x, y, z), gradz)
def get_val_and_grads(pts):
v1, x1, y1, z1 = my_interpolating_function(pts), gradx_interpol(
pts), grady_interpol(pts), gradz_interpol(pts)
return v1, x1, y1, z1
##getting_interpolated_values
k1 = my_interpolating_function(pts)
k_dx = gradx_interpol(pts)
k_dy = grady_interpol(pts)
k_dz = gradz_interpol(pts)
def a_1(x,y,z):
return -(adot/a**2)*k1
def a_2(x,y,z):
return (1/a)*k_dx
经过重做的电子邮件程序包(Python 3.6+)可用于发送相同的消息,如下所示:
MIMEMultiPart('alternative')
输出:
>>> text = 'Hello World'
>>> html = '<p>Hello World</p>'
>>> msg = MIMEMultipart('alternative')
>>> msg['Subject'] = 'Hello'
>>> msg['To'] = 'a@example.com'
>>> msg['From'] = 'b@example.com'
>>> msg.attach(MIMEText(text, 'plain'))
>>> msg.attach(MIMEText(html, 'html'))
>>> s.sendmail('a@example.com', 'b@example.com', msg.as_string())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 's' is not defined
>>> s = smtplib.SMTP('localhost:1025')
>>> s.sendmail('a@example.com', 'b@example.com', msg.as_string())
答案 1 :(得分:0)
在您的“与”处:
def temp_message(filename):
with open(filename,'r') as fl1:
l2=fl1.read()
将其更改为:
def temp_message(filename):
filename = temp_message('temp1.txt') #changed tmp1 to filename
with open(filename, 'w+', encoding='utf-8') as fl1:
fl1.write(text)
fl1.write(html)
fl1.write(regards)
您可以仅拆分文本变量的'regards'部分,以便html(table)可以位于两者之间。我对您的问题很困惑(进行了大量编辑),但是如果我没有记错,您的fl1(tempt1.txt)没有任何数据,则您只能“读取”(r)文本文件,但没有什么都不要写。我还建议您将'tmp1 = temp_message('temp1.txt')'放入您的 'def temp_message(filename)'以避免混淆。