美文网首页
Python3 发送邮件

Python3 发送邮件

作者: 花泽菠菜xqc | 来源:发表于2019-05-08 19:21 被阅读0次

    Python3 发送邮件

    使用第三方库 yagmail


    更新: 第三种方式的隐藏用户名和密码的方式,目前不再支持


    简单介绍

    目标是尽可能简单,无痛地发送电子邮件。

    最终的代码如下:

    import yagmail
    yag = yagmail.SMTP()
    contents = ['This is the body, and here is just text http://somedomain/image.png',
                'You can find an audio file attached.', '/local/path/song.mp3']
    yag.send('to@someone.com', '邮件标题', contents)
    
    

    或者在一行中实现:

    yagmail.SMTP('mygmailusername').send('to@someone.com', 'subject', 'This is the body')
    
    

    当然, 以上操作需要从你自己系统的密钥环中读取你的邮箱账户和对应的密码。关于密钥环稍后会提到如何实现。

    安装模块

    pip3 install  yagmail  # linux / Mac
    
    pip install   yagmail    # windows
    
    

    这样会安装最新的版本,并且会支持所有最新功能,主要是支持从密钥环中获取到邮箱的账户和密码。

    关于账户和密码

    开通自己邮箱的 SMTP 功能,并获取到授权码

    这个账户是你要使用此邮箱发送邮件的账户,密码不是平时登录邮箱的密码,而是开通 POP3/SMTP 功能后设置的客户端授权密码。

    这里以 126 邮箱为例:

    image image image

    方式一:不使用系统的密钥环

    不使用系统的密钥环,可以直接暴露账户和密码在脚本里

    import yagmail
    yag = yagmail.SMTP(
                user='自己的账号',
                password='账号的授权码',
                host='smtp.qq.com',  # 邮局的 smtp 地址
                port='端口号',       # 邮局的 smtp 端口
                smtp_ssl=False)
    
    yag.send(to='收件箱账号',
             subject='邮件主题',
             contents='邮件内容')
    
    

    方式二: 使用系统的密钥环管理账户和授权码

    模块支持从当前系统环境中的密钥环中获取账户和密码,要想实现这个功能,需要依赖模块 keyring。之后把账户和密码注册到系统的密钥环中即可实现。

    1. 安装依赖模块

    pip3 install  keyring 
    
    # CentOS7.3 还需要安装下面的模块
    pip3 install keyrings.alt
    
    

    2. 开始向密钥环注册

    import yagmail
    yagmail.register('你的账号', '你的授权密码')
    
    

    注册账户和密码,只需要执行一次即可。

    3. 发送邮件

    import yagmail
    
    yag = yagmail.SMTP('自己的账号',
                host='smtp.qq.com',  # 邮局的 smtp 地址
                port='端口号',       # 邮局的 smtp 端口
                smtp_ssl=False  # 不使用加密传输
    )
    
    yag.send(
        to='收件箱账号',
        subject='邮件主题',
        contents='邮件内容')
    
    

    示例展示

    下面是以我的 126 邮箱为例, 使用系统密钥环的方式,向我的 163邮箱发送了一封邮件。

    import yagmail
    
    yag = yagmail.SMTP(user='shark@126.com',
                       host='smtp.126.com',
                       port=25,
                       smtp_ssl=False)
    yag.send(to='docker@163.com',
             subject='from shark',
             contents='test')
    
    

    这样就愉快的发送了一封测试邮件到 docker@163.com 的邮箱。

    当然前提是:

    1. 126 邮箱开通了 SMTP功能。
    2. 把 126 邮箱的账号和密码已经注册到自己系统的密钥环中。

    发送附件

    发送

    发送附件只需要给 send方法传递 attachments 关键字参数

    比如我在系统的某一个目录下有一张图片,需要发送给 docker@163.com

    import yagmail
    
    yag = yagmail.SMTP(user='shark@126.com',
                       host='smtp.126.com',
                       port=25,
                       smtp_ssl=False)
    yag.send(to='docker@163.com',
             subject='from shark',
             contents='test',
             attachments='./松鼠.jpeg')
    
    

    收到的邮件和附件

    image

    使用 ssl 发送加密邮件

    要发送加密邮件,只需要把 smtp_ssl 关键字参数去掉即可,因为默认就是采用的加密方式 smtp_ssl=True

    不传递 stmp_ssl 关键字参数的同时,需要设置端口为邮箱服务提供商的加密端口,这里还是以 126 邮箱为例,端口是 465

    import yagmail
    
    yag = yagmail.SMTP(user='shark@126.com',
                       host='smtp.126.com',
                       port=465)
    yag.send(to='docker@163.com',
             subject='from sharkyunops',
             contents='test',
             attachments='./松鼠.jpeg')
    
    

    发送 带 html 标记语言的邮件内容

    在实际的生产环境中,经常会发送邮件沟通相关事宜,往往会有表格之类的内容,但是又不想以附件的形式发送,就可以利用 html 标记语言的方式组织数据。

    import yagmail
    
    yag = yagmail.SMTP(user='shark@126.com',
                       host='smtp.126.com',
                       port=465)
    
    html="""<table border="1">
        <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>shak</td>
            <td>18</td>
        </tr>
        <tr>
            <td>西瓜甜</td>
            <td>28</td>
        </tr>
        </tbody>
    </table>
    """
    
    yag.send(to='docker@163.com',
             subject='from sharkyunops',
             contents=['test',html])
    
    
    image

    更多

    如果不指定to参数,则发送给自己

    如果to参数是一个列表,则将该邮件发送给列表中的所有用户

    attachments 参数的值可以是列表,表示发送多个附件

    相关文章

      网友评论

          本文标题:Python3 发送邮件

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