Sending e-mail
There are two ways to instantiate the mail sender. You can instantiate it using the standard method:
Or you can instantiate it passing a Scrapy settings object, which will respect the settings:
And here is how to use it to send an e-mail (without attachments):
MailSender is the preferred class to use for sending emails from Scrapy, as it uses , like the rest of the framework.
class scrapy.mail.MailSender(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None)[source]
Parameters
smtphost ( or bytes) – the SMTP host to use for sending the emails. If omitted, the setting will be used.
mailfrom (str) – the address used to send emails (in the
From:
header). If omitted, the 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 ( or bytes) – the SMTP pass for authentication.
smtpport () – the SMTP port to connect to
smtptls (bool) – enforce using SMTP STARTTLS
smtpssl () – enforce using a secure SSL connection
-
Instantiate using a Scrapy settings object, which will respect these Scrapy settings.
send(to, subject, body, cc=None, attachs=(), mimetype=’text/plain’, charset=None)
Send email to the given recipients.
Parameters
to (str or ) – the e-mail recipients as a string or as a list of strings
subject (str) – the subject of the e-mail
cc ( or list) – the e-mails to CC as a string or as a list of strings
body () – the e-mail body
attachs (collections.abc.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 and is a readable file object with the contents of the attachmentmimetype () – the MIME type of the e-mail
charset (str) – the character encoding to use for the e-mail contents
Default: 'scrapy@localhost'
Sender email to use (From:
header) for sending emails.
MAIL_HOST
Default: 'localhost'
SMTP host to use for sending emails.
Default:
SMTP port to use for sending emails.
MAIL_USER
Default: None
User to use for SMTP authentication. If disabled no SMTP authentication will be performed.
Default: None
Password to use for SMTP authentication, along with .
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.
Default: False