Benjamin Brannon
Benjamin Brannon

Reputation: 315

How to Add File as Attachment to Outlook Item in Python

I have just created a few files and zipped them up, then begun an email to send it. I'ts probably simple, but I haven't been able to figure out how to specify a file by path to attach. Feeding the filepath alone doesn't seem to work?

ZipName = 'Order'+OrderNumber+'.zip'
zip = zipfile.ZipFile(ZipName, 'a', 8)
for file in os.listdir(filepath_out):
    if file.endswith(".epw"):
        zip.write(file)
zip.close()

outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
#mail.From = '[email protected]'
mail.To = '[email protected]'
mail.Subject = 'Files for Order ' + OrderNumber
mail.HtmlBody = ""
mail.Attachments.Add(ZipName)
mail.Display(True)

It's off topic but related; is there an easy way to specify a non-default "from" email address? "From" doesn't seem to be a property and "Sender" doesn't change anything.

Upvotes: 1

Views: 3992

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Attachments.Add takes a fully qualified file name (e.g. c:\temp\order1.zip), not just a file name.

Upvotes: 3

Related Questions