scrapy/docs/topics/email.rst

4.4 KiB

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

Sending e-mail

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

Unknown directive type "module".

.. module:: scrapy.mail
   :synopsis: Email sending facility

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 :ref:`settings <topics-email-settings`.

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

Unknown interpreted text role "ref".

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.

param smtphost:

the SMTP host to use for sending the emails. If omitted, the :setting:`MAIL_HOST` setting will be used.

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

Unknown interpreted text role "setting".

type smtphost:

str

param mailfrom:

the address used to send emails (in the From: header). If omitted, the :setting:`MAIL_FROM` setting will be used.

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

Unknown interpreted text role "setting".

type mailfrom:

str

param smtpuser:

the SMTP user. If omitted, the :setting:`MAIL_USER` setting will be used. If not given, no SMTP authentication will be performed.

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

Unknown interpreted text role "setting".

type smtphost:

str

param smtppass:

the SMTP pass for authetnication.

type smtppass:

str

param smtpport:

the SMTP port to connect to

type smtpport:

int

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

Unknown directive type "method".

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

    Send email to the given recipients. Emits the :signal:`mail_sent` signal.

    :param to: the e-mail recipients
    :type to: list

    :param subject: the subject of the e-mail
    :type subject: str

    :param cc: the e-mails to CC
    :type cc: list

    :param body: the e-mail body
    :type body: str

    :param attachs: an iterable of tuples ``(attach_name, mimetype,
      file_object)`` where  ``attach_name`` is a string with the name that will
      appear on the e-mail's attachment, ``mimetype`` is the mimetype of the
      attachment and ``file_object`` is a readable file object with the
      contents of the attachment
    :type attachs: iterable

Mail settings

These settings define the default constructor values of the :class:`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 :class:`MailSender`).

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

Unknown interpreted text role "class".

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

Unknown interpreted text role "class".

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

Unknown directive type "setting".

.. setting:: MAIL_FROM

MAIL_FROM

Default: 'scrapy@localhost'

Sender email to use (From: header) for sending emails.

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

Unknown directive type "setting".

.. setting:: MAIL_HOST

MAIL_HOST

Default: 'localhost'

SMTP host to use for sending emails.

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

Unknown directive type "setting".

.. setting:: MAIL_PORT

MAIL_PORT

Default: 25

SMTP port to use for sending emails.

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

Unknown directive type "setting".

.. setting:: MAIL_USER

MAIL_USER

Default: None

User to use for SMTP authentication. If disabled no SMTP authentication will be performed.

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

Unknown directive type "setting".

.. setting:: MAIL_PASS

MAIL_PASS

Default: None

Password to use for SMTP authentication, along with :setting:`MAIL_USER`.

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

Unknown interpreted text role "setting".

Mail signals

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

Unknown directive type "signal".

.. signal:: mail_sent

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

Unknown directive type "function".

.. function:: mail_sent(to, subject, body, cc, attachs, msg)

  Emitted by :meth:`MailSender.send` after an email has been sent.

  :param to: the e-mail recipients
  :type to: list

  :param subject: the subject of the e-mail
  :type subject: str

  :param cc: the e-mails to CC
  :type cc: list

  :param body: the e-mail body
  :type body: str

  :param attachs: an iterable of tuples ``(attach_name, mimetype,
    file_object)`` where  ``attach_name`` is a string with the name that will
    appear on the e-mail's attachment, ``mimetype`` is the mimetype of the
    attachment and ``file_object`` is a readable file object with the
    contents of the attachment
  :type attachs: iterable

  :param msg: the generated message
  :type msg: ``MIMEMultipart`` or ``MIMENonMultipart``
</html>