English 中文(简体)
Scrapy - Sending an E-mail
  • 时间:2024-12-22

Scrapy - Sending an E-mail


Previous Page Next Page  

Description

Scrapy can send e-mails using its own facipty 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.

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 pne sends an e-mail without attachments −

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

MailSender Class Reference

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)

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 vapdation if is not mentioned.

4

smtppass (str)

It specifies the SMTP pass for vapdation.

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.

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

classmethod from_settings(settings)

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

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 (pst)

It refers to the email receiver.

2

subject (str)

It specifies the subject of the email.

3

cc (pst)

It refers to the pst 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

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 vapdation. There will be no vapdation, if this setting is set to disable.

None
5

MAIL_PASS

It provides the password used for SMTP vapdation.

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
Advertisements