首页 > 编程语言 >[转]python发邮件smtplib和email模块详解

[转]python发邮件smtplib和email模块详解

时间:2023-01-27 21:33:48浏览次数:58  
标签:python smtp MIMEText msg smtplib com email 邮件

本文转自:https://www.cnblogs.com/auguse/articles/14578247.html

SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。,python实现发邮件也是基于此基础上进行封装的。

  1. python发邮件所需要的基础包

    • python发送邮件需要用到python内置的两个模块,smtplib和email。直接import导入即可

    • python的smtplib提供了一种很方便的途径发送电子邮件,它对smtp协议进行了简单的封装。

  2. smtplib的用法

    smtplib用法相对来说很简单,就是分为三步。

    • 创建SMTP的操作对象并连接smtp目标服务器,可以是126、163、QQ等

    • 根据自己的账号登录目标服务器(自己的邮箱地址和邮箱授权码)

    • 调用对象中的方法,发送邮件到目标地址

实例一:发送普通邮件

   
#需要使用smtplib库,来进行邮箱的连接
import smtplib
#处理邮件内容 MIMEText为文本内容
from email.mime.text import MIMEText

#****************编写一封邮件********************
#邮件正文
content = '你好,蜗牛BOSS4.0已经测试完成,测试通过'
#三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
msg = MIMEText(content, 'plain', "utf-8")
#定义邮件主题
msg['Subject'] = "邮件主题"
#发件人
msg['From'] = "[email protected]"
#收件人
msg["To"] = "[email protected]"

#******************发送邮件***********************
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.126.com", port=465)
#使用用户名和授权码登录
smtp.login("[email protected]", "safsafsadfsafa")
#三个参数:发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail("[email protected]", "[email protected]", msg.as_string())

smtp.quit()
 

实例二:发送HTML内容

   
#需要使用smtplib库,来进行邮箱的连接
import smtplib
#处理邮件内容 MIMEText为文本内容
from email.mime.text import MIMEText

#****************发送HTML格式的邮件********************
#邮件正文
content = """
<p>xx系统测试结果说明:</p>
<p><a href="http://www.baidu.com">本次测试通过,点击即可查看详细信息</a></p>
"""
#三个参数:第一个为文本内容,如果是文本内容则写 plain,本次内容为HTML,则写成HTML,第三个 utf-8 设置编码
msg = MIMEText(content, 'html', "utf-8")
#定义邮件主题
msg['Subject'] = "邮件主题"
#发件人
msg['From'] = "[email protected]"
#收件人
msg["To"] = "[email protected]"

#******************发送邮件***********************
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.126.com", port=465)
#使用用户名和授权码登录
smtp.login("[email protected]", "SBRIVHsafdsafdsad")
#三个参数:发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail("[email protected]", "[email protected]", msg.as_string())

smtp.quit()
 

实例三:发送邮件带附件(非图片)

   
#需要使用smtplib库,来进行邮箱的连接
import smtplib
#处理邮件内容 MIMEText为文本内容
from email.mime.text import MIMEText
#MIMEMultipart创建带附件的邮箱实例
from email.mime.multipart import MIMEMultipart


#****************邮件带附件(非图片)********************

#创建带附件的邮件实例
msg = MIMEMultipart()
#定义邮件主题
msg['Subject'] = "邮件主题"
#发件人
msg['From'] = "[email protected]"
#收件人
msg["To"] = "[email protected]"

#邮件正文
content = """念去去、千里烟波、暮霭沉沉楚天阔"""
#三个参数:第一个为文本内容,如果是文本内容则写,第三个 utf-8 设置编码
msg.attach(MIMEText(content, 'plain', "utf-8"))

#构造附件,传送当前目录下的 test.txt 文件
att = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att["Content-Disposition"] = 'attachment; filename="test.txt"'
msg.attach(att)


#******************发送邮件***********************
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.126.com", port=465)
#使用用户名和授权码登录
smtp.login("[email protected]", "SASBRISADASDADJGRC")
#三个参数:发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail("[email protected]", "[email protected]", msg.as_string())

smtp.quit()
 

