scrapy/docs/ref/email.rst

2.0 KiB

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> </head>

Sending email

System Message: ERROR/3 (<stdin>, line 7)

Unknown directive type "module".

.. module:: scrapy.mail
   :synopsis: Helpers to easily send e-mail.

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.

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

System Message: ERROR/3 (<stdin>, line 37); backlink

Unknown interpreted text role "setting".

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

System Message: ERROR/3 (<stdin>, line 40); backlink

Unknown interpreted text role "setting".

System Message: ERROR/3 (<stdin>, line 43)

Unknown directive type "method".

.. method:: 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


</html>