LoginSignup
20
15

More than 5 years have passed since last update.

【Python】二段階認証設定しているgmailからメール送信

Last updated at Posted at 2017-08-20

gmailでアプリパスワードを設定する

https://support.google.com/accounts/answer/185833?hl=ja
こちらから設定するだけ。
16桁のアプリ専用パスワードが手に入る。

メールの送信サンプルコード

# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText

if __name__ == '__main__':
    to_addr = 'hoge@to.com'
    from_addr = 'fuga@from.com'
    mail_id = from_addr
    # 取得した16桁パスワードを入力する
    mail_pass = 'wwwwxxxxyyyyzzzz'

    message = MIMEText('Hello')
    message['Subject'] = 'Hello'
    message['From'] = from_addr
    message['To'] = to_addr

    sender = smtplib.SMTP_SSL('smtp.gmail.com')
    sender.login(mail_id, mail_pass)
    sender.sendmail(from_addr, to_addr, message.as_string())
    sender.quit()
20
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
15