Sending e-mail¶
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 <topics-email-settings.
Quick example¶
Here’s a quick example of how to send an e-mail (without attachments):
from scrapy.mail import MailSender
mailer = MailSender()
mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@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.
-
MailSender(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None):
Parameters: - smtphost (str) – 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 be performed. - smtppass (str) – the SMTP pass for authetnication.
- smtpport (int) – the SMTP port to connect to
-
scrapy.mail.
send
(to, subject, body, cc=None, attachs=())¶ Send email to the given recipients. Emits the
mail_sent
signal.Parameters: - to (list) – the e-mail recipients
- subject (str) – the subject of the e-mail
- cc (list) – the e-mails to CC
- body (str) – the e-mail body
- attachs (iterable) – an iterable of tuples
(attach_name, mimetype, file_object)
whereattach_name
is a string with the name that will appear on the e-mail’s attachment,mimetype
is the mimetype of the attachment andfile_object
is a readable file object with the contents of the attachment
- smtphost (str) – the SMTP host to use for sending the emails. If omitted, the
Mail settings¶
These settings define the default constructor 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 MailSender
).
MAIL_USER¶
Default: None
User to use for SMTP authentication. If disabled no SMTP authentication will be performed.
Mail signals¶
-
scrapy.mail.
mail_sent
(to, subject, body, cc, attachs, msg)¶ Emitted by
MailSender.send()
after an email has been sent.Parameters: - to (list) – the e-mail recipients
- subject (str) – the subject of the e-mail
- cc (list) – the e-mails to CC
- body (str) – the e-mail body
- attachs (iterable) – an iterable of tuples
(attach_name, mimetype, file_object)
whereattach_name
is a string with the name that will appear on the e-mail’s attachment,mimetype
is the mimetype of the attachment andfile_object
is a readable file object with the contents of the attachment - msg (
MIMEMultipart
orMIMENonMultipart
) – the generated message