Scrapy 简明教程

Scrapy - Sending an E-mail

Description

Scrapy 可以使用其名为 Twisted non-blocking IO 的自有工具发送电子邮件,该工具远离爬虫程序的非阻塞 IO。你可以配置发送电子邮件的一些设置,并提供用于发送附件的简单 API。

Scrapy can send e-mails using its own facility called as Twisted non-blocking IO which keeps away from non-blocking IO of the crawler. You can configure the few settings of sending emails and provide simple API for sending attachments.

共有两种实例化 MailSender 的方法,如下表所示 −

There are two ways to instantiate the MailSender as shown in the following table −

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.

以下代码行发送不带附件的电子邮件 −

The following line sends an e-mail without attachments −

mailer.send(to = ["receiver@example.com"], subject = "subject data", body = "body data",
   cc = ["list@example.com"])

MailSender Class Reference

MailSender 类使用 Twisted non-blocking IO 从 Scrapy 发送电子邮件。

The MailSender class uses Twisted non-blocking IO for sending e-mails from Scrapy.

class scrapy.mail.MailSender(smtphost = None, mailfrom = None, smtpuser = None,
   smtppass = None, smtpport = None)

以下表格显示了 MailSender 类中使用的参数 −

The following table shows the parameters used in MailSender class −

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 类中具有以下两个方法。第一个方法,

Following two methods are there in the MailSender class reference as specified. First method,

classmethod from_settings(settings)

它通过使用 Scrapy 设置对象合并。它包含以下参数 −

It incorporates by using the Scrapy settings object. It contains the following parameter −

settings (scrapy.settings.Settings object) − 它被视为电子邮件接收者。

settings (scrapy.settings.Settings object) − It is treated as e-mail receiver.

另一种方法,

Another method,

send(to, subject, body, cc = None, attachs = (), mimetype = 'text/plain', charset = None)

下表包含了上述方法的参数 −

The following table contains the parameters of the above method −

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.

Mail Settings

通过以下设置确保无需编写任何代码,即可使用项目中的 MailSender 类配置电子邮件。

The following settings ensure that without writing any code, we can configure an e-mail using the MailSender class in the project.

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