Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
bd16d1cd
S
scrapy
项目概览
looyolo
/
scrapy
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
scrapy
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bd16d1cd
编写于
6月 13, 2010
作者:
P
Pablo Hoffman
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added SMTP-AUTH support to scrapy.mail (closes #149)
上级
495f23de
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
94 addition
and
55 deletion
+94
-55
docs/topics/email.rst
docs/topics/email.rst
+76
-14
docs/topics/settings.rst
docs/topics/settings.rst
+0
-30
scrapy/conf/default_settings.py
scrapy/conf/default_settings.py
+3
-0
scrapy/mail.py
scrapy/mail.py
+15
-11
未找到文件。
docs/topics/email.rst
浏览文件 @
bd16d1cd
...
@@ -8,17 +8,15 @@ Sending e-mail
...
@@ -8,17 +8,15 @@ Sending e-mail
:synopsis: Email sending facility
:synopsis: Email sending facility
Although Python makes sending e-mails relatively easy via the `smtplib`_
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
library, Scrapy provides its own facility for sending e-mails which is very
to use and it's implemented using `Twisted non-blocking IO`_, to avoid
easy
to use and it's implemented using `Twisted non-blocking IO`_, to avoid
interfering with the non-blocking IO of the crawler.
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
It's also very easy to configure, having only a few settings
.
:ref:`settings <topics-email-settings`
.
.. _smtplib: http://docs.python.org/library/smtplib.html
.. _smtplib: http://docs.python.org/library/smtplib.html
.. _Twisted non-blocking IO: http://twistedmatrix.com/projects/core/documentation/howto/async.html
.. _Twisted non-blocking IO: http://twistedmatrix.com/projects/core/documentation/howto/async.html
It also has built-in support for sending attachments.
Quick example
Quick example
=============
=============
...
@@ -35,7 +33,7 @@ MailSender class reference
...
@@ -35,7 +33,7 @@ MailSender class reference
MailSender is the preferred class to use for sending emails from Scrapy, as it
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.
uses `Twisted non-blocking IO`_, like the rest of the framework.
.. class:: MailSender(smtphost
, mailfrom)
.. class:: MailSender(smtphost
=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None):
:param smtphost: the SMTP host to use for sending the emails. If omitted, the
:param smtphost: the SMTP host to use for sending the emails. If omitted, the
:setting:`MAIL_HOST` setting will be used.
:setting:`MAIL_HOST` setting will be used.
...
@@ -45,6 +43,17 @@ uses `Twisted non-blocking IO`_, like the rest of the framework.
...
@@ -45,6 +43,17 @@ uses `Twisted non-blocking IO`_, like the rest of the framework.
If omitted, the :setting:`MAIL_FROM` setting will be used.
If omitted, the :setting:`MAIL_FROM` setting will be used.
:type mailfrom: str
: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.
:type smtphost: str
:param smtppass: the SMTP pass for authetnication.
:type smtppass: str
:param smtpport: the SMTP port to connect to
:type smtpport: int
.. method:: send(to, subject, body, cc=None, attachs=())
.. method:: send(to, subject, body, cc=None, attachs=())
Send email to the given recipients. Emits the :signal:`mail_sent` signal.
Send email to the given recipients. Emits the :signal:`mail_sent` signal.
...
@@ -72,16 +81,69 @@ uses `Twisted non-blocking IO`_, like the rest of the framework.
...
@@ -72,16 +81,69 @@ uses `Twisted non-blocking IO`_, like the rest of the framework.
:type attachs: iterable
:type attachs: iterable
MailSender settings
.. _topics-email-settings:
===================
Mail settings
=============
These settings define the default constructor values of the :class:`MailSender`
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
class, and can be used to configure e-mail notifications in your project without
writing any code (for those extensions that use the :class:`MailSender` class):
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`.
.. setting:: MAIL_DEBUG
MAIL_DEBUG
----------
Default: ``False``
* :setting:`MAIL_DEBUG`
Whether to enable the debugging mode.
* :setting:`MAIL_FROM`
* :setting:`MAIL_HOST`
Mail signals
Mail signals
...
...
docs/topics/settings.rst
浏览文件 @
bd16d1cd
...
@@ -615,36 +615,6 @@ If ``True``, all standard output (and error) of your process will be redirected
...
@@ -615,36 +615,6 @@ If ``True``, all standard output (and error) of your process will be redirected
to the log. For example if you ``print 'hello'`` it will appear in the Scrapy
to the log. For example if you ``print 'hello'`` it will appear in the Scrapy
log.
log.
.. setting:: MAIL_DEBUG
MAIL_DEBUG
----------
Default: ``False``
Whether to enable the debugging mode in the the :ref:`Scrapy e-mail sending
facility <topics-email>`.
.. setting:: MAIL_FROM
MAIL_FROM
---------
Default: ``'scrapy@localhost'``
Email to use as sender address for sending emails using the :ref:`Scrapy e-mail
sending facility <topics-email>`.
.. setting:: MAIL_HOST
MAIL_HOST
---------
Default: ``'localhost'``
Host to use for sending emails using the :ref:`Scrapy e-mail sending facility
<topics-email>`.
.. setting:: MEMDEBUG_ENABLED
.. setting:: MEMDEBUG_ENABLED
MEMDEBUG_ENABLED
MEMDEBUG_ENABLED
...
...
scrapy/conf/default_settings.py
浏览文件 @
bd16d1cd
...
@@ -138,7 +138,10 @@ LOG_FILE = None
...
@@ -138,7 +138,10 @@ LOG_FILE = None
MAIL_DEBUG
=
False
MAIL_DEBUG
=
False
MAIL_HOST
=
'localhost'
MAIL_HOST
=
'localhost'
MAIL_PORT
=
25
MAIL_FROM
=
'scrapy@localhost'
MAIL_FROM
=
'scrapy@localhost'
MAIL_PASS
=
None
MAIL_USER
=
None
MEMDEBUG_ENABLED
=
False
# enable memory debugging
MEMDEBUG_ENABLED
=
False
# enable memory debugging
MEMDEBUG_NOTIFY
=
[]
# send memory debugging report by mail at engine shutdown
MEMDEBUG_NOTIFY
=
[]
# send memory debugging report by mail at engine shutdown
...
...
scrapy/mail.py
浏览文件 @
bd16d1cd
...
@@ -12,7 +12,7 @@ from email.Utils import COMMASPACE, formatdate
...
@@ -12,7 +12,7 @@ from email.Utils import COMMASPACE, formatdate
from
email
import
Encoders
from
email
import
Encoders
from
twisted.internet
import
defer
,
reactor
from
twisted.internet
import
defer
,
reactor
from
twisted.mail.smtp
import
SMTPSenderFactory
from
twisted.mail.smtp
import
E
SMTPSenderFactory
from
scrapy
import
log
from
scrapy
import
log
from
scrapy.core.exceptions
import
NotConfigured
from
scrapy.core.exceptions
import
NotConfigured
...
@@ -27,9 +27,13 @@ mail_sent = object()
...
@@ -27,9 +27,13 @@ mail_sent = object()
class
MailSender
(
object
):
class
MailSender
(
object
):
def
__init__
(
self
,
smtphost
=
None
,
mailfrom
=
None
):
def
__init__
(
self
,
smtphost
=
None
,
mailfrom
=
None
,
smtpuser
=
None
,
smtppass
=
None
,
\
self
.
smtphost
=
smtphost
if
smtphost
else
settings
[
'MAIL_HOST'
]
smtpport
=
None
):
self
.
mailfrom
=
mailfrom
if
mailfrom
else
settings
[
'MAIL_FROM'
]
self
.
smtphost
=
smtphost
or
settings
[
'MAIL_HOST'
]
self
.
smtpport
=
smtpport
or
settings
.
getint
(
'MAIL_PORT'
)
self
.
smtpuser
=
smtpuser
or
settings
[
'MAIL_USER'
]
self
.
smtppass
=
smtppass
or
settings
[
'MAIL_PASS'
]
self
.
mailfrom
=
mailfrom
or
settings
[
'MAIL_FROM'
]
if
not
self
.
smtphost
or
not
self
.
mailfrom
:
if
not
self
.
smtphost
or
not
self
.
mailfrom
:
raise
NotConfigured
(
"MAIL_HOST and MAIL_FROM settings are required"
)
raise
NotConfigured
(
"MAIL_HOST and MAIL_FROM settings are required"
)
...
@@ -68,11 +72,12 @@ class MailSender(object):
...
@@ -68,11 +72,12 @@ class MailSender(object):
(
to
,
cc
,
subject
,
len
(
attachs
)),
level
=
log
.
DEBUG
)
(
to
,
cc
,
subject
,
len
(
attachs
)),
level
=
log
.
DEBUG
)
return
return
dfd
=
self
.
_sendmail
(
self
.
smtphost
,
self
.
mailfrom
,
rcpts
,
msg
.
as_string
())
dfd
=
self
.
_sendmail
(
rcpts
,
msg
.
as_string
())
dfd
.
addCallbacks
(
self
.
_sent_ok
,
self
.
_sent_failed
,
dfd
.
addCallbacks
(
self
.
_sent_ok
,
self
.
_sent_failed
,
callbackArgs
=
[
to
,
cc
,
subject
,
len
(
attachs
)],
callbackArgs
=
[
to
,
cc
,
subject
,
len
(
attachs
)],
errbackArgs
=
[
to
,
cc
,
subject
,
len
(
attachs
)])
errbackArgs
=
[
to
,
cc
,
subject
,
len
(
attachs
)])
reactor
.
addSystemEventTrigger
(
'before'
,
'shutdown'
,
lambda
:
dfd
)
reactor
.
addSystemEventTrigger
(
'before'
,
'shutdown'
,
lambda
:
dfd
)
return
dfd
def
_sent_ok
(
self
,
result
,
to
,
cc
,
subject
,
nattachs
):
def
_sent_ok
(
self
,
result
,
to
,
cc
,
subject
,
nattachs
):
log
.
msg
(
'Mail sent OK: To=%s Cc=%s Subject="%s" Attachs=%d'
%
\
log
.
msg
(
'Mail sent OK: To=%s Cc=%s Subject="%s" Attachs=%d'
%
\
...
@@ -83,13 +88,12 @@ class MailSender(object):
...
@@ -83,13 +88,12 @@ class MailSender(object):
log
.
msg
(
'Unable to send mail: To=%s Cc=%s Subject="%s" Attachs=%d - %s'
%
\
log
.
msg
(
'Unable to send mail: To=%s Cc=%s Subject="%s" Attachs=%d - %s'
%
\
(
to
,
cc
,
subject
,
nattachs
,
errstr
),
level
=
log
.
ERROR
)
(
to
,
cc
,
subject
,
nattachs
,
errstr
),
level
=
log
.
ERROR
)
def
_sendmail
(
self
,
smtphost
,
from_addr
,
to_addrs
,
msg
,
port
=
25
):
def
_sendmail
(
self
,
to_addrs
,
msg
):
""" This is based on twisted.mail.smtp.sendmail except that it
instantiates a quiet (noisy=False) SMTPSenderFactory """
msg
=
StringIO
(
msg
)
msg
=
StringIO
(
msg
)
d
=
defer
.
Deferred
()
d
=
defer
.
Deferred
()
factory
=
SMTPSenderFactory
(
from_addr
,
to_addrs
,
msg
,
d
)
factory
=
ESMTPSenderFactory
(
self
.
smtpuser
,
self
.
smtppass
,
self
.
mailfrom
,
\
to_addrs
,
msg
,
d
,
heloFallback
=
True
,
requireAuthentication
=
False
,
\
requireTransportSecurity
=
False
)
factory
.
noisy
=
False
factory
.
noisy
=
False
reactor
.
connectTCP
(
s
mtphost
,
port
,
factory
)
reactor
.
connectTCP
(
s
elf
.
smtphost
,
self
.
smtp
port
,
factory
)
return
d
return
d
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录