python实现发送文本邮件

简单实现了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 = '[email protected]'
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', '[email protected]', '******', '[email protected]')

 

转载于:https://www.cnblogs.com/bluezms/p/8955343.html

你可能感兴趣的:(python实现发送文本邮件)