简介
此项目是通过读取excel文件内容(包含公司名称,公司类型,邮箱-每个公司对应多个邮箱)自动批量发送邮件到目标公司。亲测可用。
完整代码
import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from email.header import Header
import sys
from importlib import reload
import xlrd
encoding = 'utf-8'
def main(mail_class):
# mail_class='139' 使用139邮箱发送
# mail_class='ha' 使用河南移动省公司内部邮箱发送
# 读取客户信息文件excel
root_dir = 'D:\pycharm\pycharm_chengxu\python自动发送邮件项目'
file_path = root_dir + "\\youxiang.xls"
file_list = []
work_book = xlrd.open_workbook(file_path)
sheet_data = work_book.sheet_by_name('Sheet1')
print('now is process :', sheet_data.name)
Nrows = sheet_data.nrows
for i in range(1, Nrows):
file_list.append(sheet_data.row_values(i))
if mail_class=='ha':
# 发送邮件的相关信息,公司内部邮箱
smtpHost = 'smtp.ha.chinamobile.com' # 内部邮箱的smtp服务器地址,不同的省份地址不同,一般是自己的邮件后缀,如河南省公司的邮件后缀是ha.chinamobile.com。
smtpPort = '25'
sslPort = '465'
username = '********@ha.chinamobile.com'
password = '*****************' #这里的密码需要从网页版邮箱页面获取授权码,授权码就是密码
fromMail = '中国移动通信集团有限公司 <********@ha.chinamobile.com>' # 中国移动通信集团有限公司是发送者名字,可以随意定义。
if mail_class == '139':
# 发送邮件的相关信息,139邮箱
smtpHost = 'smtp.139.com'
smtpPort = '25'
sslPort = '465'
username = '**********@139.com' # 自己的用户名
password = '**********' # 也是从网页版139邮箱获取授权码。
fromMail = '中国移动通信集团有限公司 <**********@139.com>'
# 设置默认字符集为UTF8 不然有些时候转码会出问题
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
# 连接登录邮箱
try:
# #普通方式登录邮箱,使用内网
# print('创建smtp对象')
# smtp = smtplib.SMTP()
# smtp.connect(smtpHost)
# smtp.login(username, password)
# print('登录服务器成功')
# 纯粹的ssl加密方式,通信过程加密,邮件数据安全--登陆邮箱。此种方式可以使用外网登陆发送
smtp = smtplib.SMTP_SSL(smtpHost,sslPort)
smtp.ehlo()
smtp.login(username, password)
print('登录服务器成功')
except Exception as e:
print(e)
# 循环发送邮件
for i in range(len(file_list)):
msg = MIMEMultipart()
# 构造邮件标题和内容,
subject = "中国移动通信集团有限公司高精度定位服务"
# 获取最新的email路径,如果存在说明有报错,构造附件,发送email路径下的excel文件
# 构造邮件附件 并封装
att1 = MIMEApplication(
open('D:\pycharm\pycharm_chengxu\python自动发送邮件项目\无人机行业解决方案.pdf', 'rb').read())
att1['Content-Type'] = 'application/octet-stream'
# 这里的filename可以任意写,写什么邮件就显示什么名字
att1.add_header('Content-Disposition', 'attachment', filename="无人机行业解决方案.pdf")
msg.attach(att1)
# 封装邮件主题和来源邮箱和内容
msg['Subject'] = Header(subject, encoding)
msg['From'] = Header(fromMail)
area_company =str(file_list[i][1])
company = str(file_list[i][2])
email_list = getemail(file_list, i)
if len(email_list)==0:
print(str(i+1) +":"+company+'无邮箱')
continue
msg['To'] = Header(';'.join(email_list), encoding)
# 邮件内容,用html来写邮件内容,展现方式比较丰富
content = """
HTML网页代码
"""
mail_content = MIMEText(content.encode(encoding), 'html', encoding)
msg.attach(mail_content)
try:
# 发送邮件
smtp.sendmail(fromMail, email_list, msg.as_string())
print(str(i+1) + ": " + str(company) + "->" + str(len(email_list)) + "封信" + "->" + str(email_list))
except Exception as e:
print(str(company) + ':发送失败,异常原因:'+str(e))
# for email in email_list:
# toMail = email
# msg['To'] = toMail
# try:
# # 发送邮件
# smtp.sendmail(fromMail, toMail, msg.as_string())
# except Exception as e:
# print(e)
print('发送完毕')
smtp.close()
def getemail(file_list,i):
email_list=[]
if file_list[i][12] == '-' and file_list[i][13] != '-':
email_list=file_list[i][13].split(';')
if file_list[i][12] != '-' and file_list[i][13] == '-':
email_list = file_list[i][12].split(';')
if file_list[i][12] != '-' and file_list[i][13] != '-':
email_liststr=file_list[i][12]+';'+file_list[i][13]
email_list=email_liststr.split(';')
if file_list[i][12] == '-' and file_list[i][13] == '-':
email_list=[]
return email_list
if __name__ == '__main__':
main('ha')
注意事项
下面是我运行过程中遇到的各种问题。
1,若使用公网发送邮件,端口号是25;移动公司内部邮箱每天发送邮件数量有限,一般是500封。使用内网发送邮件,端口号是465,每天发送的上限是5000封。
2,139邮箱每天发送的上限是250封。若达到上限,可利用多个邮箱,进行发送。
3,一定要提前在网页版邮箱找到授权码,并打开smtp服务,打开方式可自行在百度进行搜索,比较简单。
4,大批量发送邮件过程中,有些邮件被退回,原因有:①目标邮箱不正确。②发送DATA命令失败。
5,后续还会对此版本进行不断升级改造,若有意向可关注。