自我介绍
本人计算机专业毕业,当过网管,做过网工,现在从事运维。在工作中发现好多技术需要学习。需要不停的充实自己,努力提高自己。运维也需要开发能力。努力中。。。。。。。。。。。。
技术分享:
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装
正常发邮件能抄送,多用户
class SMTP(builtins.object)
SMTP(host='', port=0, local_hostname=None, timeout=, source_address=None)
Python代码
#coding:utf8
#加载smtplib模块
import smtplib
from email.mime.text import MIMEText
from email.header import Header
#定义连接邮箱的服务器smtp及端口、用户名和密码
server_host = 'smtp.xxxx.xxx'
server_port = '25'
user_auth = '[email protected]'
user_pass = 'xxxxxxxx'
#定义收件人、抄送人list
to_user = [xxxx@xxxx.xxxx','xxxx@xxxx.xxxx']
to_ccuser = ['xxxx@xxxx.xxxx']
#定义发送邮件的msg内容、标题、发件人、收件人、抄送人
mail_subject = '测试邮件'
mail_context = '内容'
# msg ="""From:%s <%s>
# To:%s <%s>
# Subject: %s #如果后不加一行空行,没有内容输出到邮件
#
# %s
# """%(user_auth,user_auth,to_user,to_user,mail_subject,mail_context)
msg = MIMEText(mail_context,'plain','utf-8')
msg['Subject'] = Header(mail_subject,'utf-8')
#msg['From'] = Header(user_auth,'utf-8')
msg['From'] = user_auth
msg['To'] = ','.join(to_user)
msg['cc'] = ','.join(to_ccuser)
#以迭代器形式添加抄送人到发送列表
to_user.extend(to_ccuser)
#暗抄送
#receive.extend(bccto_list)
#实例化smtp对象
smtpobj = smtplib.SMTP()
try:
smtpobj.connect(server_host, server_port)
smtpobj.login(user_auth, user_pass)
#smtpobj.sendmail(user_auth, msg['To'].split(','), msg.as_string())
smtpobj.sendmail(user_auth, to_user, msg.as_string())
print("邮件发送成功")
smtpobj.quit()
except smtplib.SMTPException:
print('Error:无法发送邮件')
正常发邮件能抄送、多用户、能发附件
Python 代码
#coding:utf8
#加载smtplib模块
import smtplib
#正常发邮件
from email.mime.text import MIMEText
from email.header import Header
#发带附件
from email.mime.multipart import MIMEMultipart
#定义连接邮箱的服务器smtp及端口、用户名和密码
server_host = 'smtp.xxxx.xxx'
server_port = '25'
user_auth = '[email protected]'
user_pass = 'xxxxxxxxxxxx'
#定义收件人、抄送人list
to_user = ['xxx@xxx.xxx','[email protected]']
to_ccuser = ['xxxx@xxxx.xxxx']
#定义发送邮件的msg内容、标题、发件人、收件人、抄送人
mail_subject = '测试邮件'
mail_context = '内容'
# msg ="""From:%s <%s>
# To:%s <%s>
# Subject: %s #如果后不加一行空行,没有内容输出到邮件
#
# %s
# """%(user_auth,user_auth,to_user,to_user,mail_subject,mail_context)
#创建一个带附件的实例
msg = MIMEMultipart()
#msg = MIMEText(mail_context,'plain','utf-8')
msg['Subject'] = Header(mail_subject,'utf-8')
#msg['From'] = Header(user_auth,'utf-8')
msg['From'] = user_auth
msg['To'] = ','.join(to_user)
msg['cc'] = ','.join(to_ccuser)
#以迭代器形式添加抄送人到发送列表
to_user.extend(to_ccuser)
#暗抄送
#receive.extend(bccto_list)
#邮件正文内容
msg.attach(MIMEText(mail_context,'plain','utf-8'))
#构造附件1,传送当前目录下的 test.txt 文件
att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(att1)
#实例化smtp对象
smtpobj = smtplib.SMTP()
try:
smtpobj.connect(server_host, server_port)
smtpobj.login(user_auth, user_pass)
#smtpobj.sendmail(user_auth, msg['To'].split(','), msg.as_string())
smtpobj.sendmail(user_auth, to_user, msg.as_string())
print("邮件发送成功")
smtpobj.quit()
except smtplib.SMTPException:
print('Error:无法发送邮件')
总结:
自己学习测试!