新版发送邮件的脚本:批量发邮件 6.0
CN
字符集统计:
“gbk”、“GBK”、“gb2312”、“GB2312”、“iso-8859-1”、“gib5”、“GIB5”、“us-ascii”、“utf-8”、“UTF-8”、“ascii”、“ASCII”
gbk、GBK、gb2312、GB2312、iso-8859-1、gib5、GIB5、us-ascii、utf-8、UTF-8、ascii、ASCII
行注意事项:
1、读取文件源码操作:根据实况而定,一般情况下eml文件定义统一,代码在运行时,若出现文件乱码,或者报错,请查看该eml文件的from、to、正文、附件、和源码(是否有:Content-Type: text/plain; charset=utf-8)
2、判断附件操作:附件可以根据自己的需求是否读取分析,一般情况下不建议读取附件,防止发送结果有影响,仅读取附件名称即可。
3、时间操作:时间获取分别有两种方式,可以根据自己的喜好而定。
4、所用的框架库,确保自己安装的pycharm装有第三方支持库:email、locale、smtplib、os、sys、time、shutil、importli。
5、运行脚本的 rootdir 指定发送样本库,确保样本库中全是eml文件,不能有文件夹、txt文件、ffs_db文件等。
# -*- encoding=utf-8 -*-
import email
import locale
import smtplib
import os
import sys
import time
import shutil
from email.mime.text import MIMEText
from email.header import Header
import importlib
x = 0 # 可以发送的邮件数量
z = 0 # 未能发送的邮件数量
c = 0 # 解析到乱码内容的邮件
c_txt = []
d = 0 # 没有字符集或者是不能解析字符集的邮件
d_txt = []
e = 0 # 主要针对的是严重的邮件解析异常
e_txt = []
g = 0 # 针对不能识别到编码格式的邮件
g_txt = []
error_eml = 0 # 针对最后的字符集,选择跳出
error_eml_txt = []
attError = [] # 针对附件文件提取时格式的问题或是提取失败做捕获
'''时间操作'''
def set_timed():
# timed = time.asctime(time.localtime((time.time())))
# print(timed)
locale.setlocale(locale.LC_ALL,'en')
locale.setlocale(locale.LC_CTYPE,'chinese')
zhou = time.strftime('%w',time.localtime(time.time()))
if zhou == "0":
zhou = "星期日"
elif zhou == "1":
zhou = "星期日"
elif zhou == "2":
zhou = "星期日"
elif zhou == "3":
zhou = "星期日"
elif zhou == "4":
zhou = "星期日"
elif zhou == "5":
zhou = "星期日"
elif zhou == "6":
zhou = "星期日"
else:
zhou = " "
timed = time.strftime('%z %Y年%m月%d日 %H:%M:%S %p ',time.localtime(time.time()))
return timed+zhou
def runPri_files(path):
global x, par
# global y
global z
global c
global c_txt
global d
global d_txt
global e
global e_txt
global g
global g_txt
global error_eml
global error_eml_txt
global attError
lsdir = os.listdir(path)
ok_email = "\\发送成功目录\\"
dirs = [i for i in lsdir if os.path.isdir(os.path.join(path, i))]
if dirs:
for i in dirs:
runPri_files(os.path.join(path, i))
files = [i for i in lsdir if os.path.isfile(os.path.join(path, i))]
for f in files:
print("\033[32m start \t\t\t >> *************************** <<\033[0m")
eml_file = os.path.join(path, f) # 文件的所在目录
print(x, "\t文件目录", f, " ", eml_file)
eml_name = eml_file.split("\\")[::-1][0] # 文件名
print("文件名: ", eml_name)
new_dir = ok_email + str(eml_name) # 发送成功的文件目录
fp = open(eml_file, "r")
try:
msg = email.message_from_file(fp)
except UnicodeDecodeError:
print("\033[31m \n codec can't decode byte 0xf3 in position 16: ordinal not in range(128)\n邮件中包含乱码,跳出继续》》》》》》》》》》》》\033[0m")
c = c + 1
c_txt.append(eml_name)
continue
subject = msg.get("Subject") # subject
print("subject: ", subject)
content_type = msg.get("Content-Type") # content_type
print("content_type: ", content_type)
charset_all = str(msg.get_charsets());print("charset_all: ", charset_all)
charset_1 = str(msg.get_charsets()[0]);print("charset_1: ", charset_1)
''' 字符集判断(已作废)
try:
charset = content_type.rsplit("=", 1)[::-1][0]
except AttributeError:
print("\033[31m AttributeError: 'NoneType' object has no attribute 'rsplit' \033