1、发送普通邮件
def send_mail(to_mail, to_title, to_content):
ret = True
FROM_MAIL = "****@***.com" # 发件人
TO_MAIL = to_mail # 收件人
SMTP_SERVER = 'mail.****.com.cn' #qq邮箱服务器
SSL_PORT = '465' #加密端口
USER_NAME = FROM_MAIL #qq邮箱用户名
USER_PWD = "**********" #qq邮箱授权码
msg = email.mime.multipart.MIMEMultipart() #实例化email对象
msg['from'] = FROM_MAIL # 对应发件人邮箱昵称、发件人邮箱账号
msg['to'] = TO_MAIL # 对应收件人邮箱昵称、收件人邮箱账号
msg['subject'] = to_title # 邮件的主题
txt = email.mime.text.MIMEText(to_content)
msg.attach(txt)
try:
# 纯粹的ssl加密方式
smtp = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT) #邮件服务器地址和端口
smtp.ehlo() # 用户认证
smtp.login(USER_NAME, USER_PWD) # 括号中对应的是发件人邮箱账号、邮箱密码
smtp.sendmail(FROM_MAIL, TO_MAIL, str(msg)) # 收件人邮箱账号、发送邮件
smtp.quit() # 等同 smtp.close() ,关闭连接
except Exception as e:
ret = False
print(">>>>>>>:" + e)
return ret
执行代码
send_mail( '*****@*****.com.cn', 'test','this is test')
2、发送带附件的
# coding:utf-8
"""
author:zhouxuan
@project--file : listing -->eamil.py
2020/3/31 10:40
@Desc:
"""
import email.mime.multipart
import email.mime.text
from email.mime.text import MIMEText
import smtplib
import os
#接受:收件人,主题,内容
#返回:邮件发送结果
def send_mail(to_mail, to_title, to_content):
ret = True
FROM_MAIL = "****@***.com" # 发件人
TO_MAIL = to_mail # 收件人
SMTP_SERVER = 'mail.****.com.cn' #邮箱服务器
SSL_PORT = '465' #加密端口
USER_NAME = FROM_MAIL #邮箱用户名
USER_PWD = "***********" #邮箱授权码
msg = email.mime.multipart.MIMEMultipart() #实例化email对象
msg['from'] = FROM_MAIL # 对应发件人邮箱昵称、发件人邮箱账号
msg['to'] = TO_MAIL # 对应收件人邮箱昵称、收件人邮箱账号
msg['subject'] = to_title # 邮件的主题
txt = email.mime.text.MIMEText(to_content)
msg.attach(txt)
"fujian"
#
# 构造附件2,传送pic下的错误截图
"原因:open(check_point.dir_filename 如果所有的jira执行都没有问题,就不会触发创建文件的命令,\
导致没有对应的文本可以打开,暂时先try"
file = os.path.join(os.path.dirname(__file__),'logs','aa.txt').replace('\\','/')
# PIC_PATH=["listing_12345_1573610882.png",
# "listing_12345_1573615347.png"]
att1 = MIMEText(open(file, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="{0}"'.format(file)
msg.attach(att1)
try:
# 纯粹的ssl加密方式
smtp = smtplib.SMTP_SSL(SMTP_SERVER, SSL_PORT) #邮件服务器地址和端口
smtp.ehlo() # 用户认证
smtp.login(USER_NAME, USER_PWD) # 括号中对应的是发件人邮箱账号、邮箱密码
smtp.sendmail(FROM_MAIL, TO_MAIL, msg.as_string()) # 收件人邮箱账号、发送邮件
smtp.quit() # 等同 smtp.close() ,关闭连接
except Exception as e:
ret = False
print(">>>>>>>:" + e)
return ret
send_mail( '****@*****.com.cn', 'test','testing')
Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html。具体代码如下:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'from@runoob.com'
receivers = ['429240967@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">这是一个链接</a></p>
"""
message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = Header("菜鸟教程", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException:
print ("Error: 无法发送邮件")