带有Python电子邮件包的BadHeaderError,如何解决?

时间:2019-04-29 12:53:39

标签: python email

 import sys
 import os
 import shutil
 import emails # sudo pip install emails 
 from emails.template import JinjaTemplate as T


 file = open('email.txt', 'r') # reading email string from the user`s .txt 
 # file

 # userm = str(file.read()) # allocating variable and casting the read 
 # result, if required

 userm = file.read() # read the file content, no variable casting in this 
 # case

 print (userm) # checking the content

 message = emails.html(subject=T('Data processing request{{ billno }}'),
          html=T('<p>Dear {{ name }}! Your genomics data have been processed, 
download the attached files and look at your genome annotations, please'),
          mail_from=('Genomics data processor', 'XXXXXXXXXXX'))

message.send(to=('Scientist', userm), # second argument is a email of the 
user coming from the .txt file
render={'name': 'Scientist', 'billno': '777'})
 message.attach(data=open('processedfiles.zip', 'rb'), 
filename='YourGenomeProcessedAnnotations.zip', content_disposition='inline')
m = message.as_message()
s = message.as_string()
r = message.send(to=('Scientist', userm), # second argument is a email of 
the user coming from the .txt file
     render={'name': 'Scientist'},
     smtp={'host':'smtp.yandex.com', 'port': 465, 'ssl': True, 'user': 
'XXXXXXXXXX', 'password': 'XXXXXXX'}) # Write your own credentials here
  assert r.status_code == 250

这些代码在Windows上有效,但是在Linux Mint上崩溃,引发BadHeaderError,请问如何解决?

一个月前,它在Linux上运行良好,现在还不行,不知道原因。在其他Linux上尝试过,错误再次相同。

这是日志:

Traceback (most recent call last):
  File "BACK_END_PYTHON.py", line 29, in <module>
    render={'name': 'Scientist', 'billno': '777'})
  File "/usr/local/lib/python2.7/dist-packages/emails/message.py", line 404, in send
    return smtp.sendmail(**params)
  File "/usr/local/lib/python2.7/dist-packages/emails/backend/smtp/backend.py", line 117, in sendmail
    msg=msg.as_string(),
  File "/usr/local/lib/python2.7/dist-packages/emails/message.py", line 337, in as_string
    r = to_native(self.build_message(message_cls=message_cls).as_string())
  File "/usr/local/lib/python2.7/dist-packages/emails/message.py", line 284, in build_message
    msg = self._build_root_message(message_cls)
  File "/usr/local/lib/python2.7/dist-packages/emails/message.py", line 258, in _build_root_message
    self.set_header(msg, 'To', ", ".join([self.encode_address_header(addr) for addr in mail_to]), encode=False)
  File "/usr/local/lib/python2.7/dist-packages/emails/message.py", line 227, in set_header
    raise BadHeaderError("Header values can't contain newlines (got %r for header %r)" % (value, key))
emails.exc.BadHeaderError: Header values can't contain newlines (got u'Scientist <XXXXXXXXXX\n>' for header u'To')

(程序退出,代码:1) 按回车继续

1 个答案:

答案 0 :(得分:1)

异常文本解释了该问题-文本结尾处有一个换行符,用于创建“ To”标题。

emails.exc.BadHeaderError: Header values can't contain newlines (got u'Scientist <XXXXXXXXXX\n>' for header u'To')

读取文件后,您需要删除换行符('\ n'):

userm = file.read().strip()