Home > Mobile >  How to Send Mails with attachments from your outlook using Python
How to Send Mails with attachments from your outlook using Python

Time:12-08

The below code works great without attachment to send 30 emails/min using outlook

the csv file i'm using having: name, email, attach as example:

firstname,[email protected],firstfile.pdf
secondname,[email protected],secondfile.pdf

so I've set a variable for the full path of the attachments folder ( since the attachment folder is changeable

attachments_path= "C:\Users\PC\Desktop\My\files\"

and added message.Attachments.add (attachments_path attach) to the loop code

import csv
from time import sleep
import win32com.client as client

attachments_path= "C:\\Users\\PC\Desktop\\My\\files\\"

# create template for message body
template = "Hello {}, this is a test."


# open distribution list
with open('emails.csv', 'r', newline='') as f:
    reader = csv.reader(f)
    distro = [row for row in reader]

# chunk distribution list into blocks of 30
chunks = [distro[x:x 30] for x in range(0, len(distro), 30)]

# create outlook instance
outlook = client.Dispatch('Outlook.Application')

# iterate through chunks and send mail
for chunk in chunks:
    # iterate through each recipient in chunk and send mail
    for name, address, attach in chunk:
        message = outlook.CreateItem(0)
        message.To = address
        message.Subject = "Subject Test"
        message.Body = template.format(name)
        message.Attachments.add (attachments_path   attach) # added for testing
        message.Send()

    # wait 60 seconds before sending next chunk
    sleep(60)

so when i run the code i got the error:

Traceback (most recent call last):
  File "c:\Users\PC\Desktop\My\app.py", line 29, in <module>
    message.Attachments.add (attachments_path   attach)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 628, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid, 0, invoke_type, 1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot add the attachment; no data source was provided.', None, 0, -2147352567), None)

any idea how to make this code run with attachments?

CodePudding user response:

Are you sure such file exists on the disk before running the code?

message.Attachments.add (attachments_path attach)

First of all, you need to make sure such file exists on the disk and file path contains allowed symbols in the name.

The Add method of the Attachments class accepts a file path string which stands for the source of the attachment. This can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

CodePudding user response:

After many trial and error the solution was only changing the file path slashes from \ to / so instead of

"C:\\Users\\PC\Desktop\\My\\files\\file.pdf" 

it should be

"C:/Users/PC/Desktop/My/files/file.pdf"
  • Related