Skip to content

Instantly share code, notes, and snippets.

@bryhal
Created November 4, 2012 01:04
Show Gist options
  • Select an option

  • Save bryhal/4009671 to your computer and use it in GitHub Desktop.

Select an option

Save bryhal/4009671 to your computer and use it in GitHub Desktop.

Revisions

  1. bryhal revised this gist Nov 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion send_email2.py
    Original file line number Diff line number Diff line change
    @@ -93,7 +93,7 @@ def strip_tags(html):

    # The actual email sendy bits
    server = smtplib.SMTP(host)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.set_debuglevel(False) # set to True for verbose output
    try:
    # gmail expect tls
    server.starttls()
  2. bryhal revised this gist Nov 4, 2012. 1 changed file with 24 additions and 12 deletions.
    36 changes: 24 additions & 12 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -11,16 +11,21 @@
    password = 'j0ej0e'
    host = 'smtp.gmail.com:587' # specify port, if required, using this notations

    fromaddr = 'joe@gmail.com' # must be a vaild 'from' address in your GApps account
    toaddr = 'joe@rogers.com'
    fromaddr = 'joe@rogers.com' # must be a vaild 'from' address in your GApps account
    toaddr = 'joe@gmail.com'
    replyto = fromaddr # unless you want a different reply-to

    msgsubject = 'This is the subject of the email! Yay!'
    msgsubject = 'This is the subject of the email! WooHoo!'

    htmlmsgtext = """<h1>This is my message body in HTML...WOW!!!!!</h1>
    <p>Hey, Hey, Ho, Ho, got a paragraph here. A lovely paragraph it is.\
    htmlmsgtext = """<h2>This is my message body in HTML...WOW!!!!!</h2>
    <p>\
    Hey, Hey, Ho, Ho, got a paragraph here. A lovely paragraph it is.\
    You've never seen a better paragraph than this.\
    I make some of the best paragraphs you have ever seen.</p>
    I make some of the best paragraphs you have ever seen.\
    Hey, Hey, Ho, Ho, got a paragraph here. A lovely paragraph it is.\
    You've never seen a better paragraph than this.\
    I make some of the best paragraphs you have ever seen.\
    </p>
    <ul>
    <li>This is a list item</li>
    <li>This is another list item</li>
    @@ -89,12 +94,19 @@ def strip_tags(html):
    # The actual email sendy bits
    server = smtplib.SMTP(host)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())

    server.quit() #bye bye
    print 'Email sent'
    try:
    # gmail expect tls
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())
    print 'Email sent'
    server.quit() # bye bye
    except:
    # if tls is set for non-tls servers you would have raised an exception, so....
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())
    print 'Email sent'
    server.quit() # sbye bye
    except:
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case
  3. bryhal revised this gist Nov 4, 2012. 1 changed file with 52 additions and 54 deletions.
    106 changes: 52 additions & 54 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,31 @@
    # Found most ofthis at http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/
    # Adapted to accept a list of files for multiple file attachments
    # From other stuff I googled, a little more elegant way of converting html to plain text
    # This works and my brain gets it. In my application, there will always be at least 1 attachment - you might want to test for that
    # This works in 2.7 and my brain gets it.

    ######### Setup your stuff here #######################################

    attachments = ['test_pdf.pdf', 'test_waiver.pdf']

    username = '[email protected]'
    password = 'j0ej0e'
    host = 'smtp.gmail.com:587' # specify port if required using this notation
    host = 'smtp.gmail.com:587' # specify port, if required, using this notations

    fromaddr = '[email protected]' #must be a vaild 'from' addy in your GApps account
    fromaddr = '[email protected]' # must be a vaild 'from' address in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to
    replyto = fromaddr # unless you want a different reply-to

    msgsubject = 'This is the subject of the email! Yay!'

    htmlmsgtext = """<h1>This is my message body in HTML...WOW!!!!!</h1>
    <p>Yup, Yup, Yup, got a paragraph here. A lovely paragraph it is. You've never seen a better paragraph than this.</p>
    <p>Hey, Hey, Ho, Ho, got a paragraph here. A lovely paragraph it is.\
    You've never seen a better paragraph than this.\
    I make some of the best paragraphs you have ever seen.</p>
    <ul>
    <li>This is a list item</li>
    <li>This is another list item</li>
    <li>And yet another list item, pretty big list</li>
    <li>OMG this is a long list!</li>
    </ul>
    <p><strong>Here are your attachments:</strong></p><br />"""

    @@ -51,52 +56,45 @@ def strip_tags(html):

    ########################################################################

    def main():

    try:
    # Make text version from HTML - First convert tags that produce a line break to carriage returns
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)

    # necessary mimey stuff
    msg = MIMEMultipart()
    msg.preamble = 'This is a multi-part message in MIME format.\n'
    msg.epilogue = ''

    body = MIMEMultipart('alternative')
    body.attach(MIMEText(msgtext))
    body.attach(MIMEText(htmlmsgtext, 'html'))
    msg.attach(body)

    if 'attachments' in globals() and len('attachments') > 0: # are there attachments?
    for filename in attachments:
    f = filename
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

    msg.add_header('From', fromaddr)
    msg.add_header('To', toaddr)
    msg.add_header('Subject', msgsubject)
    msg.add_header('Reply-To', replyto)

    # The actual email sendy bits
    server = smtplib.SMTP(host)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())

    server.quit() #bye bye
    print 'Email sent'
    except:
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case

    #######################################################################

    if __name__ == '__main__':
    main()
    try:
    # Make text version from HTML - First convert tags that produce a line break to carriage returns
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)

    # necessary mimey stuff
    msg = MIMEMultipart()
    msg.preamble = 'This is a multi-part message in MIME format.\n'
    msg.epilogue = ''

    body = MIMEMultipart('alternative')
    body.attach(MIMEText(msgtext))
    body.attach(MIMEText(htmlmsgtext, 'html'))
    msg.attach(body)

    if 'attachments' in globals() and len('attachments') > 0: # are there attachments?
    for filename in attachments:
    f = filename
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

    msg.add_header('From', fromaddr)
    msg.add_header('To', toaddr)
    msg.add_header('Subject', msgsubject)
    msg.add_header('Reply-To', replyto)

    # The actual email sendy bits
    server = smtplib.SMTP(host)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())

    server.quit() #bye bye
    print 'Email sent'
    except:
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case
  4. bryhal revised this gist Nov 4, 2012. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@
    </ul>
    <p><strong>Here are your attachments:</strong></p><br />"""

    ############### In normal use nothing changes below this line ######################
    ######### In normal use nothing changes below this line ###############

    import smtplib, os, sys
    from email.MIMEMultipart import MIMEMultipart
    @@ -33,6 +33,8 @@
    from email import Encoders
    from HTMLParser import HTMLParser

    # A snippet - class to strip HTML tags for the text version of the email

    class MLStripper(HTMLParser):
    def __init__(self):
    self.reset()
    @@ -47,6 +49,8 @@ def strip_tags(html):
    s.feed(html)
    return s.get_data()

    ########################################################################

    def main():

    try:
    @@ -55,7 +59,7 @@ def main():
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)

    #pain the ass mimey stuff
    # necessary mimey stuff
    msg = MIMEMultipart()
    msg.preamble = 'This is a multi-part message in MIME format.\n'
    msg.epilogue = ''
    @@ -65,7 +69,7 @@ def main():
    body.attach(MIMEText(htmlmsgtext, 'html'))
    msg.attach(body)

    if 'attachments' in globals(): # should be testing if len('attachments') > 0, oh well....
    if 'attachments' in globals() and len('attachments') > 0: # are there attachments?
    for filename in attachments:
    f = filename
    part = MIMEBase('application', "octet-stream")
    @@ -92,5 +96,7 @@ def main():
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case

    #######################################################################

    if __name__ == '__main__':
    main()
  5. bryhal revised this gist Nov 4, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion send_email2.py
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,6 @@

    ############### In normal use nothing changes below this line ######################

    # import smtplib, os, re, sys, glob, string, datetime
    import smtplib, os, sys
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
  6. bryhal revised this gist Nov 4, 2012. 1 changed file with 26 additions and 25 deletions.
    51 changes: 26 additions & 25 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,31 @@
    # From other stuff I googled, a little more elegant way of converting html to plain text
    # This works and my brain gets it. In my application, there will always be at least 1 attachment - you might want to test for that

    import smtplib, os, re, sys, glob, string, datetime
    attachments = ['test_pdf.pdf', 'test_waiver.pdf']

    username = '[email protected]'
    password = 'j0ej0e'
    host = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = '[email protected]' #must be a vaild 'from' addy in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is the subject of the email! Yay!'

    htmlmsgtext = """<h1>This is my message body in HTML...WOW!!!!!</h1>
    <p>Yup, Yup, Yup, got a paragraph here. A lovely paragraph it is. You've never seen a better paragraph than this.</p>
    <ul>
    <li>This is a list item</li>
    <li>This is another list item</li>
    <li>And yet another list item, pretty big list</li>
    </ul>
    <p><strong>Here are your attachments:</strong></p><br />"""

    ############### In normal use nothing changes below this line ######################

    # import smtplib, os, re, sys, glob, string, datetime
    import smtplib, os, sys
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    @@ -24,31 +48,8 @@ def strip_tags(html):
    s.feed(html)
    return s.get_data()

    attachments = ['test_pdf.pdf', 'test_waiver.pdf']

    def main():


    username = '[email protected]'
    password = 'j0ej0e'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = '[email protected]' #must be a vaild 'from' addy in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is for the email subject!'

    htmlmsgtext = """<h1>This is my message body in HTML</h1>
    <p>Yup, Yup, Yup, got a paragraph here. A lovely paragraph it is.</p>
    <ul>
    <li>This is a list item</li>
    <li>This is another list item</li>
    <li>And yet another list item, pretty big list</li>
    </ul>
    <p><strong>Here are your attachments:</strong></p><br />"""

    ############### In normal use nothing changes below this line ######################
    try:
    # Make text version from HTML - First convert tags that produce a line break to carriage returns
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    @@ -80,7 +81,7 @@ def main():
    msg.add_header('Reply-To', replyto)

    # The actual email sendy bits
    server = smtplib.SMTP(server)
    server = smtplib.SMTP(host)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
  7. bryhal revised this gist Nov 4, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -29,12 +29,12 @@ def strip_tags(html):
    def main():


    username = 'bryhal@gmail.com'
    password = 'fg23io98'
    username = 'joe@gmail.com'
    password = 'j0ej0e'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = 'bryhal@gmail.com' #must be a vaild 'from' addy in your GApps account
    toaddr = 'bryhal@rogers.com'
    fromaddr = 'joel@gmail.com' #must be a vaild 'from' addy in your GApps account
    toaddr = 'joe@rogers.com'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is for the email subject!'
  8. bryhal revised this gist Nov 4, 2012. 1 changed file with 73 additions and 62 deletions.
    135 changes: 73 additions & 62 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,8 @@
    # Found most ofthis at http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/
    # Adapted to accept a list of files for multiple file attachments
    # From other stuff I googled, a little more elegant way of converting html to plain text
    # This works and my brain gets it. In my application, there will always be at least 1 attachment - you might want to test for that

    import smtplib, os, re, sys, glob, string, datetime
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    @@ -19,67 +24,73 @@ def strip_tags(html):
    s.feed(html)
    return s.get_data()

    # Found this at http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/
    # Adapted to accept a list of files for multiple file attachments
    # From other stuff I googled, a little more elegent way of converting html to plain text
    # This works and my brain gets it. In my application, there will always be at least 1 attachment - you might want to test for that

    attachments = ['test_pdf.pdf', 'test_waiver.pdf']

    username = '[email protected]'
    password = 'j0ej0e'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = '[email protected]' # for gmail, must be a valid 'from' address in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is for the email subject!'

    htmlmsgtext = """<h1>This is my message body in HTML</h1>
    <p>Yup, Yup, Yup, got a paragraph here. A lovely paragraph it is.</p>
    <p><strong>Here are your attachments:</strong></p><br />"""

    ############### In normal use nothing changes below this line ######################
    try:
    # Make text version from HTML - First convert tags that produce a line break to carriage returns
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)

    #pain the ass mimey stuff
    msg = MIMEMultipart()
    msg.preamble = 'This is a multi-part message in MIME format.\n'
    msg.epilogue = ''

    body = MIMEMultipart('alternative')
    body.attach(MIMEText(msgtext))
    body.attach(MIMEText(htmlmsgtext, 'html'))
    msg.attach(body)

    if 'attachments' in globals(): # should be testing if len('attachments') > 0, oh well....
    for filename in attachments:
    f = filename
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

    msg.add_header('From', fromaddr)
    msg.add_header('To', toaddr)
    msg.add_header('Subject', msgsubject)
    msg.add_header('Reply-To', replyto)

    # The actual email sendy bits
    server = smtplib.SMTP(server)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())

    server.quit() #bye bye
    print 'Email sent'
    except:
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case
    def main():


    username = '[email protected]'
    password = 'fg23io98'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = '[email protected]' #must be a vaild 'from' addy in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is for the email subject!'

    htmlmsgtext = """<h1>This is my message body in HTML</h1>
    <p>Yup, Yup, Yup, got a paragraph here. A lovely paragraph it is.</p>
    <ul>
    <li>This is a list item</li>
    <li>This is another list item</li>
    <li>And yet another list item, pretty big list</li>
    </ul>
    <p><strong>Here are your attachments:</strong></p><br />"""

    ############### In normal use nothing changes below this line ######################
    try:
    # Make text version from HTML - First convert tags that produce a line break to carriage returns
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)

    #pain the ass mimey stuff
    msg = MIMEMultipart()
    msg.preamble = 'This is a multi-part message in MIME format.\n'
    msg.epilogue = ''

    body = MIMEMultipart('alternative')
    body.attach(MIMEText(msgtext))
    body.attach(MIMEText(htmlmsgtext, 'html'))
    msg.attach(body)

    if 'attachments' in globals(): # should be testing if len('attachments') > 0, oh well....
    for filename in attachments:
    f = filename
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

    msg.add_header('From', fromaddr)
    msg.add_header('To', toaddr)
    msg.add_header('Subject', msgsubject)
    msg.add_header('Reply-To', replyto)

    # The actual email sendy bits
    server = smtplib.SMTP(server)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())

    server.quit() #bye bye
    print 'Email sent'
    except:
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case

    if __name__ == '__main__':
    main()
  9. bryhal revised this gist Nov 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion send_email2.py
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ def strip_tags(html):
    # From other stuff I googled, a little more elegent way of converting html to plain text
    # This works and my brain gets it. In my application, there will always be at least 1 attachment - you might want to test for that

    attachments = ['test_pdf.pdf', 'test_waiver.pdf']a
    attachments = ['test_pdf.pdf', 'test_waiver.pdf']

    username = '[email protected]'
    password = 'j0ej0e'
  10. bryhal revised this gist Nov 4, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion send_email2.py
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,6 @@ def strip_tags(html):
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)
    # msgtext = re.sub('<.*?>','',msgtext)

    #pain the ass mimey stuff
    msg = MIMEMultipart()
  11. bryhal revised this gist Nov 4, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion send_email2.py
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ def strip_tags(html):
    password = 'j0ej0e'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = '[email protected]' #must be a vaild 'from' addy in your GApps account
    fromaddr = '[email protected]' # for gmail, must be a valid 'from' address in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to

  12. bryhal revised this gist Nov 4, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -26,12 +26,12 @@ def strip_tags(html):

    attachments = ['test_pdf.pdf', 'test_waiver.pdf']a

    username = 'bryhal@gmail.com'
    password = 'fg23io98'
    username = 'joe@gmail.com'
    password = 'j0ej0e'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = 'bryhal@gmail.com' #must be a vaild 'from' addy in your GApps account
    toaddr = 'bryhal@rogers.com'
    fromaddr = 'joe@gmail.com' #must be a vaild 'from' addy in your GApps account
    toaddr = 'bob@rogers.com'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is for the email subject!'
  13. bryhal created this gist Nov 4, 2012.
    86 changes: 86 additions & 0 deletions send_email2.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    import smtplib, os, re, sys, glob, string, datetime
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email import Encoders
    from HTMLParser import HTMLParser

    class MLStripper(HTMLParser):
    def __init__(self):
    self.reset()
    self.fed = []
    def handle_data(self, d):
    self.fed.append(d)
    def get_data(self):
    return ''.join(self.fed)

    def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

    # Found this at http://ryrobes.com/python/python-snippet-sending-html-email-with-an-attachment-via-google-apps-smtp-or-gmail/
    # Adapted to accept a list of files for multiple file attachments
    # From other stuff I googled, a little more elegent way of converting html to plain text
    # This works and my brain gets it. In my application, there will always be at least 1 attachment - you might want to test for that

    attachments = ['test_pdf.pdf', 'test_waiver.pdf']a

    username = '[email protected]'
    password = 'fg23io98'
    server = 'smtp.gmail.com:587' # specify port if required using this notation

    fromaddr = '[email protected]' #must be a vaild 'from' addy in your GApps account
    toaddr = '[email protected]'
    replyto = fromaddr #unless you want a different reply-to

    msgsubject = 'This is for the email subject!'

    htmlmsgtext = """<h1>This is my message body in HTML</h1>
    <p>Yup, Yup, Yup, got a paragraph here. A lovely paragraph it is.</p>
    <p><strong>Here are your attachments:</strong></p><br />"""

    ############### In normal use nothing changes below this line ######################
    try:
    # Make text version from HTML - First convert tags that produce a line break to carriage returns
    msgtext = htmlmsgtext.replace('</br>',"\r").replace('<br />',"\r").replace('</p>',"\r")
    # Then strip all the other tags out
    msgtext = strip_tags(msgtext)
    # msgtext = re.sub('<.*?>','',msgtext)

    #pain the ass mimey stuff
    msg = MIMEMultipart()
    msg.preamble = 'This is a multi-part message in MIME format.\n'
    msg.epilogue = ''

    body = MIMEMultipart('alternative')
    body.attach(MIMEText(msgtext))
    body.attach(MIMEText(htmlmsgtext, 'html'))
    msg.attach(body)

    if 'attachments' in globals(): # should be testing if len('attachments') > 0, oh well....
    for filename in attachments:
    f = filename
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(f,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
    msg.attach(part)

    msg.add_header('From', fromaddr)
    msg.add_header('To', toaddr)
    msg.add_header('Subject', msgsubject)
    msg.add_header('Reply-To', replyto)

    # The actual email sendy bits
    server = smtplib.SMTP(server)
    # server.set_debuglevel(True) #commenting this out, changing to False will make the script give NO output at all upon successful completion
    server.starttls()
    server.login(username,password)
    server.sendmail(msg['From'], [msg['To']], msg.as_string())

    server.quit() #bye bye
    print 'Email sent'
    except:
    print ('Email NOT sent to %s successfully. %s ERR: %s %s %s ', str(toaddr), 'tete', str(sys.exc_info()[0]), str(sys.exc_info()[1]), str(sys.exc_info()[2]) )
    #just in case