影刀RPA进阶教程:自动发送邮件与附件投递
自动化流程跑完后,把结果自动发邮件给相关人——这是最常见的"流程收尾"需求之一。
影刀没有原生的发邮件指令,但用 Python 的smtplib可以轻松实现。这篇文章覆盖 QQ 邮箱、163 邮箱、企业邮箱三种场景。
一、准备工作:获取邮箱授权码
不要用邮箱登录密码,要用"授权码"(也叫 SMTP 独立密码)。
店群矩阵自动化突破运营极限!
| 邮箱 | 获取授权码的路径 |
|---|---|
| QQ邮箱 | 设置 → 账户 → POP3/SMTP服务 → 开启 → 生成授权码 |
| 163邮箱 | 设置 → POP3/SMTP/IMAP → 开启 → 设置授权码 |
| 企业微信邮箱 | 设置 → 客户端专用密码 → 生成 |
二、基础版:发送纯文本邮件
Python代码:importsmtplibfromemail.mime.textimportMIMETextfromemail.headerimportHeader# === 邮箱配置 ===smtp_server="smtp.qq.com"# QQ邮箱smtp_port=465# SSL 端口sender="xxx@qq.com"# 发件人password="你的授权码"# 不是登录密码!receivers=["colleague@qq.com","boss@163.com"]# 收件人列表# === 邮件内容 ===subject=f"每日数据报表 -{变量_今天日期}"body=f""" 各位好, 今日数据采集已完成,概况如下: - 采集平台:淘宝 - 关键词数量:{变量_关键词总数}- 商品总数:{变量_采集总数}- 采集时间:{变量_开始时间}~{变量_结束时间}-  -  结果文件见附件。 此邮件由自动化流程自动发送,无需回复。 """# === 构建邮件 ===msg=MIMEText(body,"plain","utf-8")msg["From"]=Header(f"数据机器人 <{sender}>")msg["To"]=Header(",".join(receivers))msg["Subject"]=Header(subject,"utf-8")# === 发送 ===try:server=smtplib.SMTP_SSL(smtp_server,smtp_port)server.login(sender,password)server.sendmail(sender,receivers,msg.as_string())server.quit()print("邮件发送成功")exceptExceptionase:print(f"邮件发送失败:{e}")raisee三、进阶版:带附件的邮件
实际场景里,发了报表邮件的下一秒就要问:“附件呢?”
Python代码:importsmtplibfromemail.mime.multipartimportMIMEMultipartfromemail.mime.textimportMIMETextfromemail.mime.baseimportMIMEBasefromemailimportencodersfromemail.headerimportHeaderimportos# === 邮箱配置 ===smtp_server="smtp.qq.com"smtp_port=465sender="xxx@qq.com"password="你的授权码"receivers=["colleague@qq.com"]# === 构建带附件的邮件 ===msg=MIMEMultipart()msg["From"]=Header(f"数据机器人 <{sender}>")msg["To"]=Header(",".join(receivers))msg["Subject"]=Header(f"每日报表 -{变量_今天日期}","utf-8")# 正文body="详见附件。此邮件由自动化流程自动发送。"msg.attach(MIMEText(body,"plain","utf-8"))# 附件attachment_path=f"D:\\报表\\日报_{变量_今天日期}.xlsx"ifos.path.exists(attachment_path):withopen(attachment_path,"rb")asf:attachment=MIMEBase("application","octet-stream")attachment.set_payload(f.read())encoders.encode_base64(attachment)attachment.add_header("Content-Disposition",f'attachment; filename="{os.path.basename(attachment_path)}"')msg.attach(attachment)print(f"已添加附件:{attachment_path}")else:print(f"附件不存在:{attachment_path}")# 发送try:server=smtplib.SMTP_SSL(smtp_server,smtp_port)server.login(sender,password)server.sendmail(sender,receivers,msg.as_string())server.quit()print("邮件+附件发送成功")exceptExceptionase:print(f"发送失败:{e}")四、HTML 格式邮件(更美观的报表)
Python代码:importsmtplibfromemail.mime.textimportMIMETextfromemail.headerimportHeader# HTML 正文html_body=f""" <html> <body> <h2>📊 每日销售数据看板</h2> <p>日期:{变量_今天日期}</p> <table border="1" cellpadding="8" cellspacing="0" style="border-collapse:collapse"> <tr style="background:#4A90D9;color:white"> <th>指标</th><th>数值</th><th>环比</th> </tr> <tr> <td>销售额</td> <td>¥{变量_销售额:,.0f}</td> <td style="color:{'green'if变量_销售环比>0else'red'}">{变量_销售环比:+.1f}%</td> </tr> <tr> <td>订单量</td> <td>{变量_订单量}单</td> <td style="color:{'green'if变量_订单环比>0else'red'}">{变量_订单环比:+.1f}%</td> </tr> [video(video-wS32QjWt-1781240287751)(type-csdn)(url-https://live.csdn.net/v/embed/524992)(image-https://v-blog.csdnimg.cn/asset/b59aed2f01d4fe8583467562aaf4dcfd/cover/Cover0.jpg)(title-temu店群自动化报活动案例)] <tr> <td>访客数</td> <td>{变量_访客数}</td> <td>-</td> </tr> </table> <p style="color:#999;font-size:12px">此邮件由自动化流程自动发送</p> </body> </html> """msg=MIMEText(html_body,"html","utf-8")msg["From"]=Header(f"数据看板 <{sender}>")msg["To"]=Header(",".join(receivers))msg["Subject"]=Header(f"📊 每日看板 -{变量_今天日期}","utf-8")server=smtplib.SMTP_SSL(smtp_server,smtp_port)server.login(sender,password)server.sendmail(sender,receivers,msg.as_string())server.quit()print("HTML 邮件发送成功")五、封装为可复用的子流程
# 子流程:F_发送邮件# 输入参数:收件人列表、主题、正文、附件路径(可选)# 输出参数:发送结果(True/False)Python代码:importsmtplibfromemail.mime.multipartimportMIMEMultipartfromemail.mime.textimportMIMETextfromemail.mime.baseimportMIMEBasefromemailimportencodersfromemail.headerimportHeaderimportos sender="xxx@qq.com"password="授权码"defsend_mail(to_list,subject,body,attachment_path=None):msg=MIMEMultipart()msg["From"]=Header(sender)msg["To"]=Header(",".join(to_list))msg["Subject"]=Header(subject,"utf-8")msg.attach(MIMEText(body,"plain","utf-8"))ifattachment_pathandos.path.exists(attachment_path):withopen(attachment_path,"rb")asf:part=MIMEBase("application","octet-stream")part.set_payload(f.read())encoders.encode_base64(part)part.add_header("Content-Disposition",f'attachment; filename="{os.path.basename(attachment_path)}"')msg.attach(part)try:server=smtplib.SMTP_SSL("smtp.qq.com",465)server.login(sender,password)server.sendmail(sender,to_list,msg.as_string())server.quit()returnTrueexceptExceptionase:print(f"发送失败:{e}")returnFalse# 调用result=send_mail(to_list=变量_收件人列表,subject=变量_邮件主题,body=变量_邮件正文,attachment_path=变量_附件路径)变量_发送结果=result六、各邮箱 SMTP 配置速查
| 邮箱 | SMTP 服务器 | 端口 |
|---|---|---|
| QQ邮箱 | smtp.qq.com | 465 (SSL) |
| 163邮箱 | smtp.163.com | 465 (SSL) |
| 126邮箱 | smtp.126.com | 465 (SSL) |
| Gmail | smtp.gmail.com | 587 (TLS) |
| Outlook | smtp.office365.com | 587 (TLS) |
| 阿里企业邮箱 | smtp.mxhichina.com | 465 (SSL) |
| 腾讯企业邮箱 | smtp.exmail.qq.com | 465 (SSL) |
作者:林焱
本文为《影刀RPA学习手册》系列文章之一,内容源于实操经验的整理与分享。