Ceci est un mémo de cette vidéo. https://youtu.be/Pw_jSRyX4lQ
http://console.developers.google.com/
$ pip install gspread oauth2client
send.py
#Relations d'envoi d'e-mails
from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib
#Coopération API Google
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)
#Je reçois la première feuille intitulée 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')
#Contenu de l'e-mail
body = sheet2.cell(2,2).value
title = sheet2.cell(2,1).value
#Toutes les valeurs sont affectées à une variable appelée data.
data = sheet1.get_all_records()
#Obtenez le nombre de données
last_number = len(data)
for row in range(last_number):
#Extraire les informations nécessaires pour envoyer un email
full_name = data[row]["name"] #Nom
msg = MIMEText(full_name + body)
pprint(msg)
msg['Subject'] = sheet2.cell(2,1).value
msg['From'] = '**************@gmail.com'
msg['To'] = data[row]["adresse mail"] # adresse mail
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