私的AI研究会 > SendMail
Pythonを使ってメール送信の機能を検証する。
Python にはメール送信を行うための機能として、SMTP クライアントのライブラリ「smtplib」が標準で提供されている。
メール送信時 MIME形式のデータを扱う必要あるが「email」ライブラリが標準で提供されている。
参考引用ページ → Pythonでのメール送信
# -*- coding: utf-8 -*- ##------------------------------------------ ## Send Mail with Python Ver 0.01 ## ## 2022.09.14 Masahiro Izutsu ##------------------------------------------ ## mail.py import ssl from smtplib import SMTP, SMTP_SSL from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.utils import formatdate def createMIMEText(from_mail, to, message, subject, filepath=None, filename=""): # MIMETextを作成 msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = from_mail msg['To'] = to msg.attach(MIMEText(message, 'plain', 'utf-8')) # 添付ファイルの設定 if filepath: path = filepath with open(path, 'r') as fp: attach_file = MIMEText(fp.read(), 'plain') attach_file.add_header( "Content-Disposition", "attachment", filename=filename ) msg.attach(attach_file) return msg def send_email(msg): account = "aipromotion999@gmail.com" password = "XXXXXXXX" host = 'smtp.gmail.com' port = 465 # サーバを指定する # server = SMTP(host, port) context = ssl.create_default_context() server = SMTP_SSL(host, port, context=context) # ログイン処理 server.login(account, password) # メールを送信する server.send_message(msg) # 閉じる server.quit() if __name__ == '__main__': # メールの送り主 from_email = "aipromotion999@gmail.com" # メール送信先 to_email = "XXXXXXXXX@XXXXXX.ne.jp" subject = "メールのテスト" message = "Python によるメール送信のテストです。" # MIME形式の作成 mime = createMIMEText(from_email, to_email, message, subject) # メールの送信 send_email(mime)
(py37) $ cd ~/workspace_py37/jtalk (py37) $ python3 mail.py Traceback (most recent call last): File "mail.py", line 71, in <module> send_email(mime) File "mail.py", line 50, in send_email server.login(account, password) File "/home/XXXXXX/anaconda3/envs/py37/lib/python3.7/smtplib.py", line 730, in login raise last_exception File "/home/XXXXXX/anaconda3/envs/py37/lib/python3.7/smtplib.py", line 721, in login initial_response_ok=initial_response_ok) File "/home/XXXXXX/anaconda3/envs/py37/lib/python3.7/smtplib.py", line 642, in auth raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials o28-20020aa7979c000000b0053e8940c4absm8705028pfp.209 - gsmtp')
(py37) $ python3 mail.py・Windowsの場合
(py37) PS > python mail.py
MailServer: account: aipromotion999@gmail.com enable: 'No' from: aipromotion999@gmail.com host: smtp.gmail.com password: **** port: 465 to: XXXXXXXX@docomo.ne.jp
(py37w) PS > cd \anaconda_win\workspace_py37\face_rec (py37w) PS > python .\my_mail.py my_mail:94 [DEBUG]: MailServer account : 'aipromotion999@gmail.com' my_mail:95 [DEBUG]: MailServer password : '****' my_mail:96 [DEBUG]: MailServer host : 'smtp.gmail.com' my_mail:97 [DEBUG]: MailServer port : '465' my_mail:98 [DEBUG]: MailServer from : 'aipromotion999@gmail.com' my_mail:99 [DEBUG]: MailServer to : 'XXXXXXXX@docomo.ne.jp' my_mail:105 [INFO]: subject : 'メールのテスト12' my_mail:106 [INFO]: message : 'Python によるメール送信のテスト【その2】です hostname : HP-ELITE800-OS from : aipromotion999@gmail.com to : XXXXXXXX@docomo.ne.jp'