TypeError:预期的字符串或缓冲区-在Re.Sub中

问题描述

完整的错误是这样的:

  File "C:\scripts\WDItemAddEmail.py",line 68,in <module>
    send_email(execQuery)
  File "C:\scripts\WDItemAddEmail.py",line 66,in send_email
    s.sendmail(email_sender,email_recipient,msg)
  File "C:\Python27\lib\smtplib.py",line 749,in sendmail
    (code,resp) = self.data(msg)
  File "C:\Python27\lib\smtplib.py",line 507,in data
    q = quotedata(msg)
  File "C:\Python27\lib\smtplib.py",line 167,in quotedata
    re.sub(r'(?:\r\n|\n|\r(?!\n))',CRLF,data))
  File "C:\Python27\lib\re.py",line 155,in sub
    return _compile(pattern,flags).sub(repl,string,count)
TypeError: expected string or buffer

这是我的代码: 我正在尝试发送包含查询结果的电子邮件,但电子邮件部分出现错误。 我不确定sendemail()方法是否引起了问题。

def execQuery(server,database,query):
    try:
        cursor.execute(query)
        rows = cursor.fetchall()
        for row in rows:
            results = """<td>"""+row[0]+"""</td>
            <td>"""+row[4]+"""</td>
            <td>"""+row[5]+"""</td>
            \n"""
            
        
        cursor.close()
        conn.close()
        return results
    except pyodbc.Error as e:
        return "<td>Execution Failed.</td>"
        

def send_email(execQuery):
    results = execQuery(server,query)
    email_sender = '[email protected]'
    email_recipient = '[email protected]'
    email_subject = "WD New Item(s) Added"
    msg = MIMEMultipart()
    msg['From'] = email_sender
    msg['To'] = email_recipient
    msg['Subject'] = email_subject
    
    html = """\
    <html>
      <head></head>
      <body>
        <table>
            <tr>
                <th>GUID</th>
                <th>Part Number</th>
                <th>Manufacturer</th>
            </tr>
            <tr>
                """+results+"""
            </tr>
        </table>
      </body>
    </html>"""
    
    body = MIMEText(html,'html')
    msg.attach(body)
    
    s = smtplib.SMTP(SMTPserver)
    s.sendmail(email_sender,msg)
    s.quit()
send_email(execQuery)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)