Send using Python with Gmail

Conclusion


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):
    #Sender
    from_email = EMAIL_SENDER_ADDRESS
    from_password = EMAIL_SENDER_PASSWORD

    #Destination
    to_email = [Env.EMAIL_RECIEVE_ADDRESS]

    #The contents of the e-mail
    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)

Recommended Posts

Send using Python with Gmail
Send email via gmail with Python 3.4.3.
Send email with Python
Send Gmail in Python
Send Japanese email with Python3
[S3] CRUD with S3 using Python [Python]
Using Quaternion with Python ~ numpy-quaternion ~
[Python] Using OpenCV with Python (Basic)
Using OpenCV with Python @Mac
Send and receive Gmail via the Gmail API using Python
To automatically send an email with an attachment using the Gmail API in Python
Complement python with emacs using company-jedi
Harmonic mean with Python Harmonic mean (using SciPy)
[Python] Using OpenCV with Python (Image Filtering)
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
[Automation] Send Outlook email with Python
Notes on using rstrip with python.
Send multipart / form-data with python requests
Easily send emails with Gmail with Django
Precautions when using six with Python 2.5
Send image with python, save with php
[Python] Create API to send Gmail
[Python] Send gmail with python: Send one by one with multiple image files attached
[Python] Send an email from gmail with two-step verification set
[AWS] Using ini files with Lambda [Python]
FizzBuzz with Python3
Scraping with Python
HTML email with image to send with python
Statistics with python
Try mathematical formulas using Σ with python
Behind the flyer: Using Docker with Python
Scraping with Python
Python with Go
Send newsletters all at once with Gmail
Using Python and MeCab with Azure Databricks
Socket communication using socketserver with python now
[Python] Send email
Twilio with Python
Start using Python
Play with 2016-Python
Check stock prices with slackbot using python
Tested with Python
[Python] Send email
Tips for using python + caffe with TSUBAME
with syntax (Python)
Bingo with python
Send an email with Amazon SES + Python
I'm using tox and Python 3.3 with Travis-CI
Scraping using Python
Excel with Python
Microcomputer with Python
Cast with python
Send a message to LINE with Python (LINE Notify)
Email attachments using your gmail account in python.
Send an email to Spushi's address with python
I want to email from Gmail using Python.
Debug with VS Code using boost python numpy
I tried using mecab with python2.7, ruby2.3, php7
Send Gmail at the end of the process [Python]