python发送邮件时报: Error: need RCPT command

在一个项目中,执行了一个任务后却收不到设定的邮件,经过异常捕获发现,在发送邮件时报了如下错误:

smtplib.SMTPDataError: (503, b'Error: need RCPT command')

复现异常

>>> cc_email = ['None']
>>> send_email(subject=subject, content=content, domain=domain, to_email=to_email, cc_email=cc_email)
Traceback (most recent call last):
  File "", line 1, in 
  File "/data/web/scripts/py/custom_send_email.py", line 43, in send_email
    msg.send()
  File "/usr/local/lib/python3.7/site-packages/django/core/mail/message.py", line 294, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/usr/local/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 110, in send_messages
    sent = self._send(message)
  File "/usr/local/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 126, in _send
    self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
  File "/usr/local/lib/python3.7/smtplib.py", line 882, in sendmail
    (code, resp) = self.data(msg)
  File "/usr/local/lib/python3.7/smtplib.py", line 560, in data
    raise SMTPDataError(code, repl)
smtplib.SMTPDataError: (503, b'Error: need RCPT command')

后排查代码,原来是在获取抄送邮箱时获取了一个包含"None"的列表。

解决办法:

将cc_email = ['None'] 修改为 cc_email = None 或者 cc_email = []即可。

转载于:https://my.oschina.net/u/4153263/blog/3070273

你可能感兴趣的:(python发送邮件时报: Error: need RCPT command)