Python3 日本語でメール送信
import smtplib from email.mime.text import MIMEText from email.header import Header from email.utils import formatdate FROM_ADDR = 'myAcount@gmail.com' TO_ADDR = 'other@live.jp' ENCODING = 'iso-2022-jp' message = MIMEText( '本文', 'plain', ENCODING, ) message['Subject'] = Header('件名', ENCODING) message['From'] = '%s <%s>' % (Header('自分の名前', ENCODING), FROM_ADDR ) message['To'] = '%s <%s>' % ( Header('相手の名前', ENCODING), TO_ADDR ) message['Date'] = formatdate() s = smtplib.SMTP('smtp.gmail.com', 587) s.ehlo() s.starttls() s.ehlo() s.login('myAccount@gmail.com', 'pathword') s.sendmail( FROM_ADDR, [TO_ADDR], message.as_string(), ) s.close()
参考:Python3.0 の smtplib.py がそのままだと使えない - EAGLE 雑記
Pythonフォルダ/Lib/smptlib.py
def send(self, s): """Send `s' to the server.""" if self.debuglevel > 0: print('send:', repr(s), file=stderr) if hasattr(self, 'sock') and self.sock: if isinstance(s, str): # s = s.encode("ascii") s = s.encode("iso-2022-jp") #ideg:変更 ... def encode_plain(user, password): # s = "\0%s\0%s" % (user, password) # return encode_base64(s.encode('ascii'), eol='') return encode_base64("\0{0}\0{1}".format(user,password).encode(), eol='') #ideg:変更 ... def sendmail(self, from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]): self.ehlo_or_helo_if_needed() esmtp_opts = [] if isinstance(msg, str): # msg = _fix_eols(msg).encode('ascii') msg = _fix_eols(msg).encode('iso-2022-jp')#ideg:変更
Pythonフォルダ/Lib/email/header.py
- 'us-ascii' を 'iso-2022-jp' に変更する