python用yagmail库自动发送邮件

# 第三方库,需要pip install yagmail,目前这个库是发送邮件最简单的库,邮件已发送记录是没有的
import yagmail


class SendEmail(object):
    # user 代表用户名
    # password 代表邮箱授权密码(非邮箱登录密码)
    # host 代表发信服务器
    # port 发信端口
    # smtp_ssl 使用ssl协议(默认为true)
    # encoding 编码方式(默认utf-8)
    user = "*********@qq.com"
    password = "************"
    host = "smtp.qq.com"
    port = 465

    def __init__(self, to, subject, contents) -> None:
        self.to = to
        self.subject = subject
        self.contents = contents
        self.yag = yagmail.SMTP(user=self.user, password=self.password, host=self.host, port=self.port)

    def run(self) -> None:
        self.yag.send(
            # to 收件人,如果一个收件人用字符串,多个收件人用列表即可
            to=self.to,
            # subject 邮件主题(也称为标题)
            subject=self.subject,
            # contents 邮件正文
            contents=self.contents,
            # attachments 附件,和收件人一致,如果一个附件用字符串,多个附件用列表
            # attachments=[r'c://text.txt', r'd://mmp.exe']
        )
        print("发送成功!")

    def __del__(self) -> None:
        self.yag.close()


def main():
    to = ['************@qq.com']
    subject = "****151"
    contents = "grgghgawwae"
    send_email = SendEmail(to, subject, contents)
    send_email.run()


if __name__ == '__main__':
    main()
上一篇:Shiro框架学习


下一篇:Web_php_unserialize