1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
from
email.mime.text
import
MIMEText
from
email.header
import
Header
from
smtplib
import
SMTP_SSL
def
send_mail(sender_sina
=
'
',pwd='
',receiver='
',mail_title='
',mail_content='
'):
# 邮箱smtp服务器
host_server
=
'smtp.sina.com'
sender_sina_mail
=
sender_sina
+
'@sina.com'
#ssl登录
smtp
=
SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(
0
)
smtp.ehlo(host_server)
smtp.login(sender_sina, pwd)
msg
=
MIMEText(mail_content,
"plain"
,
'utf-8'
)
msg[
"Subject"
]
=
Header(mail_title,
'utf-8'
)
msg[
"From"
]
=
sender_sina_mail
msg[
"To"
]
=
receiver
smtp.sendmail(sender_sina_mail, receiver, msg.as_string())
smtp.quit()
send_mail(
"hequan2011"
,
"密码"
,
"hequan2011@sina.com"
,
"标题"
,
'内容'
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#!/usr/bin/python
#coding:utf-8
from
email.mime.text
import
MIMEText
from
email.header
import
Header
from
smtplib
import
SMTP_SSL
import
sys
def
send_mail(sender_sina
=
'
',pwd='
',receiver='
',mail_title='
',mail_content='
'):
host_server
=
'smtp.sina.com'
sender_sina_mail
=
sender_sina
+
'@sina.com'
#ssl登录
smtp
=
SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(
0
)
smtp.ehlo(host_server)
smtp.login(sender_sina, pwd)
try
:
msg
=
MIMEText(mail_content,
"plain"
,
'utf-8'
)
msg[
"Subject"
]
=
Header(mail_title,
'utf-8'
)
msg[
"From"
]
=
sender_sina_mail
msg[
"To"
]
=
receiver
smtp.sendmail(sender_sina_mail, receiver, msg.as_string())
smtp.quit()
print
(
"发送成功"
)
return
True
except
Exception as e :
print
(
"发送失败:"
,e)
return
False
if
__name__
=
=
"__main__"
:
#send_mail("hequan2011","密码","hequan2011@sina.com","标题",'内容')
send_mail(
"hequan2011"
,
"密码"
, sys.argv[
1
], sys.argv[
2
], sys.argv[
3
])
|
本文转自 295631788 51CTO博客,原文链接:http://blog.51cto.com/hequan/1939844,如需转载请自行联系原作者