Python의 smtplib를 사용하여 이메일 보내기
Python의 smtplib를 사용하여 이메일 보내기
1. 환경
- Windows7
- Python2.7.13
2. 하고 싶은 일
- pythonを使用してメールを送信する
- 宛先,Ccに複数人設定する
- zipファイルを添付する
- 添付ファイル名に日本語を使用する
3. 프로그램 코드
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")#要変更
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
if __name__ == '__main__':
from_addr="[email protected]"
from_addr_name = "テスト"
to_addr = "[email protected],[email protected]"
cc_addr = "[email protected],[email protected]"
subject = u"テスト"
body = u"テスト"
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)
4. 설명
1. python을 사용하여 메일 보내기
- Windows7
- Python2.7.13
- pythonを使用してメールを送信する
- 宛先,Ccに複数人設定する
- zipファイルを添付する
- 添付ファイル名に日本語を使用する
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")#要変更
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
if __name__ == '__main__':
from_addr="[email protected]"
from_addr_name = "テスト"
to_addr = "[email protected],[email protected]"
cc_addr = "[email protected],[email protected]"
subject = u"テスト"
body = u"テスト"
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")#要変更
smtp.sendmail(from_addr, to_addrs+cc_addrs, msg.as_string())
smtp.close()
2. 받는 사람,Cc에 여러 사람 설정
3. zip 파일 첨부
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)
4. 첨부 파일 이름에 일본어 사용
import zipfileJPN as zipfile
Reference
이 문제에 관하여(Python의 smtplib를 사용하여 이메일 보내기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kurikurisan/items/ba60d46db452a0a4f3d5텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)