虽然 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 加密链接。