想给公司做个网站怎么做,word模板网,wordpress 评论设计,南宫职业教育中心示范校建设网站文章目录 #x1f47d;使用Python实现发送Email电子邮件#x1f3b6;实现原理#x1f3c3;Python实现发送Email电子邮件-基础版#x1f46b;实现源码#x1f646;源码解析 #x1f487;Python实现发送Email电子邮件-完善版#x1f46b;实现源码#x1f646;源码解析使用Python实现发送Email电子邮件实现原理Python实现发送Email电子邮件-基础版实现源码源码解析 Python实现发送Email电子邮件-完善版实现源码源码解析优化 总结 使用Python实现发送Email电子邮件
实现原理 导入必要的模块 导入 smtplib 用于处理 SMTP 功能的模块以及从 email 模块导入构建电子邮件消息所需的各个组件。 定义 send_email 函数 创建一个名为 send_email 的函数该函数接受 SMTP 服务器详细信息、发件人和收件人信息、主题、内容和附件等参数。 格式化发件人地址 实现 _format_addr 函数以正确格式化发件人的电子邮件地址如果提供了显示名称则包含在内。 初始化电子邮件消息对象 创建 MIMEMultipart 的实例它将作为电子邮件消息的容器。 设置发件人信息 在电子邮件消息中设置发件人的信息包括如果提供了发件人名称则进行设置。 设置收件人信息 在电子邮件消息中设置收件人的信息。 处理抄送CC信息 如果在 CC 列表中有收件人则相应地更新电子邮件消息。 处理密送BCC信息 如果在 BCC 列表中有收件人则类似于处理 CC 列表。 设置主题和内容 在电子邮件消息中设置主题和内容。 处理附件 遍历附件列表读取每个文件确定其 MIME 类型并将其附加到电子邮件消息中。 尝试连接到 SMTP 服务器并发送电子邮件 尝试使用提供的凭据连接到指定的 SMTP 服务器。如果连接成功则使用用户名和密码进行登录。使用 sendmail 方法将电子邮件发送给指定的收件人。关闭与 SMTP 服务器的连接。 处理异常 实现异常处理以处理在过程中可能发生的错误例如文件未找到、附件读取失败或电子邮件发送失败。
该实现涉及使用 email 模块创建电子邮件消息处理发件人和收件人信息添加附件并使用 smtplib 模块连接到 SMTP 服务器并发送电子邮件。代码被组织成一个函数以便实现可重用性和清晰度。 Python实现发送Email电子邮件-基础版
实现源码
# 导入smtplib模块这个模块是Python的标准库用于发送电子邮件
import smtplib# 从email模块中导入MIMEText类这个类用于创建文本邮件的MIME消息对象
from email.mime.text import MIMEText# 定义一个变量存储QQ邮箱的SMTP服务器授权码此授权码用于登录QQ邮箱SMTP服务器
secretPass xxxxxxxxxxxxxxxxxx # SMTP服务器授权码# 定义一个函数用于发送指定邮箱的邮件
def sendqqmail(sender_email, sender_pass, rec_email, subject, message):# 使用MIMEText类创建一个邮件消息对象其中message参数是邮件的内容msg MIMEText(message)# 设置邮件的主题msg[Subject] subject# 设置邮件的发件人邮箱msg[From] sender_email# 设置邮件的收件人邮箱msg[To] rec_email# 使用smtplib模块的SMTP_SSL类创建一个SSL连接对象连接到QQ邮箱SMTP服务器其中smtp.qq.com是SMTP服务器地址465是端口号# 在这个类中有两个方法login和send_message分别用于登录和发送邮件with smtplib.SMTP_SSL(smtp.qq.com, 465) as smtp:# 使用login方法登录SMTP服务器参数sender_email和sender_pass分别是发件人的邮箱地址和授权码smtp.login(sender_email, sender_pass)# 如果登录成功打印一条消息print(登录邮箱成功)# 使用send_message方法发送邮件参数msg是要发送的邮件消息对象smtp.send_message(msg)# 发送成功后打印一条消息print(邮件发送完毕)# 关闭SMTP服务连接smtp.quit()# 定义一个主函数用于运行整个程序
def main():# 定义发件人的邮箱地址sender_email xxxxxxxxxqq.com # 发信人邮箱# 定义发件人的邮箱授权码emailpass secretPass # 邮箱授权码# 定义收件人的邮箱地址to_email xxxxxxxxx.com # 收信人邮箱# 定义邮件的主题sub_msg 测试python发送邮件 # 邮件主题# 定义邮件的正文内容content 这是我的第一个python发送邮件测试 # 邮件正文内容# 调用sendqqmail函数发送邮件sendqqmail(sender_email, emailpass, to_email, sub_msg, content) # 发送邮件# 执行main函数这是Python的标准模式
if __name__ __main__:main()源码解析
通过SMTP协议发送邮件。
导入必要的模块 smtplib: 用于连接SMTP服务器并发送邮件。MIMEText类用于创建文本邮件的MIME消息对象。
import smtplib
from email.mime.text import MIMEText定义了一个QQ邮箱的SMTP服务器授权码
secretPass xxxxxxxxxxxxxxxxxx # SMTP服务器授权码定义了一个函数 sendqqmail用于发送指定邮箱的邮件 创建MIMEText对象设置邮件主题、发件人、收件人以及邮件内容。使用smtplib.SMTP_SSL创建一个SSL连接对象连接到QQ邮箱SMTP服务器。使用login方法登录SMTP服务器。使用send_message方法发送邮件。打印登录成功和邮件发送完毕的消息然后关闭SMTP服务连接。
def sendqqmail(sender_email, sender_pass, rec_email, subject, message):# ...略定义了主函数 main 定义发件人、邮箱授权码、收件人、邮件主题和邮件内容。调用 sendqqmail 函数发送邮件。
def main():# ...略使用 if __name__ __main__: 来确保代码在作为脚本直接运行时才会执行 main 函数。
if __name__ __main__:main()注意事项
请谨慎存储和处理邮箱密码或授权码不要将其硬编码在代码中或分享给其他人。在使用SMTP服务发送邮件时需要确保你的邮箱开启了SMTP服务并使用了正确的SMTP服务器地址和端口号。这些信息可以从你的邮箱服务提供商获取。
Python实现发送Email电子邮件-完善版
实现源码
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, port25, sendernameNone, attachmentsNone):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] senderif 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_listif 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_listmsg[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))continuetry:with open(file_path, rb) as f:mime_type, encoding mimetypes.guess_type(file_path)if mime_type is None:mime_type application/octet-streampart MIMEApplication(f.read())part.add_header(Content-Disposition, attachment, filenameattachment[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 abcaliyun.com
password password
sender abcaliyun.com
recipients abcabc.cn
cc [abc126.com,abc139.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,portport, sendernamen, attachmentsattachments)源码解析
用于发送带附件的邮件的 Python 脚本。
导入必要的模块 smtplib: 用于连接SMTP服务器并发送邮件。MIMEText创建文本邮件的MIME消息对象。MIMEMultipart创建包含附件的MIME消息对象。MIMEApplication用于处理附件的MIME消息对象。Header用于对邮件头进行编码。parseaddr 和 formataddr用于格式化发件人和收件人地址。mimetypes用于猜测文件的MIME类型。os用于处理文件路径。
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定义了一个发送邮件的函数 send_email 使用 MIMEMultipart 创建一个包含附件的邮件消息对象。格式化发件人和收件人地址。设置邮件主题、发件人、收件人、抄送、密送。将文本内容添加到邮件中。添加附件到邮件中。使用 smtplib 连接到SMTP服务器登录发送邮件然后关闭连接。
def send_email(smtp_server, username, password, sender, recipients, subject, content, cc, bcc, port25, sendernameNone, attachmentsNone):# ...略定义了一个辅助函数 _format_addr 用于格式化地址
def _format_addr(s):name, addr parseaddr(s)return formataddr((Header(name, utf-8).encode(), addr))调用 send_email 函数发送邮件传递了一些必要的参数包括SMTP服务器、发件人、收件人、邮件主题、文本内容、抄送、密送、发件人姓名、附件等信息。
smtp_server smtp.aliyun.com
username abcaliyun.com
password password
sender abcaliyun.com
recipients abcabc.cn
cc [abc126.com,abc139.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,portport, sendernamen, attachmentsattachments)优化
第二段代码相对于第一段代码进行了一些优化主要体现在以下几个方面 支持附件 第二段代码引入了 email.mime.multipart 和 email.mime.application 模块允许通过 attachments 参数添加附件。这使得邮件可以携带更多类型的内容。 更灵活的邮件构建 第二段代码使用 MIMEMultipart 对象创建邮件消息可以更灵活地构建邮件内容包括添加文本部分、HTML部分、以及附件等。 更友好的发件人地址 引入了 _format_addr 辅助函数用于格式化发件人地址支持设置发件人姓名。 更丰富的邮件头信息 使用 Header 对邮件主题进行编码确保支持非ASCII字符的主题。设置了 Content-Disposition 头部用于指定附件的处理方式。 更全面的错误处理 添加了对附件文件是否存在的检查并输出相应的错误信息。在捕获异常时输出更详细的错误信息有助于定位问题。 端口号处理 第二段代码通过 str(port) 25 的判断来决定使用普通 SMTP 还是 SMTP_SSL使得端口的设置更加直观。 更清晰的代码结构 第二段代码通过将不同的功能块划分为函数使得代码结构更加清晰方便维护和阅读。
第二段代码在邮件功能的实现上更为完善具有更多的灵活性和可读性并且考虑到了更多的错误处理情况使得代码更健壮。
总结
这两段代码都是用于发送邮件的简单Python脚本但第二段代码相对于第一段代码进行了一些优化和改进。以下是一些心得总结 支持附件的扩展 第二段代码引入了附件的支持使用了email.mime.multipart和email.mime.application模块使得邮件可以携带更多类型的内容包括文本和附件。 更友好的发件人地址 引入了 _format_addr 辅助函数用于格式化发件人地址支持设置发件人姓名。这样可以使邮件中的发件人信息更加友好和易读。 更丰富的邮件头信息 使用 Header 对邮件主题进行编码确保支持非ASCII字符的主题。同时设置了 Content-Disposition 头部用于指定附件的处理方式提高邮件的兼容性。 更全面的错误处理 第二段代码在处理附件时增加了对附件文件是否存在的检查并在捕获异常时输出更详细的错误信息。这样的改进有助于提高代码的健壮性及时发现并处理潜在问题。 更清晰的代码结构 第二段代码通过将不同功能块划分为函数使得代码结构更清晰。这有助于提高代码的可读性和维护性使每个功能单元更容易理解和修改。
总的来说第二段代码在功能实现上更为完善具有更多的灵活性和可读性并且考虑到了更多的错误处理情况使得代码更加健壮。在编写邮件发送脚本时综合考虑邮件内容的复杂性和错误处理的全面性是很重要的。 文章转载自: http://www.morning.addai.cn.gov.cn.addai.cn http://www.morning.kspfq.cn.gov.cn.kspfq.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.xfdkh.cn.gov.cn.xfdkh.cn http://www.morning.tmbtm.cn.gov.cn.tmbtm.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.rjjys.cn.gov.cn.rjjys.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.qmzhy.cn.gov.cn.qmzhy.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.mlnbd.cn.gov.cn.mlnbd.cn http://www.morning.kpzrf.cn.gov.cn.kpzrf.cn http://www.morning.jtmql.cn.gov.cn.jtmql.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.nxrgl.cn.gov.cn.nxrgl.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.wptdg.cn.gov.cn.wptdg.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.rsfp.cn.gov.cn.rsfp.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.jxmjr.cn.gov.cn.jxmjr.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.nlmm.cn.gov.cn.nlmm.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.jtrqn.cn.gov.cn.jtrqn.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.txtgy.cn.gov.cn.txtgy.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.qichetc.com.gov.cn.qichetc.com http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.mhbcy.cn.gov.cn.mhbcy.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.thrgp.cn.gov.cn.thrgp.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.ljdjn.cn.gov.cn.ljdjn.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.htbsk.cn.gov.cn.htbsk.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.yknsr.cn.gov.cn.yknsr.cn http://www.morning.brld.cn.gov.cn.brld.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.fkgqn.cn.gov.cn.fkgqn.cn http://www.morning.prkdl.cn.gov.cn.prkdl.cn