使用smtp发送带有多个附件的邮件
Sending email with multiple attachments using smpt
我有这个脚本可以将带有多个附件的电子邮件发送给多个用户。但是,附件的文件名设置为其路径。
收到的文件
终端输出
如何将它们的名称设置为实际文件名,谢谢。
"""
import os
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase
from email import Encoders
#Set up crap for the attachments
files = "/tmp/test/dbfiles"
filenames = [os.path.join(files, f) for f in os.listdir(files)]
#print filenames
#Set up users for email
gmail_user = "joe@email.com"
gmail_pwd = "somepasswd"
recipients = ['recipient1','recipient2']
#Create Module
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(recipients)
msg['Subject'] = subject
msg.attach(MIMEText(text))
#get all the attachments
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
#send it
mail(recipients,
"Todays report",
"Test email",
filenames)
"""
这个link可能有解决办法:
基本上 post 的解决方案是改变你的这一行:
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
到这一行:
part.add_header('Content-Disposition', 'attachment', filename=AFileName)
最终的改变是:
#get all the attachments
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
***part.add_header('Content-Disposition', 'attachment', filename=file)***
msg.attach(part)
Documentation on how to use add_header
希望对您有所帮助! :D
更新
在你的 for 循环中包含这个应该会给你文件名:
for file in filenames:
actual_filenames = os.path.basename(file)
#Your code
part.add_header('Content-Disposition', 'attachment', filename=actual_filenames)
如果在同一目录下:
import os
for f in os.listdir('.'):
fn, fext = os.path.splitext(f)
我有这个脚本可以将带有多个附件的电子邮件发送给多个用户。但是,附件的文件名设置为其路径。
收到的文件
终端输出
如何将它们的名称设置为实际文件名,谢谢。
"""
import os
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage
from email.MIMEBase import MIMEBase
from email import Encoders
#Set up crap for the attachments
files = "/tmp/test/dbfiles"
filenames = [os.path.join(files, f) for f in os.listdir(files)]
#print filenames
#Set up users for email
gmail_user = "joe@email.com"
gmail_pwd = "somepasswd"
recipients = ['recipient1','recipient2']
#Create Module
def mail(to, subject, text, attach):
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(recipients)
msg['Subject'] = subject
msg.attach(MIMEText(text))
#get all the attachments
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, msg.as_string())
# Should be mailServer.quit(), but that crashes...
mailServer.close()
#send it
mail(recipients,
"Todays report",
"Test email",
filenames)
"""
这个link可能有解决办法:
基本上 post 的解决方案是改变你的这一行:
part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)
到这一行:
part.add_header('Content-Disposition', 'attachment', filename=AFileName)
最终的改变是:
#get all the attachments
for file in filenames:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(file, 'rb').read())
Encoders.encode_base64(part)
***part.add_header('Content-Disposition', 'attachment', filename=file)***
msg.attach(part)
Documentation on how to use add_header
希望对您有所帮助! :D
更新
在你的 for 循环中包含这个应该会给你文件名:
for file in filenames:
actual_filenames = os.path.basename(file)
#Your code
part.add_header('Content-Disposition', 'attachment', filename=actual_filenames)
如果在同一目录下:
import os
for f in os.listdir('.'):
fn, fext = os.path.splitext(f)