加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 发送邮件例子

发布时间:2020-05-25 01:13:58 所属栏目:Python 来源:互联网
导读:python 发送邮件例子

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

下面只介绍简单的发送脚本 如果需要在生产环境用起来 还需要按要求修改脚本

smtplib.SMTP([host[,port[,local_hostname[,timeout]]]])

SMTP.set_debuglevel(level)

SMTP.connect([host[,port]])

SMTP.docmd(cmd[,argstring])

    import smtplib,base64,time  
    userName = base64.encodestring('from').strip()  
    password = base64.encodestring('password').strip()  
    smtp = smtplib.SMTP()  
    smtp.connect("smtp.yeah.net:25")  
    print smtp.docmd('helo','from')  
    print smtp.docmd('auth login')  
    print smtp.docmd(userName)  
    print smtp.docmd(password)  
    print smtp.docmd('mail from:','<[emailprotected]>')  
    print smtp.docmd('rcpt to:','<[emailprotected]>')  
    #data 指令表示邮件内容   
    print smtp.docmd('data')  
    print smtp.docmd('''''from: [emailprotected] 
    to: [emailprotected] 
    subject: subject 
    email body 
    . 
    ''')  
    smtp.quit()  

SMTP.helo([hostname])

SMTP.has_extn(name)

SMTP.verify(address)

SMTP.login(user,password)

SMTP.sendmail(from_addr,to_addrs,msg[,mail_options,rcpt_options])

  1. '''''From:[emailprotected]
  2. To:[emailprotected]
  3. Subject:test
  4. justfortest'''

SMTP.quit()

email及其相关子模块

#!/usr/bin/python
#-*-coding:UTF-8-*-
 
import smtplib
import time
 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
 
#收件人列表
mail_namelist = ["[emailprotected]","[emailprotected]"]
 
#发送方信息
mail_user = "邮箱地址"
mail_pass = "邮箱密码"
 
#邮件标题
mail_subject = "python 发送测试文件"
#邮件文本内容
mail_context = "是邮件内容~~  ooxx"
 
 
 
def send_main():
        msg = MIMEMultipart()
        msg['From'] = mail_user
        msg['To'] = ";".join(mail_namelist)
        msg['Subject'] = mail_subject
#添加邮件内容
        txt = MIMEText("这是邮件内容~~  ooxx")
        msg.attach(txt)
 
 
#发送邮件   
 
        smtp = smtplib.SMTP()
        smtp.connect('smtp.qq.com:25')
        smtp.login(mail_user,mail_pass)
 
        smtp.sendmail(mail_user,mail_namelist,msg.as_string())
 
        smtp.quit()
        print ('邮件发送成功')
 
if __name__ == '__main__':
        send_main()

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读