Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
looyolo
scrapy
提交
061c6359
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,发现更多精彩内容 >>
提交
061c6359
编写于
1月 04, 2016
作者:
N
Nicolas Pennequin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MailSender.send: allow passing a charset.
Resolves Issue #348
上级
8f269558
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
55 addition
and
2 deletion
+55
-2
scrapy/mail.py
scrapy/mail.py
+5
-2
tests/test_mail.py
tests/test_mail.py
+50
-0
未找到文件。
scrapy/mail.py
浏览文件 @
061c6359
...
...
@@ -43,7 +43,7 @@ class MailSender(object):
settings
[
'MAIL_PASS'
],
settings
.
getint
(
'MAIL_PORT'
),
settings
.
getbool
(
'MAIL_TLS'
),
settings
.
getbool
(
'MAIL_SSL'
))
def
send
(
self
,
to
,
subject
,
body
,
cc
=
None
,
attachs
=
(),
mimetype
=
'text/plain'
,
_callback
=
None
):
def
send
(
self
,
to
,
subject
,
body
,
cc
=
None
,
attachs
=
(),
mimetype
=
'text/plain'
,
charset
=
None
,
_callback
=
None
):
if
attachs
:
msg
=
MIMEMultipart
()
else
:
...
...
@@ -57,8 +57,11 @@ class MailSender(object):
rcpts
.
extend
(
cc
)
msg
[
'Cc'
]
=
COMMASPACE
.
join
(
cc
)
if
charset
:
msg
.
set_charset
(
charset
)
if
attachs
:
msg
.
attach
(
MIMEText
(
body
))
msg
.
attach
(
MIMEText
(
body
,
'plain'
,
charset
or
'us-ascii'
))
for
attach_name
,
mimetype
,
f
in
attachs
:
part
=
MIMEBase
(
*
mimetype
.
split
(
'/'
))
part
.
set_payload
(
f
.
read
())
...
...
tests/test_mail.py
浏览文件 @
061c6359
# coding=utf-8
import
unittest
from
io
import
BytesIO
from
email.charset
import
Charset
from
scrapy.mail
import
MailSender
...
...
@@ -54,11 +57,58 @@ class MailSenderTest(unittest.TestCase):
text
,
attach
=
payload
self
.
assertEqual
(
text
.
get_payload
(
decode
=
True
),
b
'body'
)
self
.
assertEqual
(
text
.
get_charset
(),
Charset
(
'us-ascii'
))
self
.
assertEqual
(
attach
.
get_payload
(
decode
=
True
),
b
'content'
)
def
_catch_mail_sent
(
self
,
**
kwargs
):
self
.
catched_msg
=
dict
(
**
kwargs
)
def
test_send_utf8
(
self
):
subject
=
u
'sübjèçt'
body
=
u
'bödÿ-àéïöñß'
mailsender
=
MailSender
(
debug
=
True
)
mailsender
.
send
(
to
=
[
'test@scrapy.org'
],
subject
=
subject
,
body
=
body
,
charset
=
'utf-8'
,
_callback
=
self
.
_catch_mail_sent
)
assert
self
.
catched_msg
self
.
assertEqual
(
self
.
catched_msg
[
'subject'
],
subject
)
self
.
assertEqual
(
self
.
catched_msg
[
'body'
],
body
)
msg
=
self
.
catched_msg
[
'msg'
]
self
.
assertEqual
(
msg
[
'subject'
],
subject
)
self
.
assertEqual
(
msg
.
get_payload
(),
body
)
self
.
assertEqual
(
msg
.
get_charset
(),
Charset
(
'utf-8'
))
self
.
assertEqual
(
msg
.
get
(
'Content-Type'
),
'text/plain; charset="utf-8"'
)
def
test_send_attach_utf8
(
self
):
subject
=
u
'sübjèçt'
body
=
u
'bödÿ-àéïöñß'
attach
=
BytesIO
()
attach
.
write
(
body
.
encode
(
'utf-8'
))
attach
.
seek
(
0
)
attachs
=
[(
'attachment'
,
'text/plain'
,
attach
)]
mailsender
=
MailSender
(
debug
=
True
)
mailsender
.
send
(
to
=
[
'test@scrapy.org'
],
subject
=
subject
,
body
=
body
,
attachs
=
attachs
,
charset
=
'utf-8'
,
_callback
=
self
.
_catch_mail_sent
)
assert
self
.
catched_msg
self
.
assertEqual
(
self
.
catched_msg
[
'subject'
],
subject
)
self
.
assertEqual
(
self
.
catched_msg
[
'body'
],
body
)
msg
=
self
.
catched_msg
[
'msg'
]
self
.
assertEqual
(
msg
[
'subject'
],
subject
)
self
.
assertEqual
(
msg
.
get_charset
(),
Charset
(
'utf-8'
))
self
.
assertEqual
(
msg
.
get
(
'Content-Type'
),
'multipart/mixed; charset="utf-8"'
)
payload
=
msg
.
get_payload
()
assert
isinstance
(
payload
,
list
)
self
.
assertEqual
(
len
(
payload
),
2
)
text
,
attach
=
payload
self
.
assertEqual
(
text
.
get_payload
(
decode
=
True
).
decode
(
'utf-8'
),
body
)
self
.
assertEqual
(
text
.
get_charset
(),
Charset
(
'utf-8'
))
self
.
assertEqual
(
attach
.
get_payload
(
decode
=
True
).
decode
(
'utf-8'
),
body
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录