前段时间使用python批量发邮件,通过excel获取收件人信息,结果发完后报错如下:
Traceback (most recent call last):
smtpObj.sendmail(sender, receivers, message.as_string())
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 871, in sendmail
(code, resp) = self.rcpt(each, rcpt_options)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 541, in rcpt
self.putcmd("rcpt", "TO:%s%s" % (quoteaddr(recip), optionlist))
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\smtplib.py", line 153, in quoteaddr
if addrstring.strip().startswith('<'):
AttributeError: 'NoneType' object has no attribute 'strip'
当时百思不得其解啊,为什么发完了报错......
今天终于静下心来看了一下错误内容, NoneType object, 空类型对象,是不是表示什么都没有啊?
难道是excel多出来了空行?
于是用ws.max_row获取了一下最大行号,果然比邮件联系人行多很多
选中那些空行,删除整行,点击保存(必须点击保存啊)
再次运行程序,没有报错。
至此,问题解决~
使用excel的时候得养成一个好习惯,删除内容的时候,直接删除整行,不留下操作过的空行,否则各种程序极易认为那不是空行。