欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python邮箱发送

程序员文章站 2022-06-11 15:53:14
普通发送邮件 使用email模块和stmplib模块,内容比较固定,配好了即可实现,代码如下 一、普通邮箱发送 二、写函数发送邮箱 三、写一个发送邮箱的类 ......

普通发送邮件

使用email模块和stmplib模块,内容比较固定,配好了即可实现,代码如下

一、普通邮箱发送

# -*- coding:utf-8-*-
import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext   ## 邮件正文
from email.mime.application import mimeapplication
from email.mime.image import mimeimage


# 收件人
to_lst = ["xxx@189.cn","xxx@163.com"]

# 发件人
sender = "xxx@163.com"

# 抄送人
cc_lst = ["xxx@189.cn","xxxa@163.com"]

# 准备email
email = mimemultipart()
email['subject'] = "console日常巡检" #标题
email['from'] = sender
email['to'] = ",".join(cc_lst) # 发送
email['cc'] = ",".join(to_lst) #抄送

# 邮件正文
text = mimetext("这是邮箱内容",_subtype="plain",_charset="utf-8")
text1 = mimetext("我的图片是:<img src='cid:jay'/>",_subtype="html",_charset="utf-8")
email.attach(text)
# 发送附件
fu1 = mimeapplication(open("hello_word.txt",mode='rb').read())
fu1.add_header("content-disposition","attachment",filename = "hello_word.txt")
email.attach(fu1)


# 图片
tu = mimeimage(open("o_kj.gif",mode='rb').read())
tu.add_header("content-id","jay") #对应cid
email.attach(tu)

#发送邮件
smtp = smtplib.smtp()
#连接smtp服务器
smtp.connect("smtp.163.com") ## 填入你选择的smtp服务
# 填写用户名密码,密码是smtp的授权吗
smtp.login(sender,"xxx")
smtp.sendmail(sender,to_lst,email.as_string())
print("ok")

二、写函数发送邮箱

import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.application import mimeapplication
from email.mime.image import mimeimage

def send_mail(username,password,recv,cc,title,content,mail_host='smtp.163.com',port=25):
    """
    发送邮件函数默认使用的是163smtp
    :param username: 邮箱账号 例如:xxx@163.com
    :param password: smtp授权密码: 例如:djsjdiasd
    :param recv: 收件人地址,多账号用逗号分开
    :param cc: 抄送人邮箱地址,多人要用逗号分开
    :param title: 邮箱标题
    :param content: 邮箱内容
    :param mail_host: 邮箱服务器
    :param port: 默认的smtp端口 25
    :return:
    """
    email = mimemultipart()
    email['subject'] = title
    email['from'] = username
    email['to'] = ",".join(recv)
    email['cc'] = ",".join(cc)
    ## 邮箱正文
    text = mimetext(content,_subtype="plain",_charset="utf-8")
    email.attach(text)
    ## 发送邮件
    smtp = smtplib.smtp()
    ## 连接smtp服务器
    smtp.connect(mail_host)
    ## 登录stmp发送邮件
    smtp.login(username,password)
    smtp.sendmail(recv,cc,email.as_string())
    print("ok")


email_user = "xxx@163.com"
email_pwd = "xxx"
recv_lst = ["xxx@163.com"]
cc_lst = ["xxg@189.cn","xxx@163.com"]
title = '测试邮箱标题'
content = '这是邮箱内容'
send_mail(email_user,email_pwd,recv_lst,cc_lst,title,content)

三、写一个发送邮箱的类

import smtplib
from email.mime.multipart import mimemultipart
from email.mime.text import mimetext
from email.mime.application import mimeapplication
from email.mime.image import mimeimage

class sendmail:
    def __init__(self,username,passwd,recv,cc,title,content,file=none,images=none,
                 email_host='smtp.163.com',port=25):
        self.username = username
        self.passwd = passwd
        self.recv = recv
        self.cc = cc
        self.title = title
        self.content = content
        self.file = file
        self.images = images
        self.email_host = email_host
        self.port = port
    def send_mail(self):
        email = mimemultipart()
        if self.file:
            att = mimeapplication(open(self.file,mode='rb').read())
            att.add_header("content-dispostion","attachment",filename=self.file)
            email.attach(att)
        email.attach(mimetext(self.content,_subtype="plain",_charset="utf-8")) ##邮箱正文内容
        email['subject'] = self.title  # 标题
        email['from'] = self.username
        email['to'] = ",".join(self.recv)  # 发送
        email['cc'] = ",".join(self.cc)  # 抄送
        self.smtp = smtplib.smtp()
        self.smtp.connect(self.email_host,self.port)
        # 发送邮件到服务器的对象
        self.smtp.login(self.username,self.passwd)
        try:
            self.smtp.sendmail(self.username,self.recv,email.as_string())
        except exception as e:
            print("邮箱发送失败。。",e)
        else:
            print("邮箱发送成功")
    def __del__(self):
        self.smtp.quit()


if __name__ == '__main__':
    email_user = "xx@163.com"
    email_pwd = "xxx" #注意这里面是smtp授权密码
    recv_lst = ["xxx",] #要发送邮箱的地址
    cc_lst = ["dxxx@189.cn", "xxx@163.com"] #抄送地址
    title = 'python测试邮箱'
    content = '这是邮箱内容python'
    file = ''  #附件文档
    image = '' #附件图片

    mail = sendmail(
        username=email_user,passwd=email_pwd,recv=recv_lst,cc=cc_lst,
        title=title,content=content
        )
    mail.send_mail()