Sending e-mail
Although Python makes sending e-mails relatively easy via the smtpliblibrary, Scrapy provides its own facility for sending e-mails which is veryeasy to use and it’s implemented using Twisted non-blocking IO, to avoid interfering with the non-blockingIO of the crawler. It also provides a simple API for sending attachments andit’s very easy to configure, with a few settings.
Quick example
There are two ways to instantiate the mail sender. You can instantiate it usingthe standard init
method:
- from scrapy.mail import MailSender
- mailer = MailSender()
Or you can instantiate it passing a Scrapy settings object, which will respectthe settings:
- mailer = MailSender.from_settings(settings)
And here is how to use it to send an e-mail (without attachments):
- mailer.send(to=["[email protected]"], subject="Some subject", body="Some body", cc=["[email protected]"])
MailSender class reference
MailSender is the preferred class to use for sending emails from Scrapy, as ituses Twisted non-blocking IO, like therest of the framework.
- class
scrapy.mail.
MailSender
(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None)[source]
Parameters:
- smtphost (str or bytes) – the SMTP host to use for sending the emails. If omitted, the
MAIL_HOST
setting will be used. - mailfrom (str) – the address used to send emails (in the
From:
header).If omitted, theMAIL_FROM
setting will be used. - smtpuser – the SMTP user. If omitted, the
MAIL_USER
setting will be used. If not given, no SMTP authentication will beperformed. - smtppass (str or bytes) – the SMTP pass for authentication.
- smtpport (int) – the SMTP port to connect to
- smtptls (boolean) – enforce using SMTP STARTTLS
- smtpssl (boolean) – enforce using a secure SSL connection
- classmethod
fromsettings
(_settings)[source] - Instantiate using a Scrapy settings object, which will respectthese Scrapy settings.
Parameters:settings (scrapy.settings.Settings
object) – the e-mail recipients
send
(to, subject, body, cc=None, attachs=(), mimetype='text/plain', charset=None)[source]- Send email to the given recipients.
Parameters:
- **to** ([_str_](https://docs.python.org/3/library/stdtypes.html#str)_ or __list of str_) – the e-mail recipients
- **subject** ([_str_](https://docs.python.org/3/library/stdtypes.html#str)) – the subject of the e-mail
- **cc** ([_str_](https://docs.python.org/3/library/stdtypes.html#str)_ or __list of str_) – the e-mails to CC
- **body** ([_str_](https://docs.python.org/3/library/stdtypes.html#str)) – the e-mail body
- **attachs** (_iterable_) – an iterable of tuples <code>(attach_name, mimetype,
file_object) where attach_name
is a string with the name that willappear on the e-mail’s attachment, mimetype
is the mimetype of theattachment and file_object
is a readable file object with thecontents of the attachment
- **mimetype** ([_str_](https://docs.python.org/3/library/stdtypes.html#str)) – the MIME type of the e-mail
- **charset** ([_str_](https://docs.python.org/3/library/stdtypes.html#str)) – the character encoding to use for the e-mail contents
Mail settings
These settings define the default init
method values of the MailSender
class, and can be used to configure e-mail notifications in your project withoutwriting any code (for those extensions and code that uses MailSender
).
MAIL_FROM
Default: 'scrapy@localhost'
Sender email to use (From:
header) for sending emails.
MAIL_HOST
Default: 'localhost'
SMTP host to use for sending emails.
MAIL_PORT
Default: 25
SMTP port to use for sending emails.
MAIL_USER
Default: None
User to use for SMTP authentication. If disabled no SMTP authentication will beperformed.
MAIL_PASS
Default: None
Password to use for SMTP authentication, along with MAIL_USER
.
MAIL_TLS
Default: False
Enforce using STARTTLS. STARTTLS is a way to take an existing insecure connection, and upgrade it to a secure connection using SSL/TLS.
MAIL_SSL
Default: False
Enforce connecting using an SSL encrypted connection