python之路14发送邮件实例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之路14发送邮件实例相关的知识,希望对你有一定的参考价值。
1、发邮件的代码
from email.mime.text import MIMEText from email.utils import formataddr import smtplib msg = MIMEText(‘邮件内容‘,‘plain‘,‘utf-8‘) msg[‘from‘] = formataddr([‘sunshuhai‘,‘[email protected]‘]) msg[‘to‘] = formataddr([‘走人‘,‘[email protected]‘]) msg[‘Subject‘] = ‘主题‘ server = smtplib.SMTP(‘smtp.qq.com‘,25) server.login("[email protected]","123456") server.sendmail(‘[email protected]‘,[‘[email protected]‘,],msg.as_string()) server.quit()
2、将代码整理为函数
#发送者邮箱地址,接收着邮箱地址,主题,内容,是否发送成功 def send_email(send,receive,subject,concent): from email.mime.text import MIMEText from email.utils import formataddr import smtplib ret = True try: msg = MIMEText(‘邮件内容‘,‘plain‘,‘utf-8‘) msg[‘from‘] = formataddr([send]) msg[‘to‘] = formataddr([‘走人‘,send]) msg[‘Subject‘] = subject server = smtplib.SMTP(‘smtp.qq.com‘,25) server.login(send,"123456") server.sendmail(send,[receive,],msg.as_string()) server.quit() except: ret = False return ret #调用函数 ret = send_email(‘[email protected]‘,‘[email protected]‘,‘主题‘,‘hello world!‘) if ret: print(‘发送成功!‘) else: print(‘发送失败!‘)
以上是关于python之路14发送邮件实例的主要内容,如果未能解决你的问题,请参考以下文章