Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 charmap codec can't decode byte error with gzipped file in python
#1
I am attempting to send an email that includes a file in gzip format as an attachment using python.

But I'm getting an error when I try to send the file. This is the exception I'm getting:

Exception:
'charmap' codec can't decode byte 0x90 in position 75: character maps to <undefined>
This is what I'm doing in my code:

    import gzip
    destination = 'path/to/file'
    to_addr = input("Enter the recipient's email address: ")
    from_addr = 'cloudops@noreply.company.com'
    subject = "Commpany AWS Billing Info "
    content = "email to the user about the file"
    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = subject
    body = MIMEText(content, 'html')
    msg.attach(body)
    server = smtplib.SMTP('smtpout.us.companyworld.company.com', 25)
    filename = destination
    try:
        with open(filename, 'r') as f:
            part = MIMEApplication(f.read(), Name=basename(filename))
            part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename))
            msg.attach(part)
            server.send_message(msg, from_addr=from_addr, to_addrs=[to_addr])
        print("Email was sent to: %s" %  to_addr)
    except Exception as e:
        print("Exception: ", e)
        print("Email was not sent.")
The file I am trying to send was created in another function in these lines:
        import pandas
        read_sql = 'select * from somedb'
        mydb = mysql.connector.connect(user='x', password='x',host='x',database='aws_bill')
        results = pandas.read_sql_query(read_sql, mydb)
        results.to_csv(destination, index=False, encoding='utf8', compression='gzip')
How can I correct this problem and send the attachment?
Quote
#2
You should post all Traceback because there is also line number of error.
Also all email imports should be posted.

Try line 11:
body = MIMEText(content, 'html', 'utf-8') 
Line 16:
with open(filename, 'r', encoding='utf-8') as f:
bluethundr likes this post
Quote
#3
(Apr-30-2019, 04:58 AM)snippsat Wrote: You should post all Traceback because there is also line number of error.
Also all email imports should be posted.

Try line 11:
body = MIMEText(content, 'html', 'utf-8') 
Line 16:
with open(filename, 'r', encoding='utf-8') as f:

OK thanks! I will do that next time, and note it going forward that I should post full stack traces. The problem turned out to be that the gzipped file is a binary. So I need to use this option to send it:

with open(filename, 'rb') as f:
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  unicodedecodeerror:utf codec can't decode byte 0xe3 in position 1 mariolopes 3 102 Oct-14-2019, 10:17 PM
Last Post: mariolopes
  Getting decode error. shankar 8 2,226 Sep-20-2019, 10:05 AM
Last Post: tinman
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 7 409 Sep-12-2019, 11:19 PM
Last Post: newbieAuggie2019
  Error -3 when opening matlab file in python python_newbie09 0 118 Aug-02-2019, 11:51 AM
Last Post: python_newbie09
  Read Byte Array from Zigbee coordinator using python jenkins43 3 407 Mar-26-2019, 03:03 PM
Last Post: micseydel
  Fatal Python error: Py_Initialize: unable to load the file system codec ecg1g15 0 642 Feb-12-2019, 12:16 PM
Last Post: ecg1g15
  UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 23: c kapilan15 2 974 Jan-14-2019, 09:11 PM
Last Post: Gribouillis
  Python Directory and File Create Error basic_learner 1 422 Dec-16-2018, 11:03 AM
Last Post: buran
  decode base64 with python give error thailq 3 776 Sep-24-2018, 12:39 AM
Last Post: thailq
  Countryinfo package charmap error mkaru 6 897 Sep-16-2018, 02:58 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)