在學習Scrapy發送Email時遇到以下錯誤:
[scrapy.mail] ERROR: Unable to send mail: To=['mymail@gmail.com'] Cc=[] Subject="news" Attachs=1- 502 Server does not support secure communication via TLS / SSL
我也剛學Scrapy沒多久,程式碼部分是參考教學的,功能是Email攜帶附件CSV檔案傳送。
def close_spider(self, spider):
self.exporter.finish_exporting()
self.file.close()
mail = MailSender(
smtphost=settings.MAIL_HOST,
smtpport=settings.MAIL_PORT,
smtpuser=settings.MAIL_FROM,
smtppass=settings.MAIL_PASS,
smtptls=settings.MAIL_TLS,
)
attach_name = "posts.csv"
mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
file_object = open("posts.csv", "rb")
return mail.send(
to=["mymail"],
subject="news",
body="",
attachs=[(attach_name, mime_type, file_object)]
)
setting部分如下
MAIL_HOST = "smtp.gmail.com"
MAIL_PORT = 587
MAIL_FROM = "mymail"
MAIL_PASS = "**********"
MAIL_TLS = True
看錯誤訊息應該和TLS連線有關,上網爬文後還是找不到解答,請問有人可以告訴我哪裡出錯嗎?
或有任何的指教都很歡迎~
我的環境是Python 3.8.8 / Scrapy 2.5.0
他都說他不支援了,為何你還要叫他用TLS連線???MAIL_TLS = True
改False不好嗎?
但是寄送mail不是要藉著TLS協定嗎? 還是我搞錯了?
改False會出現另外一個錯誤:
[scrapy.mail] ERROR: Unable to send mail: To=['myemail'] Cc=[] Subject="news" Attachs=0- 530 5.7.0 Must issue a STARTTLS command first. a185sm967771pfa.205 - gsmtp
給你官方文件,好好研究,不要去找那種路上不知到哪裡檢來的教學。
Gmail:https://support.google.com/mail/answer/7126229?hl=en
Scrapy:https://docs.scrapy.org/en/latest/topics/email.html#mailsender-class-reference
請用網頁到Gmail的右上角齒輪圖案 → 查看所有設定 → 轉寄和 POP/IMAP → 啟用imap