Although Python makes sending e-mails relatively easy via the
smtplib library, Scrapy provides its own facility for sending e-mails which is very easy to use and it’s implemented using Twisted non-blocking IO, to avoid interfering with the non-blocking IO of the crawler. It also provides a simple API for sending attachments and it’s very easy to configure, with a few settings.
There are two ways to instantiate the mail sender. You can instantiate it using the standard
from scrapy.mail import MailSender
mailer = MailSender()
Or you can instantiate it passing a Scrapy settings object, which will respect the settings:
mailer = MailSender.from_settings(settings)
And here is how to use it to send an e-mail (without attachments):
mailer.send(to=["firstname.lastname@example.org"], subject="Some subject", body="Some body", cc=["email@example.com"])
MailSender class reference
MailSender is the preferred class to use for sending emails from Scrapy, as it uses Twisted non-blocking IO, like the rest of the framework.
scrapy.mail.``MailSender(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None)[source]
smtphost (str or bytes) – the SMTP host to use for sending the emails. If omitted, the
MAIL_HOSTsetting will be used.
mailfrom (str) – the address used to send emails (in the
From:header). If omitted, the
MAIL_FROMsetting will be used.
smtpuser – the SMTP user. If omitted, the
MAIL_USERsetting will be used. If not given, no SMTP authentication will be performed.
smtpport (int) – the SMTP port to connect to
smtptls (bool) – enforce using SMTP STARTTLS
smtpssl (bool) – enforce using a secure SSL connection
Instantiate using a Scrapy settings object, which will respect these Scrapy settings.
scrapy.settings.Settingsobject) – the e-mail recipients
send(to, subject, body, cc=None, attachs=(), mimetype=’text/plain’, charset=None)[source]
Send email to the given recipients.
to (str or list) – the e-mail recipients as a string or as a list of strings
subject (str) – the subject of the e-mail
cc (str or list) – the e-mails to CC as a string or as a list of strings
body (str) – the e-mail body
attachs (collections.abc.Iterable) – an iterable of tuples
(attach_name, mimetype, file_object)where
attach_nameis a string with the name that will appear on the e-mail’s attachment,
mimetypeis the mimetype of the attachment and
file_objectis a readable file object with the contents of the attachment
mimetype (str) – the MIME type of the e-mail
charset (str) – the character encoding to use for the e-mail contents
These settings define the default
__init__ method values of the
MailSender class, and can be used to configure e-mail notifications in your project without writing any code (for those extensions and code that uses
Sender email to use (
From: header) for sending emails.
SMTP host to use for sending emails.
SMTP port to use for sending emails.
User to use for SMTP authentication. If disabled no SMTP authentication will be performed.
Password to use for SMTP authentication, along with
Enforce using STARTTLS. STARTTLS is a way to take an existing insecure connection, and upgrade it to a secure connection using SSL/TLS.
Enforce connecting using an SSL encrypted connection