Untitled

 avatar
unknown
python
3 years ago
1.8 kB
1
Indexable
import smtplib
from email.mime.text import MIMEText

class Mail:
    sender: str = None

    to_list: list = None
    cc_list: list = None
    bcc_list: list = None

    content: str = None
    title: str = None
    MIME: str = None
    encoding: str = None

    smtp_addr: str = None
    smtp_port: int = None
    smtp_user: str = None
    smtp_pass: str = None

    def __init__(self, addr: str, port: int, user: str, pwd: str, sender: str):
        self.smtp_addr = addr
        self.smtp_port = port
        self.smtp_user = user
        self.smtp_pass = pwd
        self.sender = sender

    def set_content(self, title: str, content: str, MIME="html", encoding="utf8"):
        self.title = title
        self.content = content
        self.MIME = MIME
        self.encoding = encoding

    def set_list(self, to_list: list = None, cc_list: list = None, bcc_list: list = None):
        self.to_list = to_list
        self.cc_list = cc_list
        self.bcc_list = bcc_list
        if self.to_list is None:
            self.to_list = []
        if self.cc_list is None:
            self.cc_list = []
        if self.bcc_list is None:
            self.bcc_list = []


    def send(self):
        msg = MIMEText(self.content, self.MIME, self.encoding)
        msg["From"] = self.sender
        msg["To"] = ",".join(self.to_list)
        msg["Cc"] = ",".join(self.cc_list)
        msg['Subject'] = self.title
        # msg.attach(my_msg_body)
        real_send_list = self.to_list + self.bcc_list
        server = smtplib.SMTP_SSL(self.smtp_addr, self.smtp_port)
        server.login(self.smtp_user, self.smtp_pass)
        server.sendmail(self.sender, real_send_list, msg.as_string())
        server.quit()