[Python] Create API to send Gmail

I want a notification function, so I created a function to skip gmail, so I will describe it. The language will be python.

Premise By default,

gmail blocks login by python with security. You can unlock it by allowing the "Access to insecure apps" setting in your Gmail account. https://myaccount.google.com/security

This is a class for setting outgoing mail. Set the sender, destination, and gmail account passwords in the initialization, Set the content of the argument of the send method.

send_message.py


Learn more or give us feedback
from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib

class Send_Message:
    fromaddress = '[email protected]'
    toaddress = '[email protected]'
    password = 'password'

    def __init__(self, fromaddress, toaddress, password):
        self.fromaddress = fromaddress
        self.toaddress = toaddress
        self.password = password


    def send(self, content):
        smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
        smtpobj.ehlo()
        smtpobj.starttls()
        smtpobj.ehlo()
        smtpobj.login(self.fromaddress, self.password)

        msg = MIMEText(content)
        msg['Subject'] = 'subject'
        msg['From'] = self.fromaddress
        msg['To'] = self.toaddress
        msg['Date'] = formatdate()

        smtpobj.sendmail(self.fromaddress, self.toaddress, msg.as_string())
        smtpobj.close()

It will be the main process. Here, set the source, destination, password, content, If you execute it, you will be logged in to gmail and an email will be sent.

mail.py


from  send_message import Send_Message
import sys

def main():
    fromaddress = '@gmail.com'
    toaddress = '@gmail.com'
    password = ''
    content =""

    try:
        mail = Send_Message(fromaddress, toaddress, password)
        mail.send(content)
        print("success")
    except:
        print("error")

if __name__ == "__main__":
    main()

I have the source on github. https://github.com/kurihiro0119/gmail_send_API

Recommended Posts

[Python] Create API to send Gmail
Create Gmail in Python without API
Send Gmail in Python
Send using Python with Gmail
Send and receive Gmail via the Gmail API using Python
To automatically send an email with an attachment using the Gmail API in Python
I tried to create API list.csv in Python from swagger.yaml
Create Awaitable with Python / C API
I tried to create Quip API
Create folders from '01' to '12' with python
How to use OpenPose's Python API
5 Ways to Create a Python Chatbot
[Python] How to use Typetalk API
Send email via gmail with Python 3.4.3.
[September 2020 version] Explains the procedure to use Gmail API with Python
How to send a request to the DMM (FANZA) API with python
Automatically create Python API documentation with Sphinx
HTML email with image to send with python
Send a message from Python to Slack
Send email to multiple recipients in Python (Python 3)
Create Python module [CarSensor API support module csapi]
Send data to DRF API with Vue.js
[Python] Quickly create an API with Flask
API explanation to touch mastodon from python
Connect to coincheck's Websocket API from Python
Updated to Python 2.7.9
Challenge to create time axis list report with Toggl API and Python
[Python] Send email
[Python] Send email
"Backport" to python 2
Send a message from Slack to a Python server
Edit Excel from Python to create a PivotTable
Send a message to LINE with Python (LINE Notify)
How to create a Python virtual environment (venv)
Procedure to use TeamGant's WEB API (using python)
Send an email to Spushi's address with python
I want to email from Gmail using Python.
Easy to create API server using go-json-rest module
[python] Create table from pandas DataFrame to postgres
Send Gmail at the end of the process [Python]
Sample to send slack notification with python lambda
Steps to create a Twitter bot with python
Get Gmail subject and body with Python and Gmail API
How to create a Rest Api in Django
[Python] Mention to multiple people with Slack API
Create an API server quickly with Python + Falcon
I want to send Gmail with Python, but I can't because of an error
[BigQuery] How to use BigQuery API for Python -Table creation-
[First API] Try to get Qiita articles with Python
How to install Python
Create a Python module
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Blender x Python] How to create an original object
Create SpatiaLite in Python
Rewrite Python2 code to Python3 (2to3)
Create a plugin to run Python Doctest in Vim (2)
How to install python
python decorator to retry
Introduction to Python language
Create a plugin to run Python Doctest in Vim (1)