py 设计优美的邮件发送风格

先看效果
image.png
image.png
完整代码
import smtplib
from email.mime.text import MIMEText


class SendMail:

    def __init__(self):
        self.username = "xxx"
        self.pw = "xxx"
        self.from_addr = self.username
        self.smtp = "smtp.exmail.qq.com"
        self.smtp_port = 465

    def login(self):
        s = smtplib.SMTP_SSL(self.smtp, self.smtp_port)
        s.login(self.username, self.pw)
        return s

    def send_to_audit(self, order_user, order_id, reason, sql, to_addr_list):
        try:

            msg_template_audit = """

            <div  style=" padding: 1em;">
                <div style="display: table;caption-side: top;width: 100%;">
                    <div style="padding-top: 10px">
                        <h1 style="display: inline-block; margin-right: 2em;">申请理由:</h1>
                            <p>{reason}</p>
                        <h2 style="display: inline-block; margin-right: 2em;">待审批的Sql:</h2>
                            <p>{sql}
                            </p>
                            </div>
                                <nav style="display: table-caption;" role="navigation">
                                    <ol style="display: table-row;">
                                        <li style=" display: inline-block; margin-right: 1em;"><a href="https://www.baidu.com" target="_blank">同意</a></li>
                                        <li style=" display: inline-block; argin-right: 1em;"><a href="#" target="_blank">拒绝</a></li>
                                    </ol>
                                </nav>
                            </div>
                    </div>
                </div>
            </div>

            """.format(sql=sql, reason=reason)

            msg = MIMEText(msg_template_audit, "html", 'utf-8')
            msg["Subject"] = "{0}发起Sql工单号为{1}的执行申请".format(order_user, order_id)
            msg["From"] = self.from_addr
            msg["To"] = ""
            self.login().sendmail(from_addr=self.from_addr, to_addrs=to_addr_list, msg=msg.as_string())
            print("send audit mail success")
        except Exception as e:
            print(e)

    def send_to_notify(self, order_id, result, msg, to_addr_list):
        try:
            """
                $green: #82CF85;
                $red: #F57E7D;
                $blue: #7db8f5;
                $orange: #FFAC69;
            """
            if result == "success":
                color = "#82CF85"
            elif result == "execute":
                color = "#7db8f5"
            else:
                color = "#F57E7D"

            msg_template_notify = """

                <div style="position: relative;
                    display: block;
                    padding: 1rem;
                    margin: 1rem;
                    background: #E4EAEE;
                    border: 1px solid rgba(black,.15);
                    border-radius: 2px;
                    color: rgba(black,.5);position: relative;
                    display: block;
                    padding: 1rem;
                    margin: 1rem;
                    background: {color};
                    border: 1px solid rgba(black,.15);
                    border-radius: 2px;
                    color: rgba(black,.5);">
                    <strong>Hello!</strong> {msg}!
                </div>

            """.format(result=result, color=color, msg=msg)

            msg = MIMEText(msg_template_notify, "html", 'utf-8')
            msg["Subject"] = "工单号{0}待执行".format(order_id) if result == "execute" else "Sql工单号为{0}的执行结果".format(order_id)
            msg["From"] = self.from_addr
            msg["To"] = ""
            self.login().sendmail(from_addr=self.from_addr, to_addrs=to_addr_list, msg=msg.as_string())
            print("send notify mail success")
        except Exception as e:
            print(e)

# SendMail().send_to_audit("hugo", 12, "hot fix", "select * from app-api", ["chenpengren@0easy.com"])
SendMail().send_to_notify(12, "execute", "工单号12待执行", ["chenpengren@0easy.com"])

这是一个不错的细说https://www.jianshu.com/p/abb2d6e91c1f

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容