如何发送电子邮件附件?

我在理解如何使用Python向附件发送电子邮件时遇到问题。 我已经成功通过smtplib发送了简单的邮件。 有人可以解释如何发送电子邮件中的附件。 我知道在网上有其他的post,但作为一个Python初学者,我发现他们很难理解。

这是另一个,从这里改编:

 import smtplib from os.path import basename from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate def send_mail(send_from, send_to, subject, text, files=None, server="127.0.0.1"): assert isinstance(send_to, list) msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach(MIMEText(text)) for f in files or []: with open(f, "rb") as fil: part = MIMEApplication( fil.read(), Name=basename(f) ) # After the file is closed part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f) msg.attach(part) smtp = smtplib.SMTP(server) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() 

这和第一个例子差不多,但是应该更容易一些。

这是我最终使用的代码:

 import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email import Encoders SUBJECT = "Email Data" msg = MIMEMultipart() msg['Subject'] = SUBJECT msg['From'] = self.EMAIL_FROM msg['To'] = ', '.join(self.EMAIL_TO) part = MIMEBase('application', "octet-stream") part.set_payload(open("text.txt", "rb").read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="text.txt"') msg.attach(part) server = smtplib.SMTP(self.EMAIL_SERVER) server.sendmail(self.EMAIL_FROM, self.EMAIL_TO, msg.as_string()) 

代码和Oli的文章非常相似。 谢谢大家

基于二进制文件电子邮件附件问题post的代码。

这里是Oli for python 3的修改版本

 import smtplib, os from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.utils import COMMASPACE, formatdate from email import encoders def send_mail( send_from, send_to, subject, text, files=[], server="localhost", port=587, username='', password='', isTls=True): msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime = True) msg['Subject'] = subject msg.attach( MIMEText(text) ) for f in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(f,"rb").read() ) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f))) msg.attach(part) smtp = smtplib.SMTP(server, port) if isTls: smtp.starttls() smtp.login(username,password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.quit() 
 from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText from email.MIMEImage import MIMEImage import smtplib msg = MIMEMultipart() msg.attach(MIMEText(file("text.txt").read())) msg.attach(MIMEImage(file("image.png").read())) # to send mailer = smtplib.SMTP() mailer.connect() mailer.sendmail(from_, to, msg.as_string()) mailer.close() 

改编自这里 。

我能得到的最简单的代码是:

 #for attachment email from django.core.mail import EmailMessage def attachment_email(request): email = EmailMessage( 'Hello', #subject 'Body goes here', #body 'MyEmail@MyEmail.com', #from ['SendTo@SendTo.com'], #to ['bcc@example.com'], #bcc reply_to=['other@example.com'], headers={'Message-ID': 'foo'}, ) email.attach_file('/my/path/file') email.send() 

它基于官方的Django文档