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的请求操 ...
随机推荐
- 字符串-回文-Manacher算法
http://blog.csdn.net/zzkksunboy/article/details/72600679 https://segmentfault.com/a/1190000008484167 ...
- CCCC L2-001 紧急救援 floyd改的dijkstra模板 (记录路径) L3 天梯地图
https://www.patest.cn/contests/gplt/L2-001 题解:求最短路的条数,并输出点的权值最大的路径,用priority_queue会wa两个点,原因不明. 于是又学了 ...
- android adt自带eclipse无法设置ndk路径
android sdk官网下载r23版本的adt时自带的eclipse没有设置ndk路径的地方,通过Install New Software 发现无法更新,那么如何解决这个问题呢? 软件百度云 ...
- Copying and Cloning Objects
PHP Advanced and Object-OrientedProgrammingVisual Quickpro GuideLarry Ullman class someClass { publi ...
- iOS-数据缓存(转载)
一.关于同一个URL的多次请求 有时候,对同一个URL请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的. 上面的情况会造成以下问题 (1)用户流量的浪费 ...
- In MySQL, a zero number equals any string
最近在做项目的过程中发现了一个问题 数据库表 test 有个字段是 target_id int(11),这个字段可能为零 使用如下查询 select * from test where targe ...
- 新建的web工程找不到javax.servlet.http.httpservlet
1.在出现此错误的项目上右键-->Build Path --> Configure Build Path 2.点击右边的 ADD Library 3.选中“Server Runtime”, ...
- Service 的 onStartCommand(Intent, int, int) 返回值
(1)START_NOT_STICKY If the system kills the service after onStartCommand() returns, do not recreate ...
- 关于HttpServletRequest报红叉的解决办法
今天导入项目的时候,发现报错了,如题.然后找到了解决办法 解决方法:鼠标右击项目工程——>Build Path——>点击comfigure Build Path进入----->选择j ...
- 分布式网格缓存Coherence简介
Coherence企业级缓存(一) 特点 摘要:Oracle Coherence是一个企业级的分布式集群缓存框架.具有自管理,自恢复,高可用性,高扩展性等优良特点,在电信BOSS等项目中有很大的应用价 ...