代码之家  ›  专栏  ›  技术社区  ›  TylerTotally

在PYTHON 2.7中循环发送电子邮件

  •  1
  • TylerTotally  · 技术社区  · 10 年前

    我目前正在尝试制作一个程序,循环发送多封电子邮件给我自己。我已经写了2个代码补丁,但我似乎无法让它们工作。(我是在树莓皮上运行这个,所以排除任何奇怪的导演)。

    这是我的第一个while循环代码补丁

    import os
    i = 0
    while i < 2:
        os.pause(4)
        os.system("home/Tyler/desktop/test.py")
        i += 1
    

    这将打开电子邮件“发送”部分/\。

    下面是“发送”部分/

    import smtplib
    
    smtpUser = 'smilingexample@gmail.com'
    smtpPass = 'password'
    
    toAdd =  'Example@aim.com'
    fromAdd = smtpUser
    
    subject = 'yep'
    header = 'to: ' + toAdd + '\n' + 'From: ' + fromAdd + '\n' + 'Subject: ' + subject
    body = 'hi'
    
    print header + '\n' + body
    
    s = smtplib.SMTP('smtp.gmail.com',587)
    
    s.ehlo()
    s.starttls()
    s.ehlo()
    
    
    s.login(smtpUser, smtpPass)
    s.sendmail(fromAdd, toAdd, header + '\n\n' + body)
    
    s.quit ()
    
    1 回复  |  直到 10 年前
        1
  •  3
  •   Ajay    10 年前
    import datetime
    import smtplib
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.header import Header
    from email import Charset
    Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')
    
    maillist = []
    
    def send_email(messages_list, smtpUser=None, smtpPass=None, tls=False):
        failed = []
        try:
            s = smtplib.SMTP('smtp.gmail.com',587)
            s.ehlo()
            if tls:
                s.starttls()
                s.ehlo()
            if smtpUser and smtpPass:
                s.login(smtpUser, smtpPass)
        except:
            print "ehlo failed"
            failed = [x[0] for x in messages_list]
        else:
            for to_address,from_address,subject,encoding,mesg in messages_list:
                try:
                    if len(mesg) == 2:
                        msg = MIMEMultipart('alternative')
                    else:
                        msg = MIMEText(mesg[0],'plain','utf-8')
                    msg['Subject'] = "%s" % Header(subject, 'utf-8')
                    if len(from_address) == 2:
                        msg['From'] = "\"%s\" <%s>" % (Header(from_address[0], 'utf-8'), from_address[-1])
                    else:
                        msg['From'] = from_address[-1]
                    if len(to_address) == 2:
                        msg['From'] = "\"%s\" <%s>" % (Header(to_address[0], 'utf-8'), to_address[-1])
                    else:
                        msg['To'] = to_address[-1]
                    msg.set_charset("utf-8")
                    if len(mesg) == 2:
                        part1 = MIMEText(mesg[0], 'plain','utf-8')
                        part2 = MIMEText(mesg[1], 'html','utf-8')
                        msg.attach(part1)
                        msg.attach(part2)
                    s.sendmail(from_address[-1], to_address[-1], msg.as_string())
                except:
                    traceback.print_exc()
                    failed.append(to_address[-1])
        try:
            s.quit()
        except:
            pass
        return failed
    
    maillist.append(( ['someone@gmail.com'],["Me","noreply@example.com"],'Subject','utf-8',['text_message','html but you can delete this list element'] ))
    for k in send_email(maillist, smtpUser='you@gmail.com', smtpPass='pwd', tls=True):
        print k, 'not delivered'
    

    下面是我们用来发送带有替代正文的替代Mime消息的方法。但这不是必须的,所以您也可以发送简单的短信。

    它已准备好从本地主机发送,但您可以轻松修改它以正确使用它。