简介:SMTP (Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于源地址到目的地地址传送邮件到规则,由它来控制信件的中转方式。
python的smtplib对smtp协议进行了简单的封装,以便开发者轻松得发送电子邮件。
创建SMTP对象语法:
import smtplib
smtpObj = smtplib.SMTP([host,[,port [,local_hostname]]])
参数说明:
参数 | 描述 |
---|---|
host | SMTP 服务器主机,你可以指定主机的ip地址或者域名如: runoob.com,这个是可选参数 |
port | 如果你提供了 host 参数, 你需要指定 SMTP 服务使用的端口号,一般情况下 SMTP 端口号为25 |
local_host_name | 如果 SMTP 在你的本机上,你只需要指定服务器地址为 localhost 即可 |
发送邮件语法:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
参数说明:
参数 | 描述 |
---|---|
from_addr | 邮件发送者地址。 |
to_addrs | 字符串列表,邮件发送地址。 |
msg | 发送消息。 |
实例
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
from config import setting
import smtplib
import configparser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_mail(file_new):
"""
定义发送邮件
:param file_new:
:return: 成功:打印发送邮箱成功;失败:返回失败信息
"""
f = open(file_new,'rb')
mail_body = f.read()
f.close()
#发送附件
con = configparser.ConfigParser()
con.read(setting.CONFIG_DIR,encoding='utf-8')
# --------- 读取config.ini配置文件 ---------------
HOST = con.get("user","HOST_SERVER")
SENDER = con.get("user","FROM")
RECEIVER = con.get("user","TO")
USER = con.get("user","user")
PWD = con.get("user","password")
SUBJECT = con.get("user","SUBJECT")
att = MIMEText(sendfile,'base64','utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header("Content-Disposition", "attachment", filename=("gbk", "", report))
msg = MIMEMultipart('related')
msg.attach(att)
# 带附件
msgtext = MIMEText(mail_body,'html','utf-8')
msg.attach(msgtext)
msg['Subject'] = SUBJECT
msg['from'] = SENDER
msg['to'] = RECEIVER
try:
server = smtplib.SMTP('localhost')
server.sendmail(SENDER,RECEIVER,msg.as_string())
print("邮件发送成功!")
except Exception as e:
print("失败: " + str(e))
使用第三方STMP服务发送
import os,sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
from config import setting
import smtplib
import configparser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_mail(file_new):
"""
定义发送邮件
:param file_new:
:return: 成功:打印发送邮箱成功;失败:返回失败信息
"""
f = open(file_new,'rb')
mail_body = f.read()
f.close()
#发送附件
con = configparser.ConfigParser()
con.read(setting.CONFIG_DIR,encoding='utf-8')
# --------- 读取config.ini配置文件 ---------------
HOST = con.get("user","HOST_SERVER")
SENDER = con.get("user","FROM")
RECEIVER = con.get("user","TO")
USER = con.get("user","user")
PWD = con.get("user","password")
SUBJECT = con.get("user","SUBJECT")
att = MIMEText(sendfile,'base64','utf-8')
att["Content-Type"] = 'application/octet-stream'
att.add_header("Content-Disposition", "attachment", filename=("gbk", "", report))
msg = MIMEMultipart('related')
msg.attach(att)
# 带附件
msgtext = MIMEText(mail_body,'html','utf-8')
msg.attach(msgtext)
msg['Subject'] = SUBJECT
msg['from'] = SENDER
msg['to'] = RECEIVER
try:
server = smtplib.SMTP_SSL(HOST)
server.connect(HOST,465)
server.login(USER,PWD)
server.sendmail(SENDER,RECEIVER,msg.as_string())
server.quit()
print("邮件发送成功!")
except Exception as e:
print("失败: " + str(e))