This is an automated email code. It's Python, it works on a laptop, and the desktop has an error. UnicodeDecodeError in gethostbyaddr

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

I'm getting an error on my desktop. If you know the reason, please explain.

I'm only looking for this for two days, but I'm asking you a question because my learning progress is not progressing.

from email.mime.text import MIMEText

from email.mime.multipart import MIMEMultipart

import smtplib

import re


SMTP_SERVER = 'smtp.gmail.com'

SMTP_PORT = 465

SMTP_USER = '********@gmail.com' # ID is correct.

SMTP_PASSWORD = '00000000000000' #16-digit app password has been inserted.


def send_mail(name, addr, subject, contents):

    msg = MIMEMultipart('alternative')
    msg['From'] = SMTP_USER
    msg['To'] = addr
    msg['subject'] = name + 'nim, ' + subject

    text = MIMEText(contents, _charset='utf-8')
    msg.attach(text)

    smtp = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
    smtp.login(SMTP_USER, SMTP_PASSWORD)
    smtp.sendmail(SMTP_USER, addr, msg.as_string())
    smtp.close()

contents = ''Hi. Let's see if it works.

This mail is sent automatically sent automatically. '''

send_mail('*****', '******@gmail.com', 'This is automated mail', contents)

The above code works on your laptop. However, if you run it on your desktop,

Traceback (most recent call last):

  File "C:\Users\User\Desktop\Work Automation MAX\All-in-One Package-Work Automation max-Lecture Materials\9. Python Work Automation (Basic)\Basic\27th\27th-2.py", line 30, in <module>

    send_mail('*****', '******@gmailcom', 'Automated mail.', contents)

  File "C:\Users\User\Desktop\Work Automation MAX\All-in-One Package-Work Automation max-Lecture Materials\9. Python Work Automation (Basic)\Basic\27th\27th-2.py", line 21, in send_mail

    smtp = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)


  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 1034, in __init__

    SMTP.__init__(self, host, port, local_hostname, timeout,


  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 263, in __init__
    fqdn = socket.getfqdn()


  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\socket.py", line 791, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)


UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte

This is how the error appears.

I'm studying online, but I can't make progress, so please help me.

python-3.x unicodedecodeerror

2022-09-20 15:51

1 Answers

> hostname

Check the host name with the command. Perhaps this result includes Hangul. In the case of Windows, do not include Korean/special characters in the PC name with renaming PC.


2022-09-20 15:51

If you have any answers or tips


© 2022 pinfo. All rights reserved.