02: python3使用email和smtplib库发送邮件
1.1 发送qq邮箱
注:python代理登录qq邮箱发邮件,是需要更改自己qq邮箱设置的。在这里大家需要做两件事情:邮箱开启SMTP功能 、获得授权码 教程链接
1、给单个人发邮件 参考
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL #qq邮箱smtp服务器
host_server = 'smtp.qq.com'
#sender_qq为发件人的qq号码
sender_qq = '153236xxx'
#pwd为qq邮箱的授权码
pwd = 'drjzidfyftatheca'
#发件人的邮箱
sender_qq_mail = '153236xxx@qq.com'
#收件人邮箱
receiver = '153236xxx@qq.com'
#邮件的正文内容
mail_content = '你好,我是来自知乎的[tom肖] ,现在在进行一项用python登录qq邮箱发邮件的测试'
#邮件标题
mail_title = '您好,这是测试邮件' #ssl登录
smtp = SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd) msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit()
给单个人发邮件
2、群发
from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL #sender_qq为发件人的qq号码
sender_qq = '1532363xxx'
#pwd为qq邮箱的授权码
pwd = 'drjzidfyftatheca'
#收件人邮箱receiver
receiver='1532363xxx@qq.com'
#邮件的正文内容
mail_content = '你好,我是来自博客园的[tom肖] ,现在在进行一项用python登录qq邮箱发邮件的测试'
#邮件标题
mail_title = '这是一封测试邮件' def send_mail(sender_qq='',pwd='',receiver='',mail_title='',mail_content=''):
# qq邮箱smtp服务器
host_server = 'smtp.qq.com'
sender_qq_mail = sender_qq+'@qq.com' #ssl登录
smtp = SMTP_SSL(host_server)
#set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式
smtp.set_debuglevel(1)
smtp.ehlo(host_server)
smtp.login(sender_qq, pwd) msg = MIMEText(mail_content, "plain", 'utf-8')
msg["Subject"] = Header(mail_title, 'utf-8')
msg["From"] = sender_qq_mail
msg["To"] = receiver
smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
smtp.quit() for i in range(10):
send_mail(sender_qq=sender_qq,pwd=pwd,receiver=receiver,mail_title=mail_title,mail_content=mail_content)
测试群发邮件
3、邮件格式化
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import datetime def format_email(not_phone_list):
with open('content.html','r') as f:
content = f.read()
not_phone_list = [{'jobid':'','name':'张三','mobile':''},
{'jobid':'','name':'李四','mobile':''},
{'jobid':'','name':'王五','mobile':''}]
tr = '''
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tr>
'''
s = ''
for info in not_phone_list:
s += tr%(info.get('jobid'),info.get('name'),info.get('mobile'))
content = content.replace('ReplaceContent',s)
return content def notify_by_email(not_phone_list):
tos = 'naiqiang.xiao@yiducloud.cn' # 发送的人员
mailapi = 'http://op.intra.yiducloud.cn/phpmail/mail.php'
sender = 'opsheet'
mailtype = 'html' subject = '-'.join(['MIS系统同步通知', '预入职库中无法获取人员名单'])
content = format_email(not_phone_list)
data = {'content': content, 'subject': subject, 'tos': tos, 'mailtype': mailtype, 'sender': sender}
try:
r = requests.get(mailapi, params=data)
return r.status_code
except Exception as e:
print e notify_by_email('')
send_email.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
.table{
border-spacing: 0;
border-collapse: collapse;
}
.table thead tr th,td{
padding: 20px;
padding-left: 50px;
vertical-align: top;
border-top: 1px solid #ddd;
}
td,th{
display: table-cell;
}
</style>
</head>
<body>
<table class="table table-striped">
<thead>
<tr>
<th>工号</th>
<th>姓名</th>
<th>手机号</th>
</tr>
</thead>
<tbody>
ReplaceContent
</tbody>
</table>
</body>
</html>
content.html
02: python3使用email和smtplib库发送邮件的更多相关文章
- python3:利用smtplib库和smtp.qq.com邮件服务器发送邮件
python3:利用smtplib库和smtp.qq.com邮件服务器发送邮件 使用qq的邮件服务器需要注意的两个地方主要是: 1.协议问题 使用465端口 SSL 协议 2.口令问题 出现SMTPA ...
- python使用smtplib和email库发送邮件
国内很多服务器提供商都默认禁止了smtp默认的25端口服务,而启用465端口发送邮件 在smtplib库中直接调用SMTP_SSL就是默认使用465端口 示例代码如下: def send_eamil( ...
- python:利用smtplib模块发送邮件详解
自动化测试中,测试报告一般都需要发送给相关的人员,比较有效的一个方法是每次执行完测试用例后,将测试报告(HTML.截图.附件)通过邮件方式发送. 首先我们要做: 进入163邮箱,点击设置中的pop3/ ...
- Python3+HTMLTestRunner+SMTP生成测试报告后发送邮件
在前一篇https://www.cnblogs.com/zhengyihan1216/p/11549820.html 中记录了如何生成html格式的报告, 这篇记录下怎么将测试报告通过邮件发出 1.对 ...
- python之使用smtplib模块发送邮件
# 使用smtplib模块发送邮件 import smtplib from email.mime.text import MIMEText from email.header import Heade ...
- python之smtplib模块 发送邮件
# -*- coding: utf-8 -*- #python 27 #xiaodeng #smtplib模块 发送邮件 import smtplib from email.mime.text imp ...
- 使用python的email、smtplib、poplib模块收发邮件
使用python的email.smtplib.poplib模块收发邮件 一封电子邮件的旅程是: MUA:Mail User Agent——邮件用户代理.(即类似Outlook的电子邮件软件) MTA: ...
- python的email、smtplib、poplib模块收发邮件
一封电子邮件的旅程是: MUA:Mail User Agent--邮件用户代理.(即类似Outlook的电子邮件软件) MTA:Mail Transfer Agent--邮件传输代理,就是那些Emai ...
- Python3 网络爬虫(请求库的安装)
Python3 网络爬虫(请求库的安装) 爬虫可以简单分为几步:抓取页面,分析页面和存储数据 在页面爬取的过程中我们需要模拟浏览器向服务器发送请求,所以需要用到一些python库来实现HTTP的请求操 ...
随机推荐
- ZOJ 3777 - Problem Arrangement - [状压DP][第11届浙江省赛B题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 Time Limit: 2 Seconds Me ...
- cordova插件file使用时遇到的一个平台相关的问题
使用cordova-plugin-file可以帮助我们方便的操作app中的图片等文件,分享一下我在用该插件从图库读取图片时遇到的一个平台相关的小问题. 使用场景,我会在APP中新增一张图片,会有一个可 ...
- yum provides "*/nmcli" and apt-get
一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...
- Iwconfig/aircrack-ng
BT5 aircrack-ng破解无线密码(wpa/wep) - 星明月稀 - 博客频道 - CSDN.NET BT5 aircrack-ng破解无线密码(wpa/wep) - ...
- 基于bind搭建DNS主从
使用bind的主从复制功能可以实现的功能:提供冗余,避免单点故障:均衡负载查询需求,从而提高系统可用性. 一.安装 #bind-chroot 负责DNS安全作用,将bind进程严格限制在特定的目录中 ...
- flask之表单验证 2018.12.23
#flask的消息闪现依赖于flash库,用户发送的请求方式存储在request模块中 #跳转依赖于redirect模块,还可以通过url_for方法来进行方法上的寻址 from flask impo ...
- linux find 命令
Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数 ...
- 【CSS3】CSS3自学
CSS3学习网址:http://www.runoob.com/css3/css3-tutorial.html
- [py]pycharm远程环境添加
pycharm配置settings.jar pycharm远程环境调用.zip xadmin xadmin-django2 pycharm激活 最新2018.2激活---更新2018年8月8日 15: ...
- for与while的特点及其if在什么情况下使用情况
for和while的特点: 什么时候使用循环结构呢? 1:当对某些代码执行很多次时,使用循环结构完成. 2:当对一个条件进行一次判断时,可以使用if语句. 3:当对一个条件进行多次判断时,可以使用wh ...