import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.application import MIMEApplication from email.header import Header from email.utils import parseaddr, formataddr import mimetypes import os def send_email(smtp_server, username, password, sender, recipients, subject, content, cc, bcc, port=25, sendername=None, attachments=None): def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr)) if not attachments: attachments = [] msg = MIMEMultipart() if sendername: msg['From'] = _format_addr(sendername + ' <%s>' % sender) else: msg['From'] = sender if isinstance(recipients, str): recipients = [recipients] msg['To'] = ",".join(recipients) if cc: if isinstance(cc, str): cc = [cc] cc_list = [addr for addr in cc if addr not in recipients] if cc_list: msg['Cc'] = ",".join(cc_list) recipients += cc_list if bcc: if isinstance(bcc, str): bcc = [bcc] bcc_list = [addr for addr in bcc if addr not in recipients] if bcc_list: msg['Bcc'] = ",".join(bcc_list) recipients += bcc_list msg['Subject'] = Header(subject, 'utf-8').encode() text_part = MIMEText(content, 'html', 'utf-8') msg.attach(text_part) for attachment in attachments: file_path = attachment["path"] if not os.path.isfile(file_path): print("附件文件不存在:{}".format(file_path)) continue try: with open(file_path, "rb") as f: mime_type, encoding = mimetypes.guess_type(file_path) if mime_type is None: mime_type = 'application/octet-stream' part = MIMEApplication(f.read()) part.add_header('Content-Disposition', 'attachment', filename=attachment["filename"]) part.add_header('Content-Type', mime_type) msg.attach(part) except FileNotFoundError as e: print("文件未找到:{}".format(e)) except Exception as e: print("附件读取失败:{}".format(e)) try: if str(port) == "25": server = smtplib.SMTP(smtp_server, port) else: server = smtplib.SMTP_SSL(smtp_server, port) server.login(username, password) server.sendmail(sender, recipients, msg.as_string()) server.quit() print("邮件发送成功!") except Exception as e: print("邮件发送失败:{}".format(e)) smtp_server = "smtp.aliyun.com" username = "abc@aliyun.com" password = "password" sender = "abc@aliyun.com" recipients = "abc@abc.cn" cc = ["abc@126.com","abc@139.com"] bcc = "" subject = "title" content = "content" n = "name" port = 25 attachments = [{"filename":"申请单.xlsx","path":"C:/申请单.xlsx"}, {"filename": "新课标.docx", "path": "D:/新课标.docx"}, {"filename": "笨笨狗.pdf", "path": "D:/books/笨笨狗.pdf"}] send_email(smtp_server, username, password, sender, recipients, subject, content, cc,bcc,port=port, sendername=n, attachments=attachments)
python教程
Python发送Email电子邮件代码
- Python代码
- 邮件发送
-
iqiyi视频解析Python代码
最新吾爱大佬分享的一段iqiyi视频解析Python代码,转载分享给大家参考。代码说明m3u8下载部分:pip install m3u8download-hecoter使用需要nodejs项目链接:https://github.com/hecoter/videoParse/tree/main/iqiyipython代码import requestsimport reimpo...
-
python开发一个桌面僵尸宠物代码
python开发一个桌面行走的僵尸宠物代码,可切换僵尸皮肤,效果如下:python代码截图python代码如下# *_* coding : UTF-8 *_*# author : Leemamas# 开发时间 : 2021/5/28 0:48 import sysfrom PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt...
-
获取免费的https代理Python代码
前言大家用Python爬网页时候,爬快了被封IP,爬慢了,等的着急,这时候就需要https代理来切换IP了。分享一段获取免费的https代理Python代码,可以快速获取网络上免费的https代理。Python代码from multiprocessing.dummy import Lockimport reimport requestsi...
-
基于百度API文字转语音Python示例代码
准备工作1、首先需要去百度智能云注册账号,官网:https://cloud.baidu.com/2、登陆进入百度语音,领取免费资源3、创建应用,记下自己的 APIkey和 Secret Key写入代码对应的位置Python示例代码# coding=utf-8 import sysimport json # 保证兼容python2以及p...
-
微博自定义批量取消关注Python代码
用过微博都知道,微博自带的批量取消关注功能非常不好用,有跟没有一样,还是要一个个点,麻烦的一比。花了半小时写了微博自定义批量取消关注Python代码,用作批量取消关注,如果有部分,不想取消掉,还可以在白名单里设置。import requestsfrom jsonpath import js...
-
QQ邮箱+PHPMailer邮件发送API接口源码
源码介绍QQ邮箱+PHPMailer邮件发送API接口源码,QQ邮箱SMTP服务+PHPMailer框架开发的邮件发送API接口源码。API接口源码截图源码使用方法请求示例:请求格式:GET请求链接:https:/www.wdzzz.com/email.h...
-
苹果CMSV10开启邮件发送功能
苹果cmsv10在用户注册过程中需要通过邮件验证用户是否为机器人,我们可以通过配置smtp邮件服务器来完成邮件的发送。苹果cms10内置了smtp邮箱功能组件,我们只需要填写我们的smtp发件服务器信息即可向用户推送邮件。QQ邮箱、126邮箱、163邮箱等常用邮箱...
-
帝国CMS二次开发新订单新反馈信息新投稿发邮件通知管理员
一、设置管理员邮件地址和发邮参数 后台,系统设置,系统参数设置 基本属性标签:管理员邮箱。以后的通知邮件都会发到这一邮箱 FTP/EMAIL标签:如果主机支持mail函数,
-
帝国cms7.5会员投稿邮件通知管理员
帝国cms7.5会员投稿邮件通知管理员修改方法1、打开e/class/qinforfun.php文件2、查找代码:printerror("AddQinfoSuccess",$reurl,1);3、在其前增加:$email=$empire->gettotal("select fromemail as total from {$dbtbpre}enewspublic ");@include(ECMS...
-
帝国cms使用qq邮箱发通知邮件
帝国cms使用qq邮箱的pop3/stmp服务,任何等级的qq账号都可用。1、安装php的openssl的extension,并且在php.ini中开启此extension。(如图1所示)2、开通qq邮箱的POP3/SMTP服务,并且获取重要的“授权码...
-
01Python获取彩云天气实时天气API源码 2周前
-
02基于Django的RustDesk Api&Web Server源码分享 2周前
-
03批量修改照片文件大小Python脚本 2个月前
-
04文本段落自动分隔格式化Python脚本 2个月前
-
05利用ffmpeg提取视频第一帧保存成图片 2个月前
-
01123网盘解析下载python脚本 637热度
-
02Python自动下载歌曲宝音乐和歌词脚本 423热度
-
03Python和彩云自动签到云函数脚本分享 383热度
-
04Python无需认证QQ扫码登录脚本 351热度
-
05基于百度API文字转语音Python示例代码 311热度