python邮箱发送

普通发送邮件

使用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 = ["[email protected]","[email protected]"]

# 发件人
sender = "[email protected]"

# 抄送人
cc_lst = ["[email protected]","[email protected]"]

# 准备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("我的图片是:",_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: 邮箱账号 例如:[email protected]
    :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 = "[email protected]"
email_pwd = "xxx"
recv_lst = ["[email protected]"]
cc_lst = ["[email protected]","[email protected]"]
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 = "[email protected]"
    email_pwd = "xxx" #注意这里面是smtp授权密码
    recv_lst = ["xxx",] #要发送邮箱的地址
    cc_lst = ["[email protected]", "[email protected]"] #抄送地址
    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()

你可能感兴趣的:(python)