- Scrapy Tutorial
- Scrapy - Home
- Scrapy Basic Concepts
- Scrapy - Overview
- Scrapy - Environment
- Scrapy - Command Line Tools
- Scrapy - Spiders
- Scrapy - Selectors
- Scrapy - Items
- Scrapy - Item Loaders
- Scrapy - Shell
- Scrapy - Item Pipeline
- Scrapy - Feed exports
- Scrapy - Requests & Responses
- Scrapy - Link Extractors
- Scrapy - Settings
- Scrapy - Exceptions
- Scrapy Live Project
- Scrapy - Create a Project
- Scrapy - Define an Item
- Scrapy - First Spider
- Scrapy - Crawling
- Scrapy - Extracting Items
- Scrapy - Using an Item
- Scrapy - Following Links
- Scrapy - Scraped Data
- Scrapy Built In Services
- Scrapy - Logging
- Scrapy - Stats Collection
- Scrapy - Sending an E-mail
- Scrapy - Telnet Console
- Scrapy - Web Services
Scrapy - Sending an E-mail
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 −
|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 = ["firstname.lastname@example.org"], subject = "subject data", body = "body data", cc = ["email@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|
The SMTP host is used for sending the emails. If not, then MAIL_HOST setting will be used.
The address of receiver is used to send the emails. If not, then MAIL_FROM setting will be used.
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.
It specifies the SMTP pass for validation.
It specifies the SMTP port for connection.
It implements using the SMTP STARTTLS.
It administers using a safe SSL connection.
Following two methods are there in the MailSender class reference as specified. First method,
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.
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|
It refers to the email receiver.
It specifies the subject of the email.
It refers to the list of receivers.
It refers to email body data.
It refers to the email's attachment, mimetype of the attachment and name of the attachment.
It represents the MIME type of the e-mail.
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|
It refers to sender email for sending emails.
It refers to SMTP host used for sending emails.
It specifies SMTP port to be used for sending emails.
It refers to SMTP validation. There will be no validation, if this setting is set to disable.
It provides the password used for SMTP validation.
It provides the method of upgrading an insecure connection to a secure connection using SSL/TLS.
It implements the connection using a SSL encrypted connection.
Kickstart Your Career
Get certified by completing the courseGet Started