0

I have an xls file which has few images and graphs in it. I would like to attach this file and send to an email. Below is my code:

def create_message(report):
    message = MIMEMultipart()
    message['From'] = msettings.EMAILS_SEND_FROM_NAME
    message['To'] = msettings.EMAILS_SEND_TO
    message['Subject'] = msettings.EMAILS_SUBJECT

    with open(report, "r") as file:
        payload = MIMEApplication(file.read(), Name=os.path.basename(report))
        content = 'attachment; filename="{}"'.format(os.path.basename(report))
        payload['Content-Disposition'] = content
        message.attach(payload)

But I get the following Error

  message = create_message(file)
  line 148, in create_message
    payload = MIMEApplication(file.read(), Name=os.path.basename(report))
  File "python-3.4.3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 732: character maps to <undefined>

I doubt that the encoding of my xls file is not supported. How can I attach a xls file with images,graphs and a lot of formatting as an attachment without losing any information from the xls file?

Gita
  • 149
  • 1
  • 9
  • 1
    @DavidZemens Thank you. I was able to solve the issue by referring to the link you provided. – Gita Aug 15 '18 at 16:12

1 Answers1

-1

I have had good luck with the library yagmail, which would not only greatly simplify your code, but I've used attachments with no problem.

one_observation
  • 454
  • 5
  • 16
  • This is an anecdote, not an answer. This should probably be a comment on OP rather than as an answer, since it's not actually an answer. – David Zemens Aug 15 '18 at 15:57
  • It is a tool, that offers a solution, that is presented along with an anecdote -- it is certainly not a thorough answer, but it is not a comment. THIS, is a comment. – one_observation Aug 15 '18 at 16:47