- Windows7
- Python2.7.13
import os.path
import datetime
import smtplib
import codecs, collections
import tempfile
import zipfileJPN as zipfile
import unittest, time, re, os, sys,datetime, shutil
import MimeWriter, mimetools, base64, StringIO
from email import Encoders
from email.Utils import formatdate
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
def create_message(from_addr, from_addr_name, to_addr, cc_addr, subject, body, mime, attach_file):
zf = tempfile.TemporaryFile(prefix='mail', suffix='.zip')
zip = zipfile.ZipFile(zf, 'w')
files = os.listdir(attach_file['path'])
for i in files:
filename =""
filename=i
zip.write(attach_file['path']+"/"+(filename.decode("cp932")).encode("cp932"))
zip.close()
zf.seek(0)
cset = 'utf-8'
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = from_addr_name+"<"+from_addr+">"
msg["To"] = to_addr
msg["Cc"] = cc_addr
msg["Date"] = formatdate()
body = MIMEText(body.encode("utf-8"), 'plain', 'utf-8')
msg.attach(body)
attachment = MIMEBase(mime['type'],mime['subtype'])
attachment.set_payload(zf.read())
Encoders.encode_base64(attachment)
msg.attach(attachment)
attachment.add_header("Content-Disposition","attachment", filename=attach_file['name'])
return msg
def send(from_addr, to_addrs, cc_addrs, msg):
smtp = smtplib.SMTP("localhost:host")#Änderung erforderlich
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
if __name__ == '__main__':
from_addr="[email protected]"
from_addr_name = "Prüfung"
to_addr = "[email protected],[email protected]"
cc_addr = "[email protected],[email protected]"
subject = u"Prüfung"
body = u"Prüfung"
mime={'type':'application', 'subtype':'zip'}
attach_file={'name':'FileName.zip', 'path':'D://test/File'}
msg = create_message(from_addr, from_addr_name, to_addr, cc_addr, subject, body, mime, attach_file)
send(from_addr,to_addr_list, cc_addr_list, msg)
def send(from_addr, to_addrs, cc_addrs, msg):
smtp = smtplib.SMTP("localhost:host")#Änderung erforderlich
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
--Schreiben Sie die Dateien im Zielordner im Zip-Format --MIME-Typ einstellen und als Anhang festlegen
zf = tempfile.TemporaryFile(prefix='mail', suffix='.zip')
zip = zipfile.ZipFile(zf, 'w')
files = os.listdir(attach_file['path'])
for i in files:
filename =""
filename=i
zip.write(attach_file['path']+"/"+(filename.decode("cp932")).encode("cp932"))
zip.close()
zf.seek(0)
import zipfileJPN as zipfile
Recommended Posts