📅  最后修改于: 2020-10-31 14:40:55             🧑  作者: Mango
Scrapy可以使用自己的称为Twisted non-blocking IO的工具发送电子邮件,该工具与爬网程序的non-blocking IO保持距离。您可以配置一些发送电子邮件的设置,并提供用于发送附件的简单API。
实例化MailSender的方法有两种,如下表所示-
Sr.No | Parameters | Method |
---|---|---|
1 | from scrapy.mail import MailSender mailer = MailSender() | By using a standard constructor. |
2 | mailer = MailSender.from_settings(settings) | By using Scrapy settings object. |
以下行发送不带附件的电子邮件-
mailer.send(to = ["receiver@example.com"], subject = "subject data", body = "body data",
cc = ["list@example.com"])
MailSender类使用Twisted非阻塞IO从Scrapy发送电子邮件。
class scrapy.mail.MailSender(smtphost = None, mailfrom = None, smtpuser = None,
smtppass = None, smtpport = None)
下表显示了MailSender类中使用的参数-
Sr.No | Parameter & Description |
---|---|
1 |
smtphost (str) The SMTP host is used for sending the emails. If not, then MAIL_HOST setting will be used. |
2 |
mailfrom (str) The address of receiver is used to send the emails. If not, then MAIL_FROM setting will be used. |
3 |
smtpuser It specifies the SMTP user. If it is not used, then MAIL_USER setting will be used and there will be no SMTP validation if is not mentioned. |
4 |
smtppass (str) It specifies the SMTP pass for validation. |
5 |
smtpport (int) It specifies the SMTP port for connection. |
6 |
smtptls (boolean) It implements using the SMTP STARTTLS. |
7 |
smtpssl (boolean) It administers using a safe SSL connection. |
指定的MailSender类引用中有以下两种方法。第一种方法
classmethod from_settings(settings)
它通过使用Scrapy设置对象合并。它包含以下参数-
设置(scrapy.settings.Settings对象) -被视为电子邮件接收者。
另一种方法
send(to, subject, body, cc = None, attachs = (), mimetype = 'text/plain', charset = None)
下表包含上述方法的参数-
Sr.No | Parameter & Description |
---|---|
1 |
to (list) It refers to the email receiver. |
2 |
subject (str) It specifies the subject of the email. |
3 |
cc (list) It refers to the list of receivers. |
4 |
body (str) It refers to email body data. |
5 |
attachs (iterable) It refers to the email’s attachment, mimetype of the attachment and name of the attachment. |
6 |
mimetype (str) It represents the MIME type of the e-mail. |
7 |
charset (str) It specifies the character encoding used for email contents. |
以下设置确保无需编写任何代码,我们就可以使用项目中的MailSender类配置电子邮件。
Sr.No | Settings & Description | Default Value |
---|---|---|
1 |
MAIL_FROM It refers to sender email for sending emails. |
‘scrapy@localhost’ |
2 |
MAIL_HOST It refers to SMTP host used for sending emails. |
‘localhost’ |
3 |
MAIL_PORT It specifies SMTP port to be used for sending emails. |
25 |
4 |
MAIL_USER It refers to SMTP validation. There will be no validation, if this setting is set to disable. |
None |
5 |
MAIL_PASS It provides the password used for SMTP validation. |
None |
6 |
MAIL_TLS It provides the method of upgrading an insecure connection to a secure connection using SSL/TLS. |
False |
7 |
MAIL_SSL It implements the connection using a SSL encrypted connection. |
False |