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':'001','name':'张三','mobile':'18538753511'}, {'jobid':'002','name':'李四','mobile':'18538753511'}, {'jobid':'003','name':'王五','mobile':'18538753511'}] 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('')
<!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>