实例四:发送邮件带附件图片

   
#需要使用smtplib库,来进行邮箱的连接
import smtplib
#处理邮件内容 MIMEText为文本内容
from email.mime.text import MIMEText
#MIMEMultipart创建带附件的邮箱实例
from email.mime.multipart import MIMEMultipart


#****************邮件带附件图片********************

#创建带附件的邮件实例
msg = MIMEMultipart()
#定义邮件主题
msg['Subject'] = "邮件主题"
#发件人
msg['From'] = "[email protected]"
#收件人
msg["To"] = "[email protected]"

#邮件正文
content = """念去去、千里烟波、暮霭沉沉楚天阔"""
#三个参数:第一个为文本内容,如果是文本内容则写,第三个 utf-8 设置编码
msg.attach(MIMEText(content, 'plain', "utf-8"))

#构造附件,传送当前目录下的 test.txt 文件
att = MIMEText(open('1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)


#******************发送邮件***********************
#第一参数是smtp服务器,第二参数是端口
smtp = smtplib.SMTP_SSL("smtp.126.com", port=465)
#使用用户名和授权码登录
smtp.login("[email protected]", "DSDFSFSAFSDFEU")
#三个参数:发件人、收件人、as_string()是将msg(MIMEText或MIMEMultipart对象)变为str
smtp.sendmail("[email protected]", "[email protected]", msg.as_string())

smtp.quit()
   


__EOF__

   

 

标签:python,smtp,MIMEText,msg,smtplib,com,email,邮件
From: https://www.cnblogs.com/freeliver54/p/17069370.html

相关文章

  • [转]用Python自动化操作PPT
    本文转自:https://jishuin.proginn.com/p/763bfbd771b6作者:超级大洋葱806https://tangxing.blog.csdn.net/article/details/1095688301.PPT自动化能干什么?有什么优势?它......
  • [转]利用python的PyPDF2和PyMuPDF库玩转PDF的提取、合并、旋转、缩放、加密
    本文转自:https://www.cnblogs.com/steven0325/p/16888425.html一、安装PyPDF2和PyMuPDF库pipinstallPyPDF2pipinstallpymupdf#fitz是pymupdf的子模块二、工具......
  • [转]python库 Pywin32使用
    本文转自:https://www.cnblogs.com/chenjy1225/p/12174889.htmlpython库Pywin32使用 https://github.com/wuxc/pywin32docPywin32提供了很多访问windows的API。较重......
  • [转] python-docx
    https://python-docx.readthedocs.io/en/latest/ https://gitcode.net/mirrors/python-openxml/python-docx/-/tree/master/docx python-docxReleasev0.8.11(Inst......
  • [转]Python3 xlrd库基本教程
    本文转自:https://www.w3cschool.cn/python3/python3-xlrd.htmlxlrd库是一个python用于操作excel的第三方库。它的主要功能是用来读取excel。通常会与xlwt 、 xlutils组......
  • Python——02.变量
    变量概念:--变量:将数据临时存储在内存中,该内存名字就是变量,此内存为内存储,相对应的外存储为永久存储举例:手机存储量12(内存储:临时)+286G(外存储)查看内......
  • [python3.10] 引用第三方库 PyMouse、PyKeyboard
    importPyMouse前提条件,__init__.py文件中将fromwindowsimportPyMouse,PyMouseEvent 改为  frompymouse.windows importPyMouse,PyMouseEventpyHook第......
  • Python入门笔记
    Python入门笔记Nowisbetterthannever.Althoughneverisoftenbetterthanrightnow.—————TheZenofPython,byTimPeters目录Python入门笔记1.前言py......
  • 005 python 打开windows下的软件,python打开记事本 subprocess
    如果要用python代码打开windos上的记事本,代码如下:importsubprocessnotePro=subprocess.Popen('notepad.exe')print(notePro)如果要打开别的软件,则把notepad.exe......
  • Python 介绍和环境准备
    目录一、概述二、Python应用领域1)Web应用开发2)自动化运维3)网路爬虫4)人工智能领域5)科学计算三、Python环境准备1)Linux平台安装Python2)Window平台安装Python3)安装pyt......