Help us understand the problem. What is going on with this article?

速攻python3で簡単メール送信

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

kashih
ネイティブクラウド マイクロサービス AI,DX NodeJS,JavaScript(vue,react),python,
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした