该示例从excel表中读取客户名单,并群发邮件,判断性别修改称呼.添加附件,
import openpyxl, time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
usernames = {}
wb = openpyxl.load_workbook('邮件列表.xlsx')
ws = wb.active
for i in range(2, ws.max_row + 1):
usernames.setdefault(ws['A' + str(i)].value, {})
usernames[ws['A' + str(i)].value].setdefault('电子邮件', ws['D' + str(i)].value)
if str(ws['B' + str(i)].value) == '男':
usernames[ws['A' + str(i)].value].setdefault('称谓', '先生')
else:
usernames[ws['A' + str(i)].value].setdefault('称谓', '女士')
username = '[email protected]'
passwrod = 'mypassword'
frommail = '[email protected]'
subject = '*****公司的邀请函'
server = smtplib.SMTP(host='smtp.163.com', port=25)
server.login(user=username, password=passwrod)
def body(name, male):
texts = '''
尊敬的{}{}:
邀请您参加本公司于3月1日于****举办的公司展销会!'''.format(name, male)
return texts
for key in usernames.keys():
msg = MIMEMultipart()
msg['From'] = frommail
if usernames[key]['电子邮件'] is None or str(usernames[key]['电子邮件']).find('@') == -1:
print('{}没有填写电子邮件地址!或邮箱地址不正确!')
continue
print(key)
msg['To'] = usernames[key]['电子邮件']
msg['Subject'] = '***公司给' + key + '的邀请函'
msg.attach(MIMEText(body(key, usernames[key]['称谓'])))
with open('亲爱的.txt', mode='rb') as f:
attfile = f.read()
att = MIMEApplication(attfile)
att.add_header('Content-Disposition', 'attachment', filename="亲爱的.txt")
msg.attach(att)
try:
server.sendmail(frommail, usernames[key]['电子邮件'], msg.as_string())
except smtplib.SMTPSenderRefused:
print('fail')
time.sleep(10)
except smtplib.SMTPDataError:
print('fail2')
time.sleep(10)
server.close()
新手日志,高手勿喷.