Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';是否因为ascii编解码器而无法发送电子邮件?_Python_Excel_Email_Bots_Ascii - Fatal编程技术网

Python Can';是否因为ascii编解码器而无法发送电子邮件?

Python Can';是否因为ascii编解码器而无法发送电子邮件?,python,excel,email,bots,ascii,Python,Excel,Email,Bots,Ascii,目前我正在尝试制作一个机器人,它发送 修改过的电子邮件。然而,每次我尝试发送它时,我都会收到一条错误消息。下图。它使用一个简单的字符串,如“Hello,World!”。我可以把它写进一个文本文件,一切都很好。我真的不明白怎么了。请帮帮我。 这是我的密码: import pandas as pd import smtplib data = pd.read_excel('data.xls', index_col=None) def send_email(): replacements

目前我正在尝试制作一个机器人,它发送 修改过的电子邮件。然而,每次我尝试发送它时,我都会收到一条错误消息。下图。它使用一个简单的字符串,如“Hello,World!”。我可以把它写进一个文本文件,一切都很好。我真的不明白怎么了。请帮帮我。 这是我的密码:

import pandas as pd
import smtplib


data = pd.read_excel('data.xls', index_col=None)

def send_email():

    replacements = []

    for i in range(len(data)):

        with open('text.txt', 'r') as f:
            to_modify_txt = f.readlines()
        fname = data['Vorname'][i]
        lname = data['Nachname'][i]
        name = "\033[1m" + 'Name: ' + "\033[0m" + fname + ' ' + lname
        temp_bday = data['Geburtstag'][i]
        bday = "\033[1m" + 'Geb. Datum: ' + "\033[0m" + str(temp_bday.strftime('%d-%m-%Y'))
        sv = "\033[1m" + 'SV-Nummer: ' + "\033[0m" + str(data['SV-Nummer'][i])

        # name = 'Name: ' + fname + ' ' + lname
        # temp_bday = data['Geburtstag'][i]
        # bday = 'Geb. Datum: ' + str(temp_bday.strftime('%d-%m-%Y'))
        # sv = 'SV-Nummer: ' + str(data['SV-Nummer'][i])

        replacements.append(name)
        replacements.append(bday)
        replacements.append(sv)

        pos_name = to_modify_txt.index('Name:   \n')
        pos_bday = to_modify_txt.index('Geb. Datum:\n')
        pos_sv = to_modify_txt.index('SV-Nummer:\n')

        indexes = [pos_name, pos_bday, pos_sv]
        for (index, replacement) in zip(indexes, replacements):
            to_modify_txt[index] = replacement
        replacements = []
        modified_txt = to_modify_txt

        # with open(f'text_mod{i}.txt', 'w') as f:
        #      for elem in modified_txt:
        #          f.write(str(elem))



        from_email = 'daniel.maderner@gmail.com'
        to_email = 'daniel.maderner@gmail.com'
        password = 'maddan21'

        with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(from_email, password)

            print(to_modify_txt)

            body = modified_txt
            subject = 'LOL'
            msg = f'Subject: {subject}\n\n{body}'
            smtp.sendmail(from_email, to_email, msg)
send_email()
卓越:

txt写入:

错误消息:
纯文本没有颜色信息

您正试图使用终端模拟器通常支持的ANSI序列来添加,因为终端(如vt100)模拟了ANSI序列。众所周知,Windows CMD.EXE没有,据我所知,大多数电子邮件客户端也没有


正确的解决方案是切换到提供颜色信息的格式。最简单的是HTML,而HTML又要求您使用MIME。

为什么要在电子邮件中添加这些转义码?您正在尝试对彩色文本进行编码吗?是的,从技术上讲,电子邮件必须使用7位ASCII码。您可能可以通过使用
smtp.sendmail(从电子邮件到电子邮件,msg.encode('utf-8'))
来实现这一点,但它是非标准的。我正在尝试使名称:,Geb,。。。。boldI尝试一下,谢谢,如果您在代码段中意外地包含了谷歌密码,您可能会想更改它。