yagmail mail support system (a)

The original intention of the project

The hands of a large number of students' parents-mail, had previously been using the 163 mail client will copy the message to the recipient address bar and then paste mass, efficiency is not high, so wondering can design an application that automatically send e-mail function, and the program to share to friends, friends do not let Python development environment can help hair part, from the birth of --yagmail mail support system.

design concept

Parents have a first hand information participants excel sheet, which has as a mailing address, e-mail generates a read address by a program which is to be a mailing list, send a one-time since the server has a threshold value, so to control the number of mail, enter the starting number m m , then the number of the input end n n > m ) n(n>m) , is used to intercept a long n m n-m e-mail address, and a transmission by a mail address of the mail taken down, and to have transmitted the progress bar display process, it is best to print out e-mail address as a transmission error feedback, and counted separately.

The main program

import pandas as pd #导入数据分析模块
import time #导入时间模块
from tqdm import tqdm #进度条
data=pd.read_excel("test.xlsx",index_col=u'序号') #读取数据
email=data[u'邮箱'] #邮箱地址
m=input("请输入起始数码按回车键:")
n=input("请输入结束数码按回车键:")
recipients=tqdm(email[int(m):int(n)]) #待发送的邮件列表recipients
print("start sending……")
import yagmail  #导入发邮箱模块
yag=yagmail.SMTP(user="******",password="******",host='smtp.126.com',encoding='utf-8') #链接邮箱服务器
content=[
           "<h1 style='color:red'>暑假线上答疑招生啦!</h1>" ,#可以是html语言,一级标题
           "暑假是数学打基础提分的关键时期,可能你的孩子已经在其他辅导机构狂补特补,数学知识的大量摄入必然会导致消化不良。",
          "我们三行答疑专门从事数学微信群在线答疑,有一流的师资队伍, 为你的孩子搭建知识架构,解答疑惑,帮助消化,让你家孩子数学永不掉线。",
           "只要你孩子想学习数学,就可以来报名,费用美丽,报名入口“三行科创”微信公众号菜单→在线答疑。",
           yagmail.inline("D:/PythonProgram/sendmail/qrcode.jpg"),  #图片会内嵌到正文
           "前50名免费体验。",
           "D:/PythonProgram/sendmail/poster1.jpeg",  #图片文件以附件形式发送
           ] #邮件正文

i=0 #用来记总数
k=0 #用来记发送成功数
s=0 #用来记发送失败数
failure_send=[] #用来存放发送失败的邮件
for j in recipients: #对邮箱地址列表循环
    i=i+1 #计总数
    try:
        yag.send(to=j,                         #发给谁
                 contents=content,             #邮件正文
                 subject="暑假线上答疑招生啦!",    #主题
                 headers={"From": "曾老师"},   #来自何方
                 )
        k=k+1 #发送成功个数
        time.sleep(0.5) #每发一个挂起0.5秒,防止发太快了
    except:
        print("\n%s is not valid" %j) #如果邮件地址错误打印出错误邮件地址
        failure_send.append(j) #追加发送失败的邮件
        s=s+1 #发送失败个数
yagmail.SMTP.close(yag) #关闭服务器链接        
print("finished!") #1388
print("totally send: %s " %i) #发送邮件总数
print("send sucessfully: %s " %k) #发送成功数
print("send failure: %s " %s) #发送失败数

Program explained

Excel program is eventually to read e-mail address is formed inside the mailing address of a list of recipients to be transmitted, and then circulated to the recipients, using a yagmail one transmission, and to tag the messages sent, the transmission success or failure of the transmission, transmission has failed to collect there is failure_send list inside.

yagmail analysis module

First, yagmail relatively simple, the whole idea clear, the most intelligent to be able to judge the content of the list of contents inside the type, first determine whether it is a local file, if it is a local file, then send the attachment in place, can be a relative path, you can fill in the absolute path, and secondly will determine whether the html language, that is the case, the browser can be rendered in the text. If not, it is treated as ordinary text, on the body of the message.

After words

Best not to add a lot of attachments, would bankrupt the sending process, do not let n m n-m is large enough, whether it is 163 or 126 or qq and other third-party mail servers have spam traffic restrictions and measures should pay more attention, the next step is packaged into an executable Windows programs share a friend.

Published 45 original articles · won praise 12 · views 8692

Guess you like

Origin blog.csdn.net/zengbowengood/article/details/94590040