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.
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"])
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 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. |
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 (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. |
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 |