Sending email

Although Python makes sending e-mail relatively easy via the smtplib library, Scrapy provides its own class for sending emails which is very easy to use and it’s implemented using Twisted non-blocking IO, to avoid affecting the crawling performance.

It also has built-in support for sending attachments.

Quick example

Here’s a quick example of how to send an email (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.

class scrapy.mail.MailSender(smtphost, mailfrom)

smtphost is a string with the SMTP host to use for sending the emails. If omitted, MAIL_HOST will be used.

mailfrom is a string with the email address to use for sending messages (in the From: header). If omitted, MAIL_FROM will be used.

MailSender.send(to, subject, body, cc=None, attachs=())

Send mail to the given recipients

to is a list of email recipients

subject is a string with the subject of the message

cc is a list of emails to CC

body is a string with the body of the message

attachs is an iterable of tuples (attach_name, mimetype, file_object) where:

attach_name is a string with the name will appear on the emails attachment mimetype is the mimetype of the attachment file_object is a readable file object

Table Of Contents

Previous topic

Stats Collection

Next topic

Telnet Console

This Page