A simple way to send emails in Python

A simple way to send emails in Python

# send mail

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.header import Header

def sendEmail(smtpServer, sendAddr, recvAddr, muser, mpass, sendSub, sendMsg):
	msg = MIMEText(sendMsg, 'plain', 'utf-8')
	msg['From'] = Header(sendAddr, 'utf-8')
	msg['To'] =  Header(recvAddr, 'utf-8')
	msg['Subject'] = Header(sendSub, 'utf-8')

	try:
		smtpObj = smtplib.SMTP() 
		smtpObj.connect(smtpServer, 25)
		smtpObj.login(muser, mpass)  
		smtpObj.sendmail(sendAddr, recvAddr, msg.as_string())
		return True
	except smtplib.SMTPException:
		return False
		

sendEmail("smtp.qq.com", "[email protected]", ['[email protected]'], "user1", "password1", "email-test!", "Hello email test !")	

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example