5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

速攻python3で簡単メール送信

Posted at

python3でメール送信

環境

  • モジュールはsmtplibを使用します。
  • SMTPはGメールを使用します。

ソース

main.py
import smtplib
from email.mime.text import MIMEText

TO_ADDRESS = "送信先アドレス@gmail.com"
FROM_ADDRESS = "送信元アドレス@gmail.com"
MY_PASSWORD = "送信元アドレスのパスワード"

def send_mail(msg):
    try:
        smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
        smtpobj.ehlo()
        smtpobj.starttls()
        smtpobj.ehlo()
        smtpobj.login(FROM_ADDRESS, MY_PASSWORD)
        smtpobj.sendmail(FROM_ADDRESS, TO_ADDRESS, msg.as_string())
        smtpobj.close()
        return True
    except:
        return False

if __name__ == '__main__':
    msg = MIMEText("テスト本文")
    msg['Subject'] = "テスト"
    msg['From'] = FROM_ADDRESS
    msg['To'] = TO_ADDRESS

    result = send_mail(msg)
    print(result)

使用方法

  1. モジュールのインストールpip3 install smtplib
  2. スクリプト実行python3 main.py

結果

True

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?