1 Star 0 Fork 0

dragon/python5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mail_send_queue.py 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
碎ping子 提交于 2015-05-04 15:58 . 邮件发送(队列)
# coding=utf-8
__author__ = 'zhanghe'
import smtplib
from email.mime.text import MIMEText
from mail_config import from_email_name
from mail_config import from_email
from mail_config import password
from mail_config import smtp_server
from email.header import Header
from email.utils import parseaddr, formataddr
# 邮件内容
email_info = {
'to_email_name': u'测试收件人',
'to_email': u'[email protected]',
'subject': u'这是一封来自python的测试邮件!',
'content': u'<html><body><h1>Hello</h1>' +
u'<p>send by <a href="http://www.python.org">Python</a>...</p>' +
u'</body></html>',
}
def _format_email(s):
"""
格式化邮件地址
:param s:
:return:
"""
name, email = parseaddr(s)
return formataddr((Header(name, 'utf-8').encode(), email.encode('utf-8') if isinstance(email, unicode) else email))
def send_mail(to_email, email_format='html'):
"""
发送邮件
适用场景:邮件队列
:return:
"""
if email_format == 'text':
msg = MIMEText(email_info['content'], 'plain', 'utf-8')
else:
msg = MIMEText(email_info['content'], 'html', 'utf-8')
msg['From'] = _format_email(u'%s <%s>' % (from_email_name, from_email))
msg['To'] = _format_email(u'%s <%s>' % (email_info['to_email_name'], email_info['to_email']))
msg['Subject'] = Header(u'%s' % email_info['subject'], 'utf-8').encode()
server = smtplib.SMTP(smtp_server, 25) # SMTP协议默认端口是25
# server.set_debuglevel(1) # 打印出和SMTP服务器交互的所有信息
server.login(from_email, password)
server.sendmail(from_email, [to_email], msg.as_string())
server.quit()
# TODO:与队列结合起来
if __name__ == '__main__':
send_mail(email_info['to_email'])
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/dragon-teng140806/python5.git
[email protected]:dragon-teng140806/python5.git
dragon-teng140806
python5
python5
master

搜索帮助