python发送邮件封装(可直接使用)

封装代码如下:
author = 'damao'

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

class SendMail(object):

    def __init__(self,smtp_server,
                 smtp_port,
                 smtp_sender,
                 smtp_senderpassword,
                 smtp_receiver,
                 smtp_subject,
                 smtp_body,
                 smtp_file=None):
        """
        to init parameter
        :param smtp_server: 邮件服务器
        :param smtp_port:端口号
        :param smtp_sender:发件人
        :param smtp_senderpassword:密码
        :param smtp_receiver:收件人
        :param smtp_subject:邮件主题
        :param smtp_body:邮件内容
        :param smtp_file_path:文件路径
        """
        self.smtp_server = smtp_server
        self.smtp_port = smtp_port
        self.smtp_sender = smtp_sender
        self.smtp_senderpassword = smtp_senderpassword
        self.smtp_receiver = smtp_receiver
        self.smtp_subject = smtp_subject
        self.smtp_body = smtp_body
        self.smtp_file = smtp_file

    def mail_content(self):
        """
        to edit mail content
        :param subject: 邮件主题
        :param body:邮件内容
        :return:msg
        """
        if self.smtp_file != None :
            msg = MIMEMultipart()
            with open(self.smtp_file,'rb') as fp:
                mail_body =fp.read()
            att = MIMEText(mail_body, "base64", "utf-8")
            att['Conten-Type'] = "application/octet-stream"
            att["Content-Disposition"] = 'attachment; filename="%s"' % self.smtp_file
            msg.attach(att)
            msg.attach(MIMEText(self.smtp_body, "html", "utf-8"))
            msg['from'] = self.smtp_sender
            msg['to'] = ";".join(self.smtp_receiver)
            msg['subject'] = self.smtp_subject
            return msg
        else:
            msg = MIMEText(self.smtp_body, "html", "utf-8")
            msg['from'] = self.smtp_sender
            msg['to'] = ";".join(self.smtp_receiver)
            msg['subject'] = self.smtp_subject
            return msg

    def send_mail(self):
        try:
            smtp = smtplib.SMTP()
            smtp.connect(self.smtp_server)
            smtp.login(user=self.smtp_sender,password=self.smtp_senderpassword)
        except:
            smtp = smtplib.SMTP_SSL()
            smtp.login(user=self.smtp_sender,password=self.smtp_senderpassword)
        aaa = self.mail_content()
        try:
            smtp.sendmail(self.smtp_sender,self.smtp_receiver,aaa.as_string())
            print("发送成功----")
        except Exception as e:
            print("发送失败...",e)
        smtp.quit()
测试代码如下:
if __name__ =='__main__':
    # 普通邮箱测试
    a = SendMail(smtp_server="XXXXXXXXX",
                 smtp_port=XXXX,
                 smtp_sender="XXXXXXXXX.com",
                 smtp_senderpassword="XXXXXXXXX",
                 smtp_receiver="XXXXXXXXX",
                 smtp_subject="大毛的邮件!!",
                 smtp_body="

我的地址:https://www.jianshu.com/u/1f9e71a85238

") a.send_mail() # SSL认证邮箱测试,如:QQ邮箱 b = SendMail(smtp_server="XXXXXXXXX", smtp_port=XXXX, smtp_sender="XXXXXXXXX", smtp_senderpassword="XXXXXXXXX", smtp_receiver="XXXXXXXXXqq.com", smtp_subject="小毛的邮件!!", smtp_body="

我的地址:https://www.jianshu.com/u/1f9e71a85238

") b.send_mail() # 发送多人邮件 c = SendMail(smtp_server="XXXXXXXXX", smtp_port=XXXX, smtp_sender="XXXXXXXXX.com", smtp_senderpassword="XXXXXXXXX", smtp_receiver=["XXXXXXXXX.com","XXXXXXXXX.com","XXXXXXXXX.com"], smtp_subject="大毛的邮件!!", smtp_body="

我的地址:https://www.jianshu.com/u/1f9e71a85238

") c.send_mail() # 带附件的邮件发送 d = SendMail(smtp_server="XXXXXXXXX", smtp_port=XXXX, smtp_sender="XXXXXXXXX.com", smtp_senderpassword="XXXXXXXXX", smtp_receiver="XXXXXXXXX.com", smtp_subject="大毛的邮件!!", smtp_body="

我的地址:https://www.jianshu.com/u/1f9e71a85238

", smtp_file='.\\aaa.txt') d.send_mail()

你可能感兴趣的:(python发送邮件封装(可直接使用))