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...
-
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函数,
-
Python获取彩云天气实时天气API源码
import requests # 用户输入的秘钥和经纬度(以逗号分隔)api_key = "" # 这里输入用户自己的秘钥location = "...
-
基于Django的RustDesk Api&Web Server源码分享
项目介绍一个 python 实现的 Rustdesk API 接口,支持 WebUI 管理。基于Django的RustDes...
-
批量修改照片文件大小Python脚本
一段批量修改照片文件大小Python脚本,输入和输出图片文件夹,图片你的最大和最小标准,批量调整照片文件大小。# ...
-
文本段落自动分隔格式化Python脚本
我们在做网站时,需要处理大量的文本内容,要求文本段落内容按我们的格式分隔排版,手动一个个的修改就非常浪费时...
-
利用ffmpeg提取视频第一帧保存成图片
做影视采集站的找不到影视封面可以试试这个方法,利用ffmpeg提取视频第一帧保存成图片,这样就省去了找封面图的...