美文网首页
使用python代码发送邮件

使用python代码发送邮件

作者: 王文强Python | 来源:发表于2020-04-14 10:05 被阅读0次

    封装成类

    from email import encoders
    from email.mime.text import MIMEText
    from email.mime.base import MIMEBase
    from email.mime.image import MIMEImage
    from email.mime.multipart import MIMEMultipart
    from email.parser import BytesParser, Parser
    import smtplib
    
    class BsSmtplib:
        def __init__(self):
            self.email = '邮箱地址'
            self.pwd = '邮箱密码'
            self.host = "smtp.mxhichina.com"   #不同类型邮箱这里需要修改
            self.port = "465" #不同类型邮箱这里需要修改
    
        def send_text(self, subject, msg_text, to_recipients):
            if not isinstance(to_recipients, list):
                raise TypeError('to_recipients类型不正确,需要时list类型。')
            msg = MIMEText(msg_text)
            msg['Subject'] = subject
            msg['From'] = self.email
            msg['To'] = ', '.join(to_recipients)
    
            server = smtplib.SMTP_SSL(self.host, self.port)
            server.login(self.email, self.pwd)
            server.sendmail(self.email, to_recipients, msg.as_string())
            server.quit()
    
        def send_with_attachments(self, subject, msg_html, to_recipients, images=None, atts=None):
            if not isinstance(to_recipients, list):
                raise TypeError('to_recipients类型不正确,需要时list类型。')
            msg = MIMEMultipart()
            msg['Subject'] = subject
            msg['From'] = self.email
            msg['To'] = ','.join(to_recipients)
            html_body = MIMEText(msg_html, 'html')
            msg.attach(html_body)
    
            if images is not None:
                for f_name in images:
                    with open(f_name, 'rb') as f:
                        img = MIMEImage(f.read())
                        img.add_header('Content-Disposition',
                                       'attachment',
                                       filename=os.path.basename(f_name))
                        msg.attach(img)
    
            if atts is not None:
                for att in atts:
                    part = MIMEBase('application', "octet-stream")
                    with open(att, 'rb') as f:
                        part.set_payload(f.read())
                        encoders.encode_base64(part)
                        part.add_header(
                            'Content-Disposition',
                            'attachment; filename="{}"'.format(os.path.basename(att))
                        )
                        msg.attach(part)
            print(to_recipients)
            server = smtplib.SMTP_SSL(self.host, self.port)
            server.login(self.email, self.pwd)
            server.sendmail(self.email, to_recipients, msg.as_string())
            server.quit()
    

    用法

    MailSend = BsSmtplib()
    subject ="邮件标题"
    content ="##########邮件内容##########"
    to_recipients = ['收件箱地址1','收件箱地址2','收件箱地址3']
    MailSend.send_with_attachments(subject,content,to_recipients)
    

    相关文章

      网友评论

          本文标题:使用python代码发送邮件

          本文链接:https://www.haomeiwen.com/subject/hgfzmhtx.html