美文网首页
python发送邮件

python发送邮件

作者: 繁华丶凋零 | 来源:发表于2022-08-15 16:03 被阅读0次

    实现机制: email库

    1.脚本

    import sys
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email import encoders
    
    
    class MailApi(object):
    
        def __init__(self, host, port, user, password):
            self.mail_from = user
            self.mail_attachment = []
            self.smtp = smtplib.SMTP(host, port)
            # self.smtp.set_debuglevel(1)
            self.smtp.starttls()
            self.smtp.login(user, password)
    
        def add_attachment(self, filepath):
            '''
                添加附件
            '''
            att = MIMEBase('application', 'octet-stream')
            att.set_payload(open(filepath, 'rb').read())
            filename = filepath.split('/')[-1]
            att.add_header('Content-Disposition', 'attachment', filename=('gbk', '', filename))
            encoders.encode_base64(att)
    
            self.mail_attachment.append(att)
    
        def send(self, subject, content, mail_to=[], mail_cc=[], mail_bcc=[]):
            '''
                发送邮件
            '''
            msg = MIMEMultipart('alternative')
            contents = MIMEText(content, "html", _charset='utf-8')
            msg['Subject'] = subject
            msg['From'] = self.mail_from
            msg['To'] = ','.join(mail_to)
            if mail_cc:
                msg['Cc'] = ','.join(mail_cc)
                mail_to.extend(mail_cc)
            if mail_bcc:
                msg['Bcc'] = ','.join(mail_bcc)
                mail_to.extend(mail_bcc)
    
            for att in self.mail_attachment:
                msg.attach(att)
            msg.attach(contents)
            try:
                self.smtp.sendmail(self.mail_from, mail_to, msg.as_string())
                return True
            except Exception as e:
                print(str(e))
                return False
    
        def close(self):
            self.smtp.quit()
    
    
    
    mail_body_template = '''
    Dear All:<br />
    <br />
    test<br />
    <table border="1" cellpadding="0" cellspacing="0">
        <tr>
            <td>发布人员</td>
            <td>盖伦</td>
        </tr>
    </table>
    <br />
    BR
    
        '''
    
    
    RELEASE = {}
    RELEASE['to'] = ['邮箱1','邮箱2']
    RELEASE['cc'] = ['邮箱3','邮箱4']
    RELEASE['subject'] = 'test test'
    
    
    mail_body = mail_body_template
    mail_host = '邮箱服务器'
    mail_user = '邮箱服务器用户'
    mail_pass = '邮箱服务器密码'
    mail_to = RELEASE['to']
    mail_cc = RELEASE['cc']
    mail_bcc = ['']
    mail_subject = RELEASE['subject']
    mail_api = MailApi(mail_host, 465, mail_user, mail_pass)
    mail_api.add_attachment('附件')
    mail_api.send(mail_subject, mail_body, mail_to, mail_cc)
    mail_api.close()
    
    

    相关文章

      网友评论

          本文标题:python发送邮件

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