email.rst 4.8 KB
Newer Older
1
.. _topics-email:
P
Pablo Hoffman 已提交
2

3
==============
4
Sending e-mail
5
==============
P
Pablo Hoffman 已提交
6 7

.. module:: scrapy.mail
8
   :synopsis: Email sending facility
P
Pablo Hoffman 已提交
9

10
Although Python makes sending e-mails relatively easy via the `smtplib`_
11 12 13 14
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
15
:ref:`settings <topics-email-settings>`.
P
Pablo Hoffman 已提交
16

17
.. _smtplib: https://docs.python.org/2/library/smtplib.html
N
nyov 已提交
18
.. _Twisted non-blocking IO: https://twistedmatrix.com/documents/current/core/howto/defer-intro.html
P
Pablo Hoffman 已提交
19 20 21 22

Quick example
=============

23 24
There are two ways to instantiate the mail sender. You can instantiate it using
the standard constructor::
P
Pablo Hoffman 已提交
25 26 27

    from scrapy.mail import MailSender
    mailer = MailSender()
28 29 30 31 32 33 34 35

Or you can instantiate it passing a Scrapy settings object, which will respect
the :ref:`settings <topics-email-settings>`::

    mailer = MailSender.from_settings(settings)

And here is how to use it to send an e-mail (without attachments)::

36
    mailer.send(to=["someone@example.com"], subject="Some subject", body="Some body", cc=["another@example.com"])
P
Pablo Hoffman 已提交
37

38 39
MailSender class reference
==========================
P
Pablo Hoffman 已提交
40

41
MailSender is the preferred class to use for sending emails from Scrapy, as it
42
uses `Twisted non-blocking IO`_, like the rest of the framework.
P
Pablo Hoffman 已提交
43

44
.. class:: MailSender(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None)
P
Pablo Hoffman 已提交
45

46
    :param smtphost: the SMTP host to use for sending the emails. If omitted, the
47 48
      :setting:`MAIL_HOST` setting will be used.
    :type smtphost: str
P
Pablo Hoffman 已提交
49

50 51 52
    :param mailfrom: the address used to send emails (in the ``From:`` header).
      If omitted, the :setting:`MAIL_FROM` setting will be used.
    :type mailfrom: str
P
Pablo Hoffman 已提交
53

54 55 56 57 58
    :param smtpuser: the SMTP user. If omitted, the :setting:`MAIL_USER`
      setting will be used. If not given, no SMTP authentication will be
      performed.
    :type smtphost: str

59
    :param smtppass: the SMTP pass for authentication.
60 61 62 63 64
    :type smtppass: str

    :param smtpport: the SMTP port to connect to
    :type smtpport: int

65
    :param smtptls: enforce using SMTP STARTTLS
66
    :type smtptls: boolean
67 68

    :param smtpssl: enforce using a secure SSL connection
69
    :type smtpssl: boolean
70

71 72 73 74 75 76 77 78
    .. classmethod:: from_settings(settings)

        Instantiate using a Scrapy settings object, which will respect
        :ref:`these Scrapy settings <topics-email-settings>`.

        :param settings: the e-mail recipients
        :type settings: :class:`scrapy.settings.Settings` object

79
    .. method:: send(to, subject, body, cc=None, attachs=(), mimetype='text/plain', charset=None)
P
Pablo Hoffman 已提交
80

81
        Send email to the given recipients.
82

83 84
        :param to: the e-mail recipients
        :type to: list
P
Pablo Hoffman 已提交
85

86 87
        :param subject: the subject of the e-mail
        :type subject: str
P
Pablo Hoffman 已提交
88

89 90
        :param cc: the e-mails to CC
        :type cc: list
P
Pablo Hoffman 已提交
91

92 93
        :param body: the e-mail body
        :type body: str
P
Pablo Hoffman 已提交
94

95 96 97 98 99 100
        :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
101

N
Nikita Nikishin 已提交
102 103 104
        :param mimetype: the MIME type of the e-mail
        :type mimetype: str

105 106 107
        :param charset: the character encoding to use for the e-mail contents
        :type charset: str

P
Pablo Hoffman 已提交
108

109 110 111 112
.. _topics-email-settings:

Mail settings
=============
113 114 115

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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
writing any code (for those extensions and code that uses :class:`MailSender`).

.. setting:: MAIL_FROM

MAIL_FROM
---------

Default: ``'scrapy@localhost'``

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

.. setting:: MAIL_HOST

MAIL_HOST
---------

Default: ``'localhost'``

SMTP host to use for sending emails.

.. setting:: MAIL_PORT

MAIL_PORT
---------

Default: ``25``

SMTP port to use for sending emails.

.. setting:: MAIL_USER

MAIL_USER
---------

Default: ``None``

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

.. setting:: MAIL_PASS

MAIL_PASS
---------

Default: ``None``

Password to use for SMTP authentication, along with :setting:`MAIL_USER`.
163 164 165 166

.. setting:: MAIL_TLS

MAIL_TLS
A
Aron Bordin 已提交
167
--------
168 169 170 171 172 173 174 175

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.

.. setting:: MAIL_SSL

MAIL_SSL
A
Aron Bordin 已提交
176
--------
177 178 179 180

Default: ``False``

Enforce connecting using an SSL encrypted connection