有没有办法发送带有数据框附件的电子邮件?
Is there a way to send email with a dataframe attachment?
我目前有一个使用 pandas 操作 .csv 文件的脚本。我正在尝试发送一封 MIMEMultipart 电子邮件,其中包含已修改的文件的最新 .csv 版本,但出于某种原因,电子邮件收件人不断收到我尝试发送的较旧的未更改版本的 .csv。我试图在脑海中理解它,因为旧版本的 .csv 文件在发送之前已被覆盖,但原始版本的 .csv 已发送给收件人。
也许我需要为 smtplib 指定一个路径来获取文件,而不是仅仅给出文件名。有没有办法做到这一点,或者有其他方法可以解决我的问题?我已经尝试将名称更改为其他名称,以便 smtplib 能够区分旧的 .csv 和新的。
这不起作用,因为文件已放在目录中,但我的脚本显示新文件不存在
这是我当前的代码:
email_user = 'Bot@gmail.com'
email_password = 'Business101'
email_send = ('myemail@gmail.com', 'myfriendsemail@gmail.com')
subject = 'TOP 5 CONTRACTS'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = ",".join(email_send)
msg['Subject'] = subject
body = 'These are the latest contracts for this week!'
msg.attach(MIMEText(body,'plain'))
filename='CC.csv'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()
print("Emailed Recipients")
可能值得一提的是,这个过程是一个自动化过程,因此脚本是 运行 来自我 mac.
上的 Unix 可执行文件
如果您能提供帮助,我将不胜感激!
您实际上可以使用以下库来执行此操作:email.mime.application
、MIMEApplication, email.mime.multipart
和 email.mime.text
。现在,我不知道您使用的是哪个客户端,您可能需要对此做一些调整。
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
SEND_FROM = 'myaddress@client.com' ## Your email address
TOSEND = {'dataframe.csv': export_csv, 'dataframe.xlsx': export_my_excel}
def send_dataframe(send_to, subject, body, df):
multipart = MIMEMultipart()
multipart['From'] = SEND_FROM
multipart['To'] = send_to
multipart['Subject'] = subject
for filename in EXPORTERS:
attachment = MIMEApplication(TOSEND[filename](df)) ### Here is where the df is attached
attachment['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
multipart.attach(attachment)
multipart.attach(MIMEText(body, 'html'))
s = smtplib.SMTP('localhost')
s.sendmail(SEND_FROM, send_to, multipart.as_string())
s.quit()
在 send_dataframe()
中,您必须输入要附加的 df
。这就是 TOSEND 发挥作用的地方。如您所见,有一个名为 export_my_excel
的函数。您可以将其创建为
import io
import pandas as pd
def export_my_excel(df):
with io.BytesIO() as buffer:
writer = pd.ExcelWriter(buffer)
df.to_excel(writer)
writer.save()
return buffer.getvalue()
这是最好的方法,谢谢
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib
import sys
import pandas as pd
df_test = pd.read_csv('/Users/emmanuelafoke/Documents/Selenium/CC.csv')
email_user = 'myemailaddress@gmail.com'
email_password = 'mypassword'
recipients = ['theiremailaddress@gmail.com']
emaillist = [elem.strip().split(',') for elem in recipients]
msg = MIMEMultipart()
msg['Subject'] = 'SUBJECT'
msg['From'] = 'myemailaddress@gmail.com'
html = """\
<html>
<head></head>
<body>
{0}
</body>
</html>
""".format(df_test.to_html())
part1 = MIMEText(html, 'html')
msg.attach(part1)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(msg['From'], emaillist , msg.as_string())
感谢大家的帮助!
我目前有一个使用 pandas 操作 .csv 文件的脚本。我正在尝试发送一封 MIMEMultipart 电子邮件,其中包含已修改的文件的最新 .csv 版本,但出于某种原因,电子邮件收件人不断收到我尝试发送的较旧的未更改版本的 .csv。我试图在脑海中理解它,因为旧版本的 .csv 文件在发送之前已被覆盖,但原始版本的 .csv 已发送给收件人。
也许我需要为 smtplib 指定一个路径来获取文件,而不是仅仅给出文件名。有没有办法做到这一点,或者有其他方法可以解决我的问题?我已经尝试将名称更改为其他名称,以便 smtplib 能够区分旧的 .csv 和新的。
这不起作用,因为文件已放在目录中,但我的脚本显示新文件不存在
这是我当前的代码:
email_user = 'Bot@gmail.com'
email_password = 'Business101'
email_send = ('myemail@gmail.com', 'myfriendsemail@gmail.com')
subject = 'TOP 5 CONTRACTS'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = ",".join(email_send)
msg['Subject'] = subject
body = 'These are the latest contracts for this week!'
msg.attach(MIMEText(body,'plain'))
filename='CC.csv'
attachment =open(filename,'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+filename)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()
print("Emailed Recipients")
可能值得一提的是,这个过程是一个自动化过程,因此脚本是 运行 来自我 mac.
上的 Unix 可执行文件如果您能提供帮助,我将不胜感激!
您实际上可以使用以下库来执行此操作:email.mime.application
、MIMEApplication, email.mime.multipart
和 email.mime.text
。现在,我不知道您使用的是哪个客户端,您可能需要对此做一些调整。
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
SEND_FROM = 'myaddress@client.com' ## Your email address
TOSEND = {'dataframe.csv': export_csv, 'dataframe.xlsx': export_my_excel}
def send_dataframe(send_to, subject, body, df):
multipart = MIMEMultipart()
multipart['From'] = SEND_FROM
multipart['To'] = send_to
multipart['Subject'] = subject
for filename in EXPORTERS:
attachment = MIMEApplication(TOSEND[filename](df)) ### Here is where the df is attached
attachment['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
multipart.attach(attachment)
multipart.attach(MIMEText(body, 'html'))
s = smtplib.SMTP('localhost')
s.sendmail(SEND_FROM, send_to, multipart.as_string())
s.quit()
在 send_dataframe()
中,您必须输入要附加的 df
。这就是 TOSEND 发挥作用的地方。如您所见,有一个名为 export_my_excel
的函数。您可以将其创建为
import io
import pandas as pd
def export_my_excel(df):
with io.BytesIO() as buffer:
writer = pd.ExcelWriter(buffer)
df.to_excel(writer)
writer.save()
return buffer.getvalue()
这是最好的方法,谢谢
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import smtplib
import sys
import pandas as pd
df_test = pd.read_csv('/Users/emmanuelafoke/Documents/Selenium/CC.csv')
email_user = 'myemailaddress@gmail.com'
email_password = 'mypassword'
recipients = ['theiremailaddress@gmail.com']
emaillist = [elem.strip().split(',') for elem in recipients]
msg = MIMEMultipart()
msg['Subject'] = 'SUBJECT'
msg['From'] = 'myemailaddress@gmail.com'
html = """\
<html>
<head></head>
<body>
{0}
</body>
</html>
""".format(df_test.to_html())
part1 = MIMEText(html, 'html')
msg.attach(part1)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(msg['From'], emaillist , msg.as_string())
感谢大家的帮助!