python代码发送邮件功能小全

python代码发送邮件功能小全

1、使用代码给指定账户发送邮件,python使用我总结了套代码,下面一步步分享下。
2、使用python发送邮件,需要添加发送邮件依赖包【smtplib】;发送邮件需要登录发送邮件者,登录方式为授权码登录,如何生成授权码请百度了解下,这里不详细解释了。以上完成后新建.py文件导入以下代码

import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mailserver = "smtp.qq.com"  #邮箱服务器地址
username_send = '123456@qq.com'  #邮箱用户名
password = 'ABCDabcd'   #邮箱密码:需要使用授权码
username_recv = '654321@qq.com'  #收件人,多个收件人用逗号隔开
#uesename_recv2 = '123123@qq.com'
mail = MIMEText('python这是发用的邮件内容')
mail['Subject'] = 'python这是邮件主题'
mail['From'] = username_send  #发件人
mail['To'] = username_recv  #收件人;[]里的三个是固定写法,别问为什么,我只是代码的搬运工

#smtp = smtplib.SMTP(mailserver,port=25) # 连接邮箱服务器,smtp的端口号是25QQ邮箱POP3的端口号995,SMTP的端口号是465或587
smtp=smtplib.SMTP_SSL(mailserver,port=465) #QQ邮箱的服务器和端口号
smtp.login(username_send,password)  #登录邮箱

smtp.sendmail(username_send,username_recv,mail.as_string())# 参数分别是发送者,接收者,第三个是把上面的发送邮件的内容变成字符串
#smtp.sendmail(username_send,uesename_recv2,mail.as_string())
smtp.quit() # 发送完毕后退出smtp
print ('success')

3、发送邮件带附件也是我们常常遇到的情况。代码如下

from email import encoders
from email._header_value_parser import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib

#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件1
att1 = MIMEText(open('D:/123.html', 'rb').read(), 'base64', 'gb2312')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="123.html"' #这里的filename可以任意写,写什么名字,邮件中显示什么名字
msg.attach(att1)
#构造附件2
att2 = MIMEText(open('D:/123.txt', 'rb').read(), 'base64', 'gb2312')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="123.txt"'
msg.attach(att2)

mailserver = "smtp.qq.com"  #邮箱服务器地址
username_send = '123456@qq.com'  #邮箱用户名
password = 'ABCDabcd'   #邮箱密码:需要使用授权码
#收件人,多个收件人用逗号隔开
username_recv =[ '654321@qq.com']
#加邮件头
msg['From'] = username_send   # 邮件发送者名字
msg['To'] =  'all_of_the_recv'   # 邮件接收者名字
msg['Subject'] = "hello"   # 邮件主题
#发送邮件

try:
    server = smtplib.SMTP()
    server.connect(mailserver)
    server.login(username_send,password)#XXX为用户名,XXXXX为密码
    server.sendmail(username_send,username_recv,msg.as_string())
    server.quit()
    print ('发送成功')
except Exception as e:
    print("完蛋咯....",e)

4.1、发送邮件带渲染页面,因为涉及到页面元素等,这里使用二个文件
新建一个mailhtml文件,代码如下

class mailhtmlclass():
    def mailbody(self,folseinfo):
        text = '<html><body>'
        text += '<center>这是信息内容</center>'
        text += '<center>下边是一张图片</center>'
        # html中以<img>标签添加图片,align和width可以调整对齐方式及图片的宽度
        text += '<img src="cid:0" alt="0" align="center" width=70% >'
        text += '<p>这是信息说明'+folseinfo+'</p>'
        text += '</body></html>'
        return text

4.2新建发送邮件mailsent文件,代码如下

import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from Basicmethod.MailHtml import mailhtmlclass

class MailSendclass():
    #发送带有页面和图片的;username_recv 为list 
    def mailsendpicture(self,picturename,fouseinfo):
        message = MIMEMultipart()  # 邮件主体
        # 邮件加入文本内容
        # text = '<img src="cid:0">'   # html文本引入id为0的图片
        text = mailhtmlclass().mailbody(fouseinfo)
        m_text = MIMEText(text, 'html', 'utf-8')
        message.attach(m_text)
        # 邮件加入图片
        m_img = MIMEBase('image', 'png')
        m_img.add_header('Content-Disposition', 'attachment')
        m_img.add_header('Content-ID', '<0>')  # 设置图片id为0
        f = open("/Users/picture/"+picturename+".png", "rb")  # 读取本地图片
        m_img.set_payload(f.read())
        encoders.encode_base64(m_img)
        message.attach(m_img)
        #启用连接邮箱
        mailserver = "smtp.qq.com"  # 邮箱服务器地址
        username_send = '123456@qq.com'  # 邮箱用户名
        password = 'abcdABCD'  # 邮箱密码:需要使用授权码
    	#收件人,多个收件人用逗号隔开
        username_recv = ['654321@qq.com']
        message['From'] = Header('python自动程序')  # 邮件发送者名字
        message['To'] = Header('接收人名字')  # 邮件接收者名字
        message['Subject'] = Header('信息和截图内容!')  # 邮件主题
        #发送操作
        mail = smtplib.SMTP()
        mail.connect(mailserver)  # 连接 qq 邮箱
        mail.login(username_send, password)  # 账号和授权码
        mail.sendmail(username_send, username_recv, message.as_string())
        mail.quit()
        #print("sucess", username_recv)
        return picturename
picturename = "123" #存在的真实的图片名称
fouseinfo = “要展示的信息”
rest = MailSendclass().mailsendpicture(picturename,fouseinfo)

5、运行mailsent文件,即可发送邮件成功。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值