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

python 发送邮件例子

发布时间:2020-12-17 17:07:57 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 下面只介绍简单的发送脚本 如果需要在生产环境用起来 ?还需要按要求修改脚本 smtplib.SMTP([host[,port[,local_hostname[,timeout]]]]) SMTP.set_debu

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

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:','<[email?protected]>')  
    print smtp.docmd('rcpt to:','<[email?protected]>')  
    #data 指令表示邮件内容   
    print smtp.docmd('data')  
    print smtp.docmd('''''from: [email?protected] 
    to: [email?protected] 
    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:?[email?protected]?
  2. To:?[email?protected]?
  3. Subject:?test?
  4. ?
  5. just?for?test'''??

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 = ["[email?protected]","[email?protected]"]
 
#发送方信息
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()

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

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

    推荐文章
      热点阅读