Send an email to Spushi's address with python

This is a memo of the video. https://youtu.be/Pw_jSRyX4lQ

Preparation

http://console.developers.google.com/

  1. From the above URL, enable the API of "Google Sheets" and "Google Drive" and get the JSON file.
  2. Create a new folder
  3. Rename the JSON file to creds.json and put it in a folder
  4. Create a new file called send.py in the folder
  5. Enter the following command to complete the two installations
$ pip install gspread oauth2client

The contents of the code

send.py


#Email sending relations
from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib
#Google API cooperation
import gspread 
from oauth2client.service_account import ServiceAccountCredentials
from pprint import pprint 

scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)

client = gspread.authorize(creds)

#I'm getting the first sheet titled python.
# sheet1 = client.open("python").sheet1

SPREADSHEET_KEY = '18j7KiKC1C0VhqZV_CQ7tx5n7mZfR6teKENvEDcuyRe0'
sheet1= client.open_by_key(SPREADSHEET_KEY).worksheet('sheet1') 
sheet2= client.open_by_key(SPREADSHEET_KEY).worksheet('sheet2') 

#Email content
body = sheet2.cell(2,2).value
title = sheet2.cell(2,1).value

#All values are assigned to a variable called data.
data = sheet1.get_all_records()

#Get the number of data
last_number = len(data)

for row in range(last_number):
    #Extract the information required to send an email
    full_name = data[row]["name"]  #name
    msg = MIMEText(full_name + body)
    pprint(msg)
    
    msg['Subject'] = sheet2.cell(2,1).value
    msg['From'] = '**************@gmail.com'
    msg['To'] = data[row]["mail address"]  # mail address
    msg['Date'] = formatdate()

    smtp = smtplib.SMTP('smtp.gmail.com', 587)
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login('**************@gmail.com', '**************')
    smtp.send_message(msg)
    smtp.close()

Recommended Posts

Send an email to Spushi's address with python
[Python] Send an email with outlook
Send an email to a specific email address with python without SMTP settings
Send email with Python
Send an email with Amazon SES + Python
Send an email with Excel attached in Python
Send Japanese email with Python3
[Automation] Send Outlook email with Python
Note: Send an email with Django
To automatically send an email with an attachment using the Gmail API in Python
Send email via gmail with Python 3.4.3.
[Python] Send an email from gmail with two-step verification set
[Python] Send email
[Python] Send email
Send email to multiple recipients in Python (Python 3)
I tried sending an email with python.
Send a message to LINE with Python (LINE Notify)
How to crop an image with Python + OpenCV
Send email with Django
Sample to send slack notification with python lambda
Validate E-Mail with Python
I tried sending an email with SendGrid + Python
Post an article with an image to WordPress with Python
Output the report as PDF from DB with Python and automatically attach it to an email and send it
I want to send Gmail with Python, but I can't because of an error
Send an email directly by pulling an MX record from your email address in Python
An introduction to Python distributed parallel processing with Ray
Reading Note: An Introduction to Data Analysis with Python
I tried to implement an artificial perceptron with python
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Creating an egg with python
Send email in Python (Outlook)
Switch python to 2.7 with alternatives
Write to csv with Python
An introduction to Python Programming
Send using Python with Gmail
If you can't send an email with python smtplib and have trouble, command line
How to install NPI + send a message to line with python
I tried various methods to send Japanese mail with Python
I tried sending an email from Amazon SES with Python
How to convert an array to a dictionary with Python [Application]
Send experiment results (text and images) to slack with Python
Code to send an email based on the Excel email list
How to run an app built with Python + py2app built with Anaconda
[Python Kivy] How to create an exe file with pyinstaller
How to read an Excel file (.xlsx) with Pandas [Python]
Send push notifications to iOS apps with Python2 (with sample code)
[boto3] Send an email using SES
Cut out an image with python
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
Create an Excel file with Python3
How to create an email user
Convert list to DataFrame with python