雖然 Python 經過 smtplib 庫使得發送 email 變得很是簡單,Scrapy 仍然提供了本身的實現。該功能十分易用,同時因爲採用了 Twisted 非阻塞式(non-blocking)IO,其避免了對爬蟲的非阻塞式 IO 的影響。另外,其也提供了簡單的 API 來發送附件。經過一些 settings 設置,能夠很簡單的進行配置。安全
有兩種方法能夠建立郵件發送器(mail sender)。能夠經過標準構造器(constructor)建立:框架
from scrapy.mail import MailSender mailer = MailSender()
或者能夠傳遞一個 Scrapy 設置對象,其會參考 settings:scrapy
mailer = MailSender.from_settings(settings)
這是如何來發送郵件(不包括附件):加密
mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@example.com"])
MailSender 類參考手冊spa
在 Scrapy 中發送 email 推薦使用 MailSender。其同框架中的其它部分同樣,使用了 Twsited 非阻塞式(non-blocking)IO。code
參數:對象
使用 Scrapy 設置對象來初始化對象。blog
參數:ip
settings (scrapy.settings.Settings object) – the e-mail recipientsssl
發送 email 到給定的接收者。
參數:
這些設置定義了 MailSender 構造器的默認值。使得你在不編寫任何一行代碼的狀況下,爲你的項目配置實現 email 通知的功能。
默認值: 'scrapy@localhost'
用於發送 email 的地址(address)(填入 From
:) 。
默認值:'localhost'
發送 email 的 SMTP 主機(host)。
默認值:25
發用郵件的 SMTP 端口。
默認值:None
SMTP 用戶。若是未給定,則將不會進行 SMTP 認證(authentication)。
默認值:None
用於 SMTP 認證,與 MAIL_USER
配套的密碼。
默認值:False
強制使用 STARTTLS。STARTTLS 能使得在已經存在的不安全鏈接上,經過使用 SSL/TLS 來實現安全鏈接。
默認值: False
強制使用 SSL 加密鏈接。