1. 程式人生 > >python實現發送文本郵件

python實現發送文本郵件

rom file HA info sel login 簡單實現 ring AI

簡單實現了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])

python實現發送文本郵件