0

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

Alexia
  • 19
  • 3
  • `but it doesn't work` - what happens instead? – Rafa Guillermo May 31 '22 at 13:19
  • it doesn't find the file maybe it's because it's from drive – Alexia May 31 '22 at 13:23
  • Do you get any errors? Does the email get sent just without the attachment? Please help me to help you – Rafa Guillermo May 31 '22 at 13:25
  • I have the following error : [Errno 2] No such file or directory: 'https://drive.google.com/open?id=IDexemple' of course it's a real ID, it doesn't send the email – Alexia May 31 '22 at 13:25
  • You need to use the Drive API to get the file and attach it. [Files: get](https://developers.google.com/drive/api/v3/reference/files/get) is the method you need. Check out the quickstart [here](https://developers.google.com/drive/api/quickstart/python) – Rafa Guillermo May 31 '22 at 13:28

0 Answers0