Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
Middleware
提交
04aba5d3
M
Middleware
项目概览
OpenDocCN
/
Middleware
通知
0
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Middleware
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
04aba5d3
编写于
11月 15, 2019
作者:
片刻小哥哥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加邮件测试文件
上级
ed9ab1ec
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
154 addition
and
1 deletion
+154
-1
src/test/tmp_mail.py
src/test/tmp_mail.py
+78
-0
u_tool/__init__.py
u_tool/__init__.py
+3
-1
u_tool/u_mail.py
u_tool/u_mail.py
+73
-0
未找到文件。
src/test/tmp_mail.py
0 → 100644
浏览文件 @
04aba5d3
import
smtplib
from
email.header
import
Header
from
email.mime.text
import
MIMEText
from
email.mime.multipart
import
MIMEMultipart
# 第三方 SMTP 服务
mail_host
=
"smtp.163.com"
# SMTP服务器
mail_user
=
"test_mail_pk"
# 用户名
mail_pass
=
"pk123321"
# 授权密码,非登录密码
sender
=
'test_mail_pk@163.com'
# 发件人邮箱(最好写全, 不然会失败)
receivers
=
[
'test_mail_pk@163.com'
]
# 接收邮件,可设置为你的QQ邮箱或者其他邮箱
content
=
'我用Python'
title
=
'人生苦短'
# 邮件主题
def
sendEmail
():
message
=
MIMEText
(
content
,
'plain'
,
'utf-8'
)
# 内容, 格式, 编码
message
[
'From'
]
=
"{}"
.
format
(
sender
)
message
[
'To'
]
=
","
.
join
(
receivers
)
message
[
'Subject'
]
=
title
try
:
smtpObj
=
smtplib
.
SMTP_SSL
(
mail_host
)
# smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj
.
login
(
mail_user
,
mail_pass
)
# 登录验证
smtpObj
.
sendmail
(
sender
,
receivers
,
message
.
as_string
())
# 发送
print
(
"mail has been send successfully."
)
smtpObj
.
quit
()
except
smtplib
.
SMTPException
as
e
:
print
(
e
)
def
send_email2
(
SMTP_host
,
from_account
,
from_passwd
,
to_account
,
subject
,
content
):
email_client
=
smtplib
.
SMTP
(
SMTP_host
)
email_client
.
login
(
from_account
,
from_passwd
)
# create msg
msg
=
MIMEText
(
content
,
'plain'
,
'utf-8'
)
msg
[
'Subject'
]
=
Header
(
subject
,
'utf-8'
)
# subject
msg
[
'From'
]
=
from_account
msg
[
'To'
]
=
to_account
email_client
.
sendmail
(
from_account
,
to_account
,
msg
.
as_string
())
email_client
.
quit
()
def
send_email_file
():
message
=
MIMEMultipart
()
message
[
'From'
]
=
"{}"
.
format
(
sender
)
message
[
'To'
]
=
","
.
join
(
receivers
)
message
[
'Subject'
]
=
title
# 发送的文本文件
part1
=
MIMEText
(
"ddddd"
,
'plain'
,
'utf-8'
)
message
.
attach
(
part1
)
# 构造附件1
zipname
=
"data/city.xlsx"
part2
=
MIMEText
(
open
(
zipname
,
'rb'
).
read
(),
'base64'
,
'utf-8'
)
part2
[
"Content-Type"
]
=
'application/octet-stream'
part2
[
"Content-Disposition"
]
=
'attachment; filename=%s'
%
(
zipname
)
message
.
attach
(
part2
)
try
:
smtpObj
=
smtplib
.
SMTP_SSL
(
mail_host
)
# smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj
.
login
(
mail_user
,
mail_pass
)
# 登录验证
smtpObj
.
sendmail
(
sender
,
receivers
,
message
.
as_string
())
# 发送
print
(
"mail has been send successfully."
)
smtpObj
.
quit
()
except
smtplib
.
SMTPException
as
e
:
print
(
e
)
if
__name__
==
'__main__'
:
# sendEmail()
send_email_file
()
# receiver = '***'
# send_email2(mail_host, mail_user, mail_pass, receiver, title, content)
\ No newline at end of file
u_tool/__init__.py
浏览文件 @
04aba5d3
# coding: utf8
from
u_tool.text
import
*
from
u_tool.u_file
import
*
from
u_tool.u_text
import
*
from
u_tool.u_mail
import
*
u_tool/u_mail.py
0 → 100644
浏览文件 @
04aba5d3
import
smtplib
from
email.header
import
Header
from
email.mime.text
import
MIMEText
from
email.mime.multipart
import
MIMEMultipart
class
MailSender
(
object
):
def
__init__
(
self
,
mail_host
=
'smtp.xxx.com'
,
mail_user
=
'xxx'
,
mail_pass
=
'xxx'
,
mail_receivers
=
[
'xxx@xxx.com'
],
mail_postfix
=
'xxx.com'
)
:
# 第三方 SMTP 服务
self
.
mail_host
=
mail_host
# 设置服务器
self
.
mail_user
=
mail_user
# 用户名
self
.
mail_sender
=
"%s<%s@%s>"
%
(
mail_user
,
mail_user
,
mail_postfix
)
# 发件箱的后缀
if
"alibaba"
in
self
.
mail_host
:
self
.
mail_user
=
"%s@%s"
%
(
mail_user
,
mail_postfix
)
# 用户名
self
.
mail_pass
=
mail_pass
# 密码
self
.
mail_postfix
=
mail_postfix
# 发件箱的后缀
self
.
mail_receivers
=
mail_receivers
# 邮件接收人
def
send_mssage
(
self
,
title
,
content
,
zipname
=
""
):
if
zipname
!=
""
:
# message = MIMEMultipart
message
=
MIMEMultipart
(
'alternative'
)
else
:
message
=
MIMEText
(
content
,
'plain'
,
'utf-8'
)
# 内容, 格式, 编码
message
[
'From'
]
=
"{}"
.
format
(
self
.
mail_sender
)
message
[
'To'
]
=
","
.
join
(
self
.
mail_receivers
)
message
[
'Subject'
]
=
title
if
zipname
!=
""
:
# 发送的文本文件
part_text
=
MIMEText
(
content
,
'plain'
,
'utf-8'
)
message
.
attach
(
part_text
)
# 构造附件
part_zip
=
MIMEText
(
open
(
zipname
,
'rb'
).
read
(),
'base64'
,
'utf-8'
)
part_zip
[
"Content-Type"
]
=
'application/octet-stream'
part_zip
[
"Content-Disposition"
]
=
'attachment; filename=%s'
%
(
zipname
.
split
(
"/"
)[
-
1
])
message
.
attach
(
part_zip
)
try
:
if
"alibaba"
in
self
.
mail_host
:
client
=
smtplib
.
SMTP
()
client
.
connect
(
self
.
mail_host
,
25
)
else
:
client
=
smtplib
.
SMTP_SSL
(
self
.
mail_host
)
# client = smtplib.SMTP_SSL(self.mail_host, 465) # 启用SSL发信, 端口一般是465
client
.
login
(
self
.
mail_user
,
self
.
mail_pass
)
# 登录验证
client
.
sendmail
(
self
.
mail_sender
,
self
.
mail_receivers
,
message
.
as_string
())
# 发送
print
(
"mail has been send successfully."
)
client
.
quit
()
return
True
except
Exception
as
e
:
print
(
"Failed"
,
e
)
return
False
# if __name__ == '__main__':
# mail_host = 'smtp.163.com'
# mail_user = 'test_mail_pk'
# # mail_pass='pk123456',
# # pc 密码
# mail_pass = 'pk123321'
# mail_postfix = '163.com'
# mail_receivers = ['test_mail_pk@163.com']
# mail = MailSender(mail_host, mail_user, mail_pass, mail_receivers, mail_postfix)
# status = mail.send_mssage('我是谁', "ddd", "data/poetry.txt")
# if status:
# print("发送成功")
# else:
# print("发送失败")
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录