1

I have created a python mailer that creates a document, names the document a name from excel stored in a variable and attaches it to an email. Once this is done it sends the email.

I'm getting stuck at an pywintypes.com_error: that stops me attaching the document because the file name cant be found but i'm not sure what i'm doing wrong, I thought it was a encoding/decoding issue but i just havent had any luck getting past this

# Stores each individual's information in the variables below
        first_name = row[2].value
        last_name = row[1].value
        full_name = row[2].value + " " + row[1].value
        renewal_date_value = row[4].value
        renewal_date = renewal_date_value.strftime('%m/%d/%Y')
        email = row[3].value
        ncbfaa_id = str(row[0].value)
        # Opens outlook
        outlook = win32.Dispatch('outlook.application')
        # Gets email ready
        mail = outlook.CreateItem(0)

        mail.To = email

        mail.Subject = '2019'

        #Plain Text Message
        plaintext_body = ccs_plaintext_message.replace('{first_name}', first_name.encode('utf-8')) # lavon
        mail.Body = plaintext_body.replace('{renewal_date}', renewal_date)

        #HTML Message

        html_body = ccs_html_message.replace('{first_name}', first_name.encode('utf-8')) # lavon
        html_body2 = html_body.replace('{id}', ncbfaa_id)
        mail.HTMLBody = html_body2.replace('{renewal_date}', renewal_date)



        attachment = "C:\\Users\Education\Documents\D\Python\Email\\2019_CCS_Renewal\"" + full_name + ".pdf"
        attachment2 = "C:\\Users\Education\Documents\LTowns\CES CCS Supplimetary Documents\CCS Designation Statement of Use.pdf"
        #mail.Attachments.Add(Source=attachment)
        #mail.Attachments.Add(Source=attachment2)
        mail.Attachments.Add(Source=attachment)
        mail.Attachments.Add(Source= "C:\\Users\Education\Documents\LTowns\CES CCS Supplimetary Documents\CCS Designation Statement of Use.pdf")


        mail.Send()
        print("Email sent to " + full_name)

I expected the document to be created and named dynamically then attached but i get this error below

main.py
Traceback (most recent call last):
  File "C:\Users\Education\Documents\DDonnelly\Python\Email\2019_CCS_Renewal\main.py", line 11, in <module>
    mailer()
  File "C:\Users\Education\Documents\DDonnelly\Python\Email\2019_CCS_Renewal\ccs_renewal_mailer.py", line 50, in mailer
    mail.Attachments.Add(Source=attachment)
  File "<COMObject <unknown>>", line 3, in Add
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)
LaMar T
  • 15
  • 5
  • You have a double backslash: attachment = "C:\\Users\Education\Documents\D\Python\Email\\2019_CCS_Renewal\"" + full_name + ".pdf" – francisco sollima Oct 11 '19 at 15:53
  • On python 2.7 and windows, use an `r` prefix on your strings (`r"string"`) to avoid escape characters and backslash confusions. https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-and-what-are-raw-string-literals. Python 3.4+ has `pathlib.Path` objects which make this much less tedious. – Jwely Oct 11 '19 at 15:57
  • `attachment = "C:\\Users\\Education\\Documents\\D\\Python\\Email\\2019_CCS_Renewal\\" + full_name + ".pdf"` Use double backslash for all your paths, or use `r'C:\Users...'` raw strings (but you can't end with a backslash) – r.ook Oct 11 '19 at 16:45

0 Answers0