Search

31 May, 2019

Send email with excel file as attachment

We want to send a mail which will have some content in it's body and an attachment of excel file. The excel file is not physically saved on anywhere. We have just created it, filled it and and now , we want to send it in an email.

So what we really have as an attachment is a workbook object.

Let's get started.


    from io import BytesIO
    output = BytesIO()
    attachment.save(output)
    msg = EmailMultiAlternatives(subject, "", from_email, to_email, cc=cc, bcc=bcc)
    msg.attach('my_file.xlsx', output.getvalue(), 'application/vnd.ms-excel')
    msg.attach_alternative(content, "text/html")
    try:
        msg.send()
        success = True
    except socket.gaierror:
        success = False
    return success



No comments:

Post a Comment