I want to send file to a list of email, but i don't know how to attached a fill from drive, i know the id of my pdf but it doesn't work.
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.utils import COMMASPACE, formatdate
from email import encoders
from pathlib import Path
sender_email = "mail"
receiver_email = "mail"
password = "passeword"
message = MIMEMultipart("alternative")
message["Subject"] = "multipart test"
message["From"] = sender_email
message["To"] = receiver_email
for index, row in df.iterrows():
# we are dealing with dictionary, so you can use get method
report = row.get('report')
date2 = row.get('date2')
to_email = row.get('mail')
cc1 = row.get('cc1')
cc2 = row.get('cc2')
text = row.get('text')
filename = date2+report
file_list = drive.ListFile({
'q':f"title contains '${filename}' and mimeType='application/pdf' and trashed=false",
'supportsAllDrives':True,
'corpora': "teamDrive",
'teamDriveId': 'ID',
'supportsTeamDrives' :True,
'includeItemsFromAllDrives':True,
}).GetList()
html = """\
<html>
<body>
<p>Hi,<br>
How are you?<br>
</p>
</body>
</html>
"""
# create MIMEText objects
part1 = MIMEText(html, 'html')
# attach the text/html part to the MIMEmultipart message
message.attach(part1)
# attaching an attachement
with open('https://drive.google.com/open?id=IDexample', "rb") as f:
attach = email.mime.application.MIMEApplication(f.read(),_subtype="pdf")
attach = MIMEApplication(f.read(),_subtype="pdf")
attach.add_header('Content-Disposition','attachment',filename=str(path_to_pdf))
msg.attach(attach)
# create a secure SSL context
context = ssl.create_default_context()
# log in and send the email
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, to_email, message.as_string())
Everythings work except the attachment, so i add a list of email in my spreetsheet, and several information. I search my file with my information in my spreadsheet