1 Star 0 Fork 0

dragon/python5

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mail_send_list.py 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
碎ping子 提交于 2015-05-04 15:54 . 邮件发送(列表)
# 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 mail_config import to_email_name
from mail_config import to_email
from email.header import Header
from email.utils import parseaddr, formataddr
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))
# 实际收件人地址(列表格式,可以群发):
# 邮件主题
subject = u'来自邮件列表的问候……'
# 邮件内容(默认html格式)
email_content = '''<html>
<body>
<h1>Hello你好</h1>
<p>send by <a href="http://www.python.org">Python</a>...</p>
</body>
</html>'''
def send_mail(to_email_list, email_format='html'):
"""
发送邮件
适用场景:邮件列表,EDM邮件营销,群发(特点:邮件内容统一)
:return:
"""
if email_format == 'text':
msg = MIMEText(email_content, 'plain', 'utf-8')
else:
msg = MIMEText(email_content, 'html', 'utf-8')
msg['From'] = _format_email(u'%s <%s>' % (from_email_name, from_email))
msg['To'] = _format_email(u'%s <%s>' % (to_email_name, to_email))
msg['Subject'] = Header(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_list, msg.as_string())
server.quit()
if __name__ == '__main__':
send_mail(email_list)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/dragon-teng140806/python5.git
[email protected]:dragon-teng140806/python5.git
dragon-teng140806
python5
python5
master

搜索帮助