【问题标题】:Why is smtp.sendmail in python3 not sending the subject?为什么 python3 中的 smtp.sendmail 不发送主题?
【发布时间】:2021-12-01 06:12:04
【问题描述】:

我有这个发送邮件的功能:

    def sendmail(self,ffrom,to,subject,message ="",cc = "",adj = None,msgtype = "html", codificacion='utf-8'):
        to = to.replace(" ","")
        cc = cc.replace(" ","")
        email = to
        if(cc != ""):
            email += "," + cc
        email = email.replace(" ","")

        msg = MIMEMultipart()

        msg['From'] = ffrom
        msg['To'] = to
        msg['Subject'] = subject
        msg['Cc'] = cc

        if(adj != None):
            for file in adj.split(","):
                file_name = file.split("/")[-1]
                part = MIMEBase('application', "octet-stream")
                fc = open(file, "rb")
                part.set_payload(fc.read())
                encoders.encode_base64(part)
                part.add_header('Content-Disposition', 'attachment; filename="%s"' % (file_name))
                msg.attach(part)
                fc.close()
        t=MIMEText(message, msgtype, _charset=codificacion)
        msg.attach(t)
        #self.smtp.set_debuglevel(True)
        try: 
            #smtp = smtplib.SMTP('smtp-mi.risorse.enel:25') 
            print(msg['Subject'])
            print(msg.as_string())
            self.smtp.sendmail(msg['From'],email.split(","), msg.as_string()) 
            print ("\\nCorreo enviado\\n") 
        except: 
            print ('\nError: el mensaje no pudo enviarse. Compruebe que sendmail se encuentra instalado en su sistema\n')

而且大多数时候它运行良好。但是,有时它不会发送主题。

我已经打印了主题,它就在那里。

【问题讨论】:

  • 你有没有检查收到的消息的原始来源,看看是否有主题?

标签: python smtp smtplib


【解决方案1】:

我找到了问题的原因。

在收件人列表中,有一个换行符 (\n)。

解决办法是:

msg['To'] = to.replace(" ",'').replace("\n",'') 

在发送邮件之前删除空格和换行符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多