Search code examples
pythonpython-2.7emailwhile-loopraspberry-pi2

sending emails in a loop In PYTHON 2.7


I am currently trying to make a program that send multiple emails to my self in a loop. I have already written 2 patches of code but i can not seem to get them to work. (I am running this on a raspberry pi so exsuse any weird directorys).

This is my first patch of while loop code

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

This opens the email "sending" part /\ .

This down here is the "sending" part /

import smtplib

smtpUser = '[email protected]'
smtpPass = 'password'

toAdd =  '[email protected]'
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 ()

Solution

  • 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(( ['[email protected]'],["Me","[email protected]"],'Subject','utf-8',['text_message','html but you can delete this list element'] ))
    for k in send_email(maillist, smtpUser='[email protected]', smtpPass='pwd', tls=True):
        print k, 'not delivered'
    

    Here's what we use to send alternative Mime messages with alternative body. It is not necessary though so you can send simple text messages as well.

    It's prepared to send from localhost but you can easily modify it to use it properly.