ASCII-Codec in Python3 [SOLVED] - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: ASCII-Codec in Python3 [SOLVED] (/thread-34219.html) |
ASCII-Codec in Python3 [SOLVED] - AlphaInc - Jul-07-2021 Hello everyony, I have a Python-Script which worked fine under Python2 but when I try to run it in Python3 I get the following error: Traceback (most recent call last): File "debug2.py", line 49, in <module> sender.sendmail(sendTo, emailSubject, emailContent) File "debug2.py", line 39, in sendmail session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content) File "/usr/lib/python3.7/smtplib.py", line 855, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode characters in position 131-133: ordinal not in range(128)Since Python2 reached it's EOL I wanted to "update" my scripts to Python3 and most of them worked out pretty well. This is one of my scripts: #!/usr/bin/env python #-*- coding:utf-8 -*- import smtplib import time f1 = open("../index/mail.txt","r") mail = f1.read() [:-1] f2 = open("../index/passwd.txt","r") passwd = f2.read() [:-1] f3 = open("../index/receiver.txt","r") receiver = f3.read() [:-1] #Email Variables SMTP_SERVER = 'smtp.gmail.com' #Email Server (don't change!) SMTP_PORT = 587 #Server Port (don't change!) GMAIL_USERNAME = mail #change this to match your gmail account GMAIL_PASSWORD = passwd #change this to match your gmail password class Emailer: def sendmail(self, recipient, subject, content): #Create Headers headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient, "MIME-Version: 1.0", "Content-Type: text/html"] headers = "\r\n".join(headers) #Connect to Gmail Server session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) session.ehlo() session.starttls() session.ehlo() #Login to Gmail session.login(GMAIL_USERNAME, GMAIL_PASSWORD) #Send Email & Exit session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content) session.quit sender = Emailer() sendTo = receiver emailSubject = "Subject" emailContent = "ÄÖÜ" #Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and "emailConten$ sender.sendmail(sendTo, emailSubject, emailContent)I only get the error when I use something like an 'äöü' which is pretty common for the german language. In Python2 I had a similiar issue which I could resolve by using the coding:utf-8line in the head. How do you solve this in Python3? RE: ASCII-Codec in Python3 - Gribouillis - Jul-07-2021 The documentation of smtplib.SMTP.sendmail() saysQuote:msg may be a string containing characters in the ASCII range, or a byteAs you want to send a string containing characters outside the ASCII range, I suggest that you encode the string manually using another encoding, such as utf8, so try this msg = (headers + "\r\n\r\n" + content).encode('utf8') session.sendmail(GMAIL_USERNAME, recipient, msg) RE: ASCII-Codec in Python3 - AlphaInc - Jul-07-2021 (Jul-07-2021, 06:39 PM)Gribouillis Wrote: The documentation of Thanks for your reply. I edited the ending of my file to this (I hope you meant that). #Send Email & Exit msg = (headers + "\r\n\r\n" + content).encode('utf8') session.sendmail(GMAIL_USERNAME, recipient, msg) session.quit sender = Emailer() sendTo = receiver emailSubject = "Subject" emailContent = "ÄÖÜ" #Sends an email to the "sendTo" address with the specified "emailSubject" as the subject and "emailConten$ sender.sendmail(sendTo, emailSubject, emailContent)When I try to execute it I get the following error: File "debug2.py", line 40 msg = (headers + "\r\n\r\n" + content).encode('utf8') ^ TabError: inconsistent use of tabs and spaces in indentationI don't see the error here tbh. RE: ASCII-Codec in Python3 - Gribouillis - Jul-07-2021 Make sure the python file is indented with 4 spaces instead of tab characters. You can use the old but excellent reindent command to reindent the program, or a more recent tool such as the black module, or replace the tab characters by 4 space characters with your editor. Also note that your editor can be configured to enter 4 spaces when you hit the tabulation key. Use this configuration for Python programming. RE: ASCII-Codec in Python3 - AlphaInc - Jul-07-2021 (Jul-07-2021, 07:03 PM)Gribouillis Wrote: Make sure the python file is indented with 4 spaces instead of tab characters. You can use the old but excellent reindent command to reindent the program, or a more recent tool such as the black module, or replace the tab characters by 4 space characters with your editor. Yeah, of course there was a tab, stupid me Thanks for the help, now it works |