- Windows7
- Python2.7.13
--Envoyer du courrier en utilisant python --Définir plusieurs personnes pour la destination et Cc
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")#Changement requis
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
if __name__ == '__main__':
from_addr="[email protected]"
from_addr_name = "tester"
to_addr = "[email protected],[email protected]"
cc_addr = "[email protected],[email protected]"
subject = u"tester"
body = u"tester"
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")#Changement requis
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
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