Sending an Email Using win32com with a Buffer as Attachments

34 views Asked by At

I'm trying to write a script that will be deployed as a lambda, and supposed to send an automatic emails with some dfs as attachments. Our company using outlook, so I'm using win32com.Client. For some reason, I can't using StringIo as a buffer here. Is any one can help me?

This is the code I tried:

def emailing_alerts(df):
    ol = win32com.client.Dispatch('Outlook.Application')
    olmailitem = 0x0
    newmail = ol.CreateItem(olmailitem)
    newmail.Subject = 'Testing Mail - Deployment Alerts'
    newmail.To = '[email protected]'
    newmail.Body = f"Hi all, \n" \
               f"This is a test email to see whether my pipeline-alerts works."
    csv_buffer = StringIO()
    df.to_csv(csv_buffer, index=False)
    newmail.Attachments.Add(Source=csv_buffer.getvalue())
    newmail.Send()
    print(f"The mail was send successfully")

0

There are 0 answers