如何使用Python发送带有.csv附件的电子邮件 [英] How do I send an email with a .csv attachment using Python

查看:580
本文介绍了如何使用Python发送带有.csv附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我知道有一些问题在解决这个问题,但我找不到一种方法使其正常工作。我会假设它和下面的代码一样简单,但是这并不附加我的文件。任何帮助将不胜感激。我也是Python的新手。是否有一个邮件模块,我应该导入以使功能正常工作?

  import smtplib 
fromaddr =example @ example.com
toaddrs =reciever@example.com

msg =帮助我无法发送附件以挽救我的生命
attach =(csvonDesktp.csv )

username = user
password = password

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls ()
server.login(username,password)
server.sendmail(fromaddr,toaddrs,msg,attach)
server.quit()


解决方案

发送带有相应MIME类型的多部分电子邮件。



https:// docs.python.org/2/library/email-examples.html



所以可能这样(我测试过):

  import smtplib 
import mimetypes
from email.mime.multipart import MIMEMultipart
从电子邮件导入编码器
从email.message import消息
从email.mime.audio导入MIMEAudio
从email.mime.base导入MIMEBase
从email.mime.image导入MIMEImage
从电子邮件。
emailto =destination@example.com
fileToSend =hi.csv
username =user
password =password

msg = MIMEMultipart()
msg [From] = emailfrom
msg [To] = mailto
msg [主题] =帮助我无法发送附件以拯救我的生命
msg.preamble =帮助我无法发送附件以挽救我的生命

ctype,encod ing = mimetypes.guess_type(fileToSend)
如果ctype为None或编码不是None:
ctype =application / octet-stream

maintype,subtype = ctype.split (/,1)

如果maintype ==text:
fp = open(fileToSend)
#注意:我们应该处理计算字符集
附件= MIMEText(fp.read(),_subtype =子类型)
fp.close()
elif maintype ==image:
fp = open(fileToSend,rb)
attachment = MIMEImage(fp.read(),_subtype = subtype)
fp.close()
elif maintype ==audio:
fp = open(fileToSend,rb )
attachment = MIMEAudio(fp.read(),_subtype = subtype)
fp.close()
else:
fp = open(fileToSend,rb)
attachment = MIMEBase(maintype,subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
附件。 add_header(Content-Disposition,attachment,filename = fileToSend)
msg.attach(attachment)

server = smtplib.SMTP(smtp.gmail.com:587)
server.starttls()
server.login(用户名,密码)
服务器.sendmail(emailfrom,emailto,msg.as_string())
server.quit()


Okay, I know there is a few questions out there addressing this, but I cannot find a way to make it work properly. I would assume it is as simple as the below code, but this does not attach my file. Any help would be greatly appreciated. I am also very new to Python. Is there a mail module that I should be importing to make the function work?

import smtplib
fromaddr = "example@example.com
toaddrs = "reciever@example.com

msg = "help I cannot send an attachment to save my life"
attach = ("csvonDesktp.csv")

username = user
password = password

server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg, attach)
server.quit()

解决方案

Send a multipart email with the appropriate MIME types.

https://docs.python.org/2/library/email-examples.html

So possible something like this (I tested this):

import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText

emailfrom = "sender@example.com"
emailto = "destination@example.com"
fileToSend = "hi.csv"
username = "user"
password = "password"

msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "help I cannot send an attachment to save my life"
msg.preamble = "help I cannot send an attachment to save my life"

ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
    ctype = "application/octet-stream"

maintype, subtype = ctype.split("/", 1)

if maintype == "text":
    fp = open(fileToSend)
    # Note: we should handle calculating the charset
    attachment = MIMEText(fp.read(), _subtype=subtype)
    fp.close()
elif maintype == "image":
    fp = open(fileToSend, "rb")
    attachment = MIMEImage(fp.read(), _subtype=subtype)
    fp.close()
elif maintype == "audio":
    fp = open(fileToSend, "rb")
    attachment = MIMEAudio(fp.read(), _subtype=subtype)
    fp.close()
else:
    fp = open(fileToSend, "rb")
    attachment = MIMEBase(maintype, subtype)
    attachment.set_payload(fp.read())
    fp.close()
    encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
msg.attach(attachment)

server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()

这篇关于如何使用Python发送带有.csv附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