LoginSignup
1
1

More than 3 years have passed since last update.

GmailでPythonを使って送信する

Posted at

結論


import smtplib
from email.utils import formatdate
from os.path import basename
from email.mime.text import MIMEText
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart


EMAIL_SENDER_ADDRESS = ''
EMAIL_SENDER_PASSWORD = ''

def sendEmail(message, subject):
    #送り主
    from_email = EMAIL_SENDER_ADDRESS
    from_password = EMAIL_SENDER_PASSWORD

    # 送り先
    to_email = [Env.EMAIL_RECIEVE_ADDRESS]

    # メールの内容
    msg = MIMEMultipart()
    body = MIMEText(message, "html")
    msg.attach(body)
    msg["Subject"] = subject
    msg["To"] = ",".join(to_email)
    msg["From"] = from_email

    gmail = smtplib.SMTP("smtp.gmail.com", 587)
    gmail.starttls()
    gmail.login(from_email, from_password)
    gmail.send_message(msg)
1
1
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
1
1