资源简介
基于SMTP的自动发邮件的设计实现(Python版),很小的程序,适合初学者。
代码片段和文件信息
# -*- coding: utf-8 -*-
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr formataddr
import smtplib
from email.mime.base import MIMEbase
from email.mime.multipart import MIMEMultipart
def _format_addr(s):
name addr = parseaddr(s) #解析字符串中的email地址
return formataddr((Header(name ‘utf-8‘).encode() addr))
from_name=‘发件人‘
from_addr = ‘rg...4@163.com‘
password = ‘r...4‘
to_name=‘anyone‘
to_addr = ‘10...@qq.com‘
# to_addr = ‘ren...@gmail.com‘
smtp_server = ‘smtp.163.com‘
smtp_port = 25
title=‘网络‘
# content=u‘‘‘%s,这是...发的邮件,你看到了吗?‘‘‘
content=u‘‘‘Dear %slang=EN-US>
Hello,
‘‘‘;
#
# msg = MIMEText((content % (to_name)) ‘plain‘ ‘utf-8‘)
# msg = MIMEText((content%(to_name)) ‘html‘ ‘utf-8‘)
msg = MIMEMultipart()
# 邮件正文是MIMEText:
msg.attach(MIMEText((content%(to_name)) ‘html‘ ‘utf-8‘));
# 添加附件就是加上一个MIMEbase,从本地读取一个图片:
with open(‘./test.pdf‘ ‘rb‘) as f:
# 设置附件的MIME和文件名,这里是png类型:
# mime = MIMEbase(‘image‘ ‘png‘ filename=‘test.png‘)
mime = MIMEbase(‘application‘ ‘pdf‘ filename=‘test.pdf‘)
# 加上必要的头信息:
mime.add_header(‘Content-Disposition‘ ‘attachment‘ filename=‘test.pdf‘)
mime.add_header(‘Content-ID‘ ‘<0>‘)
mime.add_header(‘X-Attachment-Id‘ ‘0‘)
# 把附件的内容读进来:
mime.set_payload(f.read())
# 用base64编码:
encoders.encode_base64(mime)
# 添加到MIMEMultipart:
msg.attach(mime)
msg[‘From‘] = _format_addr(from_name+‘ <%s>‘ % from_addr)
msg[‘To‘] = _format_addr(to_name+‘ <%s>‘ % to_addr)
msg[‘Subject‘] = Header(title ‘utf-8‘).encode()
server = smtplib.SMTP(smtp_server smtp_port)
server.set_debuglevel(1)
server.login(from_addr password)
server.sendmail(from_addr [to_addr] msg.as_string())
server.quit()
# def sendmsg(recipientnamerecipientaddr):
# #msg = MIMEText(‘hello send by Python...‘ ‘plain‘ ‘utf-8‘)
# msg = MIMEText((content % (recipientname)) ‘html‘ ‘utf-8‘)
# msg[‘From‘] = _format_addr(u‘%s <%s>‘ % (from_namefrom_addr))
# msg[‘To‘] = _format_addr(u‘%s <%s>‘ % (recipientnamerecipientaddr))
# msg[‘Subject‘] = Header(title ‘utf-8‘).encode()
#
# server = smtplib.SMTP(smtp_server 25)
# #server.set_debuglevel(1)
# server.login(from_addr password)
# server.sendmail(from_addr [recipientaddr] msg.as_string())
# server.quit()
- 上一篇:Python高级编程和异步IO并发编程
- 下一篇:python登陆
相关资源
- python登陆
- Python高级编程和异步IO并发编程
- 廖雪峰最新Python3教程
- Python搜索爬虫抓取超高清视频
- 微博图片视频小爬虫
- Python 简单的登录界面源码
- python编程小游戏汉诺塔hanoi
- 小甲鱼零基础入门学习Python+全套源码
- pygraphviz python3.4 轮子
- 用Python编写潮流计算极坐标
- Python-图像操作
- python建模
- windows将Python添加到注册表
- 位图转g代码轮廓加工源码
- 基于神经网络控制一阶倒立摆小车
- 基于Python爬虫的股票信息爬取保存到
- 基于NMF和PCA实现人脸图像特征提取与
- 基于PCA实现鸢尾花数据集降维
- 基于KNN实现“手写识别”
- 基于sklearn模块的神经网络实现“手写
- 2018最新BAT-python面试题内附答案
- BM3D去噪python代码
- insert_test_data.py
- 栅格投影转换.py
- IDW插值批处理
- 神经网络模型python模板
- python3程序设计习题答案第3版
- python核心基础.txt
- 尚硅谷2018年Python视频
- 基于用户最近邻模型的协同过滤算法
评论
共有 条评论