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

sending emails in a loop In PYTHON 2.7

我目前正在尝试制作一个循环向自己发送多封电子邮件的程序。我已经编写了 2 个代码补丁,但我似乎无法让它们工作。 (我 运行 这个在 raspberry pi 上所以请原谅任何奇怪的目录)。

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

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

这将打开电子邮件 "sending" 部分 /\ .

下面是 "sending" 部分 /

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 ()
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 消息的方法。但这不是必需的,因此您也可以发送简单的短信。

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