'How do i attach PDF correctly with Gmail API?

Hello to everyone i hope yall good. Im trying to create a draft with gmail api and attach some files. I have no problems with the "JPG" files, but when i add some pdf files.

This error appears:

*client_secret.json-gmail-v1-(['https://mail.google.com/'],) ['https://mail.google.com/'] gmail service created successfully Traceback (most recent call last): File "F:\Python Proyectos\gmail\borrador.py", line 52, in ).execute() File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googleapiclient_helpers.py", line 131, in positional_wrapper return wrapped(*args, *kwargs) File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googleapiclient\http.py", line 922, in execute resp, content = _retry_request( File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googleapiclient\http.py", line 221, in _retry_request raise exception File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\googleapiclient\http.py", line 190, in retry_request resp, content = http.request(uri, method, *args, **kwargs) File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\google_auth_httplib2.py", line 218, in request response, content = self.http.request( File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\httplib2_init.py", line 1701, in request (response, content) = self.request( File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\httplib2_init.py", line 1421, in _request (response, content) = self.conn_request(conn, request_uri, method, body, headers) File "C:\Users\bRUNS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\httplib2_init.py", line 1373, in _conn_request response = conn.getresponse() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 1374, in getresponse response.begin() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 318, in begin
version, status, reason = self._read_status() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 279, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\socket.py", line 705, in readinto
return self._sock.recv_into(b) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\ssl.py", line 1273, in recv_into
return self.read(nbytes, buffer) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.752.0_x64__qbz5n2kfra8p0\lib\ssl.py", line 1129, in read return self._sslobj.read(len, buffer) TimeoutError: The read operation timed out

This is my code:

from Google import Create_Service
import os
import base64
import mimetypes 
from email import encoders  
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email import encoders
from sys import api_version



CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'gmail'
API_VERSION = 'v1'
SCOPES = ['https://mail.google.com/']

service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)

file_attachments = [r'.\Attachments\a.jpg', r'.\Attachments\b.jpg', r'.\Attachments\duoc.jpeg', r'.\Attachments\fachadatextil1.jpg', r'.\Attachments\fachadatextil2.jpg', r'.\Attachments\forum1.jpg', r'.\Attachments\forum2.jpg', r'.\Attachments\hdpedagoberto1.jpeg', r'.\Attachments\hdpedagoberto2.jpeg', r'.\Attachments\ingevec.jpeg', r'.\Attachments\pintana.jpg', r'.\Attachments\Santander.jpg', r'.\Attachments\terrazapvc.jpg', r'.\Attachments\BRO-Flexlight-Advanced-1002-S2-ES.pdf', r'.\Attachments\fichatecnicaforum.pdf', r'.\Attachments\MC_Brochure_2021.pdf', r'.\Attachments\Parasolbrochure(ingles).pdf']

mimeMessage = MIMEMultipart()
mimeMessage['from'] = '[email protected]' #validar dominio
mimeMessage['to'] = '[email protected]' #este deberia cambiar
mimeMessage['subject'] = 'Arquitectura Textil para habitar nuevos espacios'
msg_body = 'prueba'     #va un txt con la plantilla
mimeMessage.attach(MIMEText(msg_body, 'plain'))


for attachment in file_attachments:
    content_type, encoding = mimetypes.guess_type(attachment)
    main_type, sub_type = content_type.split('/', 1)
    file_name = os.path.basename(attachment)

    f = open(attachment, 'rb')

    myFile = MIMEBase(main_type, sub_type)
    myFile.set_payload(f.read())
    myFile.add_header('Content-Disposition', 'attachment', filename=file_name)
    encoders.encode_base64(myFile)

    f.close()

    mimeMessage.attach(myFile)

raw_string = base64.urlsafe_b64encode(mimeMessage.as_bytes()).decode()

response = service.users().drafts().create(
    userId='me',
    body={'message' : {'raw':raw_string }}
).execute()

This error just occurs with the PDF files

New Edit: The Draft its created even with that error.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source