我已经浏览了很多教程,以及有关堆栈溢出的其他问题,文档和解释至少是,只是无法解释的代码。我想发送一个我已压缩的文件,并将其作为附件发送。我已经尝试复制和粘贴提供的代码,但它不起作用,因此我无法解决问题。
所以我要问的是,如果有人知道谁解释smtplib以及电子邮件和MIME库如何协同发送文件,更具体地说,如何使用zip文件。任何帮助将不胜感激。
这是每个人都提到的代码:
import smtplib
import zipfile
import tempfile
from email import encoders
from email.message import Message
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
def send_file_zipped(the_file, recipients, sender='you@you.com'):
myzip = zipfile.ZipFile('file.zip', 'w')
# Create the message
themsg = MIMEMultipart()
themsg['Subject'] = 'File %s' % the_file
themsg['To'] = ', '.join(recipients)
themsg['From'] = sender
themsg.preamble = 'I am not using a MIME-aware mail reader.\n'
msg = MIMEBase('application', 'zip')
msg.set_payload(zf.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',
filename=the_file + '.zip')
themsg.attach(msg)
themsg = themsg.as_string()
# send the message
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail(sender, recipients, themsg)
smtp.close()
我怀疑问题是这个代码也会压缩文件。我不想拉链,因为我已经有了一个我想发送的压缩文件。在任何一种情况下,这些代码都没有很好的文档和python库本身,因为它们没有提供任何有关img文件和文本文件的信息。
更新:我现在收到错误。我还使用上面的代码更新了我文件中的内容
Traceback (most recent call last):
File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 100, in <module>
send_file_zipped('hw5.zip', 'avaldez@oswego.edu')
File "/Users/Zeroe/Documents/python_hw/cgi-bin/zip_it.py", line 32, in send_file_zipped
msg.set_payload(myzip.read())
TypeError: read() takes at least 2 arguments (1 given)
答案 0 :(得分:9)
我真的没有看到问题。只需省略创建zip文件的部分,而只需加载您拥有的zip文件。
基本上,这部分是
msg = MIMEBase('application', 'zip')
msg.set_payload(zf.read())
encoders.encode_base64(msg)
msg.add_header('Content-Disposition', 'attachment',
filename=the_file + '.zip')
themsg.attach(msg)
创建附件。在
msg.set_payload(zf.read())
将附件的有效负载设置为您从文件zf
读取的内容(可能意味着zip文件)。
请事先打开您的zip文件,然后从中读取该行。
答案 1 :(得分:0)
我同意电子邮件包尚未完整记录。之前我调查了它并编写了一个简化这些任务的包装器模块。例如,以下工作:
from pycopia import ezmail
# Get the data
data = open("/usr/lib64/python2.7/test/zipdir.zip").read()
# Make a proper mime message object.
zipattachement = ezmail.MIMEApplication.MIMEApplication(data, "zip",
filename="zipdir.zip")
# send it.
ezmail.ezmail(["Here is the zip file.", zipattachement],
To="me@mydomain.com", From="me@mydomain.com", subject="zip send test")
一旦安装完所有内容,就可以满足您的需求。 : - )
答案 2 :(得分:0)
我的回答使用 shutil
压缩包含附件的目录,然后将 .zip
添加到电子邮件中。
# Importing Dependencies
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
import shutil
def Send_Email():
# Create a multipart message
msg = MIMEMultipart()
Body = MIMEText( {Enter Email Body as str here} )
# Add Headers
msg['Subject'] = ''
msg['From'] = ''
msg['To'] = ''
msg['CC'] = ''
msg['BCC'] = ''
# Add body to email
msg.attach(Body)
# Using Shutil to Zip a Directory
dir_name = {Add Path of the Directory to be Zipped}
output_filename = {Add Output Zip File Path}
shutil.make_archive(output_filename, 'zip', dir_name)
part = MIMEBase("application", "octet-stream")
part.set_payload(open(output_filename + ".zip", "rb").read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename=\"%s.zip\"" % (output_filename))
msg.attach(part)