zoukankan      html  css  js  c++  java
  • Python自动发邮件——smtplib和email库和yagmail库

    '''
    一、先导入smtplib模块  导入MIMEText库用来做纯文本的邮件模板
    二、发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是  smtp.163.com
    三、写邮件主题和正文,这里的正文是HTML格式的
    四、最后调用SMTP发件服务
    '''
    126mail -> qqmail send email
    复制代码
    import uuid
    import smtplib
    from email.mime.text import MIMEText
    
    #发邮件相关参数
    smtpsever = 'smtp.126.com'
    sender = '123@126.com'
    psw = "XXX"              #126邮箱授权码
    receiver = '456@qq.com'
    
    #编辑邮件的内容
    # subject=u"NBA"
    body=str(uuid.uuid4())
    msg=MIMEText(body,'html','utf-8')
    msg['from']='123@126.com'
    msg['to']='456@qq.com'
    # msg['subject']=subject
    
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpsever)
        smtp.login(sender,psw)
        smtp.sendmail(sender,receiver,msg.as_string())
        print ("邮件发送成功")
    except smtplib.SMTPException:
        print ("Error: 无法发送邮件")
    复制代码

      qqmail->126mail send email 

    复制代码
    import smtplib,uuid
    from email.mime.text import MIMEText
    
    #发邮件相关参数
    smtpsever='smtp.qq.com'
    sender='123@qq.com'
    psw="XXXXX"            #qq邮箱授权码
    receiver='456@qq.com'
    port=465
    
    
    #编辑邮件内容
    subject = u"你猜这是啥?"
    body = str(uuid.uuid4())
    msg=MIMEText(body,'html','utf-8')
    msg['from']='123@qq.com'
    msg['to']='456@qq.com'
    msg['subject'] = subject
    
    #链接服务器发送
    smtp = smtplib.SMTP_SSL(smtpsever,port)
    smtp.login(sender,psw)                          #登录
    smtp.sendmail(sender,receiver,msg.as_string())  #发送
    smtp.quit()                                     #关闭
    复制代码

    发送带附件的邮件

    复制代码
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    #发邮件相关参数
    smtpsever='smtp.qq.com'
    sender='123@qq.com'
    #psw="xxxx"            #126邮箱授权码
    psw="xxxx"
    receiver='456789@qq.com'
    port=465
    
    filepath="E:\result.html"  #编辑邮件的内容
    with open(filepath,'rb') as fp:    #读文件
        mail_body=fp.read()
    
    #主题
    msg=MIMEMultipart()
    msg["from"]=sender
    msg["to"]=receiver
    msg["subject"]=u"这个我的主题"
    
    #正文
    body=MIMEText(mail_body,"html","utf-8")
    msg.attach(body)
    att = MIMEText(mail_body,"base64","utf-8")
    att["Content-Type"] = "application/octet-stream"
    att["Content-Disposition"] = 'attachment; filename="test_report.html"'
    msg.attach(att)
    
    try:
        smtp=smtplib.SMTP()
        smtp.connect(smtpsever)                     #连接服务器
        smtp.login(sender,psw)
    except:
        smtp=smtplib.SMTP_SSL(smtpsever,port)
        smtp.login(sender,psw)  #登录
    smtp.sendmail(sender,receiver,msg.as_string())  #发送
    smtp.quit()
    复制代码

    之前写过用标准库使用Python Smtplib和email发送邮件,感觉很繁琐,久了不用之后便忘记了。前几天看知乎哪些Python库让你相见恨晚?,看到了yagmail第三方库,学习过程中遇到一些问题,记录在此处。

    之前使用的python的smtplib、email模块发模块的一步步骤是:

    一、先导入smtplib模块  导入MIMEText库用来做纯文本的邮件模板
    二、发邮件几个相关的参数,每个邮箱的发件服务器不一样,以126为例子百度搜索服务器是 smtp.126.com
    三、写邮件主题和正文,这里的正文是HTML格式的
    四、最后调用SMTP发件服务

    看下代码:
    复制代码
    import uuid
    import smtplib
    from email.mime.text import MIMEText
    
    #发邮件相关参数
    smtpsever = 'smtp.126.com'
    sender = 'sender@126.com'
    psw = "126mail@126.com"            #126邮箱授权码
    receiver = 'receiver@qq.com'
    
    #编辑邮件的内容
    # subject=u"NBA"
    body=str(uuid.uuid4())
    msg=MIMEText(body,'html','utf-8')
    msg['from']='username@126.com'
    msg['to']='userename@qq.com'
    # msg['subject']=subject
    
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpsever)
        smtp.login(sender,psw)
        smtp.sendmail(sender,receiver,msg.as_string())
        print ("邮件发送成功")
    except smtplib.SMTPException:
        print ("Error: 无法发送邮件")
    复制代码

    要完成上面的邮件发送,需要以下信息:

    1.发件服务器
    2.发件人账号
    3.发件人邮箱密码[指授权码]
    4.收件人账号
    5.编辑邮件内容[正文、从哪里、到哪里]
    6.链接邮箱服务器
    7.登录邮箱[提供发件人账号和密码(指授权码)]
    8.发送邮件

    看起来整个过程有那么一点点复杂,但是也还好吧,毕竟功能可以实现。。。

    但是yagmail的出现会颠覆一些人对email的印象,因为yagmail对比email来说,实现上真的是太简单了,yagmail 在Github上Home Page给出了yagmail的简单介绍以及使用。下面给出一个简洁的示例:

    yagmail安装

    pip install yagmail

    给单个接受者发送邮箱

    复制代码
    import yagmail
    
    #链接邮箱服务器
    yag = yagmail.SMTP(user="sender@126.com", password="126邮箱授权码", host='smtp.126.com')
    
    #邮箱正文
    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(receiver@qq.com', 'subject', contents)
    复制代码

    发送结果如下:

    对比email、smtp模块,yagmail的实现真的是太简单了,感动的要哭了~~~~

     给多个接受者发送邮件

    # 发送邮件
    yag.send(['aa@126.com','bb@qq.com','cc@gmail.com'], 'subject', contents)

    在()内新增多个收件人的邮箱即可,其他参数不需要修改,是不是很简单~~~

    发送带附件的邮件

    yag.send('aaaa@126.com', '发送附件', contents, ["E://whitelist.txt","E://baidu_img.jpg"])

    上面的["E://whitelist.txt","E://baidu_img.jpg"]是上传附件的路径。返回结果如下:

  • 相关阅读:
    腰围2尺1,2,3,4,5,6,7,8寸各自等于是多少厘米/英寸(对比表)
    MySQL Server 5.0 下载与 安装指南[图文] (安装到非系统路径+设置root账号相应password)
    UISearchDisplayController UISearchBar
    第八届蓝桥杯JavaB组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第八届蓝桥杯JavaA组省赛真题
    第七届蓝桥杯JavaC组省赛真题
  • 原文地址:https://www.cnblogs.com/xiaowenshu/p/10237189.html
Copyright © 2011-2022 走看看