41 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Python
		
	
	
	
| """
 | ||
| Author: tianyang.zhang
 | ||
| Date: 2020-03-24 15:36:15
 | ||
| function: 发送邮件
 | ||
| version: 1.0
 | ||
| prameter: email
 | ||
| """
 | ||
| import smtplib
 | ||
| import traceback
 | ||
| from email.utils import formataddr
 | ||
| from email.mime.text import MIMEText
 | ||
| from email.header import Header
 | ||
| 
 | ||
| def sendmessage(subject:str, message:str, u):
 | ||
|     my_pass = 'Pintai123'
 | ||
|     my_sender = 'gxpt@ctc.ac.cn'
 | ||
|     try:
 | ||
|         msg=MIMEText(message, 'plain', 'utf-8')
 | ||
|         # 括号里的对应发件人邮箱昵称、发件人邮箱账号
 | ||
|         msg['From']=formataddr(["国检集团检验检测能力共享平台",my_sender])  
 | ||
|         # 括号里的对应收件人邮箱昵称、收件人邮箱账号
 | ||
|         msg['To']=formataddr(["",u])   
 | ||
|         # 邮件的主题           
 | ||
|         msg['Subject'] = Header(str(subject), 'utf-8').encode()               
 | ||
| 
 | ||
|         # SMTP服务器,腾讯企业邮箱端口是465,腾讯邮箱支持SSL(不强制), 不支持TLS
 | ||
|         # qq邮箱smtp服务器地址:smtp.qq.com,端口号:456
 | ||
|         # 163邮箱smtp服务器地址:smtp.163.com,端口号:25
 | ||
|         server=smtplib.SMTP_SSL("smtp.exmail.qq.com", 465)  
 | ||
|         # 登录服务器,括号中对应的是发件人邮箱账号、邮箱密码
 | ||
|         server.login(my_sender, my_pass)  
 | ||
|         # 发送邮件,括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
 | ||
|         server.sendmail(my_sender,[u,],msg.as_string()) 
 | ||
|         # Message.objects.filter(mail=u).delete()
 | ||
|         # Message.objects.create(mail=u, msg=msg)
 | ||
|     except Exception:
 | ||
|         traceback.print_exc()
 | ||
|     finally:
 | ||
|         #关闭连接
 | ||
|         server.quit()  
 | ||
|     return 'OK' |