码迷,mamicode.com
首页 > 编程语言 > 详细

python实现发送文本邮件

时间:2018-04-27 02:44:35      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:end   har   site   ext   sendmail   inf   string   rom   ring   

简单实现了python发送文本邮件

 

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3 
 4 # @Time    : 2018/4/25 17:09
 5 # @Author  : zms
 6 # @Site    : 
 7 # @File    : SendEmail.py
 8 # @Software: PyCharm Community Edition
 9 
10 import time
11 from email.mime.text import MIMEText
12 import smtplib
13 
14 
15 class SendEmail(object):
16     def __init__(self):
17         self.port = 25
18         self.fromemail = test@test.com
19         self.emailpasswd = ******
20 
21     def sendemail(self, subject, msg, fromemail=None, emailpasswd=None, toemail=None):
22         ‘‘‘实现发送邮件功能函数‘‘‘
23         if fromemail == None | emailpasswd == None:
24             _user = self.fromemail
25             _pwd = self.emailpasswd
26         else:
27             _user = fromemail
28             _pwd = emailpasswd
29         _to = toemail
30         nowtime = time.strftime(%Y-%m-%d %H:%M:%S)
31 
32         msg = MIMEText(msg)
33         msg["Subject"] = subject
34         msg["From"] = _user
35         msg["To"] = _to
36 
37         try:
38             # s = smtplib.SMTP_SSL(‘****.****.cn‘, 25)
39             s = smtplib.SMTP(‘****.****.cn, self.port)
40             s.login(_user, _pwd)
41             s.sendmail(_user, _to, msg.as_string())
42             s.quit()
43             print "[%s]INFO:%s Email send Success!" % (nowtime, _to)
44         except smtplib.SMTPException, e:
45             print "[%s]ERROR:%s Email send Falied,%s" % ((nowtime, e), _to)
46 
47 
48 if __name__ == __main__:
49     email = SendEmail()
50     email.sendemail(test, test, test@test.com, ******, test@test.com)

 

python实现发送文本邮件

标签:end   har   site   ext   sendmail   inf   string   rom   ring   

原文地址:https://www.cnblogs.com/bluezms/p/8955343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!