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的请求操 ...
随机推荐
- opencv学习网站
强烈推荐一个老外的网站,pyimagesearch 网址:https://www.pyimagesearch.com/
- AudioUnit录音并同步播放时遇到的问题
AudioComponentDescription desc; desc.componentType =kAudioUnitType_Output; desc.componentSubType = k ...
- Apple Pay的实现
首先是搜到的大神写的全套知识点:http://www.jianshu.com/p/8d7b86f1d142 http://www.cnblogs.com/dashunzi/archive/2016/0 ...
- Django - 补充目录
Django项目部署 importlib应用 - django contenttypes - django组件 Django - Model操作 Django - 用户认证.用户组.用户权限 Djan ...
- 在django项目中自定义manage命令(转)
add by zhj 是我增加的注释 原文:http://www.cnblogs.com/holbrook/archive/2012/03/09/2387679.html 我们都用过Django的dj ...
- Unity3D中使用Profiler精确定位性能热点的优化技巧
本文由博主(SunboyL)原创,转载请注明出处:http://www.cnblogs.com/xsln/p/BeginProfiler.html 简介 在使用Profiler定位代码的性能热点时,很 ...
- 【Python】if相关知识点
1.9选课中心前两页课程名称打印到固定系统文件夹的某个csv文件中. #-*- coding: UTF-8 -*- #coding==utf-8 #from selenium.webdriver.su ...
- js屏蔽f12键
<script> $(document).keydown(function(e) { if (e.keyCode == 123) {/ ...
- sql server 2005 使用Log Explorer查看和恢复数据
使用Log Explorer查看和恢复数据 Log Explorer 4.1.可用于SQL Server2005的日志查看工具 下载地址: http://download.csdn.net/ ...
- URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/whyorwhnt/article/details/34075603 题意:给出两个串的长度(一样长) ...