网站建设资讯

NEWS

网站建设资讯

发邮件

发邮件:https://www.cnblogs.com/hudieren/p/.html

创新互联建站是少有的网站设计、成都网站建设、营销型企业网站、成都微信小程序、手机APP,开发、制作、设计、卖友情链接、推广优化一站式服务网络公司,成立于2013年,坚持透明化,价格低,无套路经营理念。让网页惊喜每一位访客多年来深受用户好评

收邮件:https://www.cnblogs.com/hudieren/p/.html

解析邮件:https://www.cnblogs.com/hudieren/p/.html

删除邮件:https://www.cnblogs.com/hudieren/p/.html

# -*- coding: utf-8 -*-
# @Author  : 107
# @File    : emaildriver.py
# @explain : 发邮件
import json
import re
import smtplib
import email
import time
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
import imaplib

import os


class SendEmail:
def __init__(self, sender, sender_name, sender_token, attn, attn_name, subject, connect, files, server):
"""
        :param sender: 发件人邮箱账号
        :param sender_name: 发件人邮箱昵称
        :param sender_token: 发件人邮箱密码(当时申请smtp给的口令)
        :param attn: 收件人邮箱账号
        :param attn_name: 收件人邮箱昵称
        :param subject: 主题
        :param connect: 内容
        :param files: 附件,格式:{'path': '路径', 'read': 'r', 'encoding': 'utf-8', '_charset': 'utf-8', 'name': '文件名'}
        :param server: 邮件服务器地址
"""
        self.sender= sender
        self.sender_name= sender_name
        self.sender_token= sender_token
        self.attn= attn
        self.attn_name= attn_name
        self.subject= subject
        self.connect= connect
        self.files= files
        self.server= server

def accessory(self, file):
"""
        处理单个附件
        :param file:
        :return:
"""
        path= file.get('path')
        rea= file.get('read')
        e= file.get('encoding')
        _charset= file.get('_charset')
        name= file.get('name')
if not name:
            name= os.path.split(path)[-1]
if e:
            with open(path, rea, encoding=e) as r:
                connect= r.read()
else:
            with open(path, rea) as r:
                connect= r.read()
        acc= MIMEText(connect, _charset=_charset)
        acc["Content-Type"] = 'application/octet-stream'
        # 不能兼容outlook
        # acc1["Content-Disposition"] = f'attachment; filename="{name}"'
        # 可以兼容outlook
        acc.add_header("Content-Disposition", "attachment", filename=("gb", "", name))
return acc

def send_email(self):
# connect = "这是我使用python smtplib及email模块发送的邮件"  # 正文
        msg = MIMEMultipart()
# 括号里的对应发件人邮箱昵称、发件人邮箱账号
        msg['From'] = formataddr([self.sender_name, self.sender])
# 括号里的对应收件人邮箱昵称、收件人邮箱账号
        msg['To'] = formataddr([self.attn_name, self.attn])
# 邮件的主题,也可以说是标题
        msg['Subject'] = self.subject

# 正文
        msg.attach(MIMEText(self.connect, _charset='utf-8'))
# 附件
        for file in self.files:
            acc= self.accessory(file)
            msg.attach(acc)

# 发件人邮箱中的SMTP服务器,端口是465
        # server = smtplib.SMTP_SSL("smtp.qq.com", 465)
        server = smtplib.SMTP_SSL(self.server, 465)

# 括号中对应的是发件人邮箱账号、邮箱密码
        server.login(self.sender, self.sender_token)
# 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.sendmail(self.sender, [self.attn, ], msg.as_string())

        server.quit()# 关闭连接

    def run(self):
# self.send_email()
        try:
            self.send_email()
return True
except Exception as e:
print(e.args)

新闻名称:发邮件
浏览路径:http://cdweb.net/article/dsogcec.html