• Python发送邮件


    发送文本格式邮件,默认使用25端口

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    smtp_srv = 'mail.xx.com'
    from_addr = 'ld@xx.com'
    #to_addr = ('l1@xx.com','ld@xx.com')
    to_addr = ('l1@xx.com')
    cc_addr = 'l1@xx.com'
    password = 'mypass'
    subject = 'pp test'
    msg_content = '你好
    错误记录
    asdfl
    asdf'
    
    msg = MIMEText(msg_content,'plain','utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['from_addr'] = from_addr
    msg['Cc'] = cc_addr
    msg['To'] = ''.join(to_addr)
    
    smtp = smtplib.SMTP()
    smtp.connect(smtp_srv)
    smtp.login(from_addr,password)
    #smtp.set_debuglevel(1) #显示邮件发送的详细信息
    smtp.sendmail(from_addr,to_addr,msg.as_string())
    smtp.quit()

    587端口发送邮件:

    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    smtp_srv = '10.160.31.32'
    from_addr = 'sysuser@xin.com'
    login_addr = 'sysuser' 
    to_addr = ('lll@xin.com')
    password = 'pass'
    subject = 'pytest5872222teaea'
    msg_content = 'tt'
    
    msg = MIMEText(msg_content,'plain','utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    msg['from_addr'] = from_addr
    msg['To'] = ''.join(to_addr)
    
    smtp = smtplib.SMTP()
    smtp.connect(smtp_srv,587) #
    smtp.starttls() #
    smtp.login(login_addr,password) #如果SMTP登录验证失败,则其名称去掉@xin.com域名部分
    smtp.set_debuglevel(1)
    smtp.sendmail(from_addr,to_addr,msg.as_string())
    smtp.quit()

     发送正文带有图片的邮件:

    import os,sys,datetime,time
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage 
    
    smtp_srv = 'mail.x.com'
    from_addr = 'ITS@x.com'
    to_addr = ['ly@x.com']
    cc_addr = 'IT@x.com'
    to_addr2 = ['IT@x.com'] #由于抄送不起作用,所以将邮件重新发送一次
    password = r'password'
    subject = 'Hello' 
    
    msg = MIMEMultipart('related')
    msg['Subject'] = 'test message' 
    
    msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<img alt="" src="cid:image1" />good!','html','utf-8')
    msg.attach(msgText) 
    
    fp = open('D:\pyweb\uxin.jpg', 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close() 
    
    #在正文里添加图片
    msgImage.add_header('Content-ID', 'image1')
    msg.attach(msgImage) 
    
    msg['from_addr'] = from_addr
    msg['To'] = ''.join(to_addr)
    # print msg_content
    smtp = smtplib.SMTP()
    smtp.connect(smtp_srv,587)
    smtp.starttls()
    smtp.login(from_addr,password)
    #smtp.set_debuglevel(1) #显示邮件发送的详细信息
    smtp.sendmail(from_addr,to_addr2,msg.as_string())
    smtp.quit()

     发邮件时显示发件人的中文名称:

    import smtplib
    from email import encoders
    from email.mime.text import MIMEText
    from email.header import Header
    from email.utils import parseaddr, formataddr
    
    smtp_srv = 'smtp.gmail.com'
    from_addr = 'u@gmail.com'
    to_addr = ('l@x.com')
    password = 'Y'
    subject = '您的帐号'
    msg_content = '您好,
    您的密码'
    
    msg = MIMEText(msg_content,'plain','utf-8')
    msg['Subject'] = Header(subject, 'utf-8')
    #msg['from_addr'] = from_addr
    msg['From'] = formataddr(["管理员", from_addr]) #显示发件人的中文名称
    msg['To'] = ''.join(to_addr)
    
    smtp = smtplib.SMTP()
    smtp.connect(smtp_srv,587) #
    smtp.ehlo()
    smtp.starttls() #
    
    login_addr = from_addr #.split('@')[0]
    
    try:
        smtp.login(login_addr,password)
        smtp.set_debuglevel(1)
        smtp.sendmail(from_addr,to_addr,msg.as_string())
        smtp.quit()
    except Exception,e:
        print e

    发送多个图片:

    # -*- coding: UTF-8 -*-
    import time,os,datetime,sys,smtplib,csv,glob
    
    from email import encoders
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    from email.mime.image import MIMEImage 
    
    
    def sendmail(auser,buser,cuser):
        smtp_srv = 'mail.x.com'
        from_addr = '11@x.com'
        to_addr = ('%s@x.com' %(auser))
        password = 'y'
        subject = '报告'
        smtp = smtplib.SMTP()
        smtp.connect(smtp_srv,587) 
        
        msg = MIMEMultipart('related')
        msg['Subject'] = Header(subject, 'utf-8')
        
        msg_content = '<p>您好,<br>员工%s(%s)访问了网站! </p>'%(buser,cuser)
        mail_jpg = ''
        fnumber = len(glob.glob(pathname='E:\Python\imgs\i*.*')) #计算图片目录下图片数量
        for i in  range(fnumber):
            mail_jpg += '<img alt="" src="cid:image%d" /><br>' %(i+1)
        msg.attach(MIMEText(msg_content + mail_jpg, 'html', 'utf-8'))
    
        i=0
        for fpathe,dirs,fs in os.walk('E:\Python\imgs\i'):  #遍历图片所在目录
            for f in fs:
                i=i+1
                img_name = os.path.join(fpathe,f)
                with open(img_name, 'rb') as fp:
                    print(img_name)
                    img_data = fp.read()
                    msg_image = MIMEImage(img_data)
                    msg_image.add_header('Content-ID', '<image%d>' %(i))
                    msg.attach(msg_image)
    
    
        msg['Subject'] = Header(subject, 'utf-8')
        msg['from_addr'] = from_addr
        msg['To'] = ''.join(to_addr)
    
        
        login_addr = from_addr.split('@')[0]
        smtp.login(login_addr,password)
    
    
        smtp.set_debuglevel(1)
        smtp.sendmail(from_addr,to_addr,msg.as_string())
        smtp.quit()
    
    count=0
    with open("E:\Python\imgs\aa.csv") as flist:    
        flists = csv.reader(flist)
        for line in flists:
            user0 = line[0]
            user1 = line[1]
            user2 = line[2]
            count = count + 1
            #url = line[1]
            sendmail(user2,user0,user1)
            print(user0,user1,count)
          
            time.sleep(3)
  • 相关阅读:
    Could not load file or assembly 'System.Core, Version=2.0.5.0
    r 数据分组处理
    r函数知识总结
    R-Sys.time计算程序运行时间
    R语言包_dplyr_1
    R语言系列:生成数据
    R语言-分组统计
    导数——平均变化率与瞬时变化率
    梯度下降法
    梯度
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/5817874.html
Copyright © 2020-2023  润新知