Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Last active September 25, 2024 15:04
Show Gist options
  • Save perfecto25/4b79b960eb58dc1f6025b56394b51cc1 to your computer and use it in GitHub Desktop.
Save perfecto25/4b79b960eb58dc1f6025b56394b51cc1 to your computer and use it in GitHub Desktop.

Revisions

  1. perfecto25 revised this gist May 11, 2021. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions default.j2
    Original file line number Diff line number Diff line change
    @@ -21,8 +21,8 @@ a:hover { color: #B40057; background-color:#C4FFF9; text-decoration: underline }
    <div class="body">
    Hello Marvel heroes, lets have a beat down at Metropolis. DO NOT bring the following items!!

    {{ vars['item1'] }} - this causes Superman to puke<br>
    {{ vars['item2'] }} - this causes Green Lantern to be useless
    {{ item1 }} - this causes Superman to puke<br>
    {{ item2 }} - this causes Green Lantern to be useless

    <p>
    </div>
    2 changes: 1 addition & 1 deletion send_email.py
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ def send_email(to, sender='MyCompanyName<[email protected]>', cc=None, bcc=N
    item2 = 'green clothing'

    # generate HTML from template
    html = render_template('default.j2', vars=locals())
    html = render_template('default.j2', **locals())

    to_list = ['[email protected]', '[email protected]']
    sender = 'superman<[email protected]>'
  2. perfecto25 revised this gist Nov 22, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion send_email.py
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ def send_email(to, sender='MyCompanyName<[email protected]>', cc=None, bcc=N
    from email.utils import formataddr

    # convert TO into list if string
    if not type(to) == 'list':
    if type(to) is not list:
    to = to.split()

    to_list = to + [cc] + [bcc]
  3. perfecto25 revised this gist Nov 22, 2017. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion send_email.py
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,11 @@ def send_email(to, sender='MyCompanyName<[email protected]>', cc=None, bcc=N
    from email.mime.text import MIMEText
    from email.header import Header
    from email.utils import formataddr


    # convert TO into list if string
    if not type(to) == 'list':
    to = to.split()

    to_list = to + [cc] + [bcc]
    to_list = filter(None, to_list) # remove null emails

  4. perfecto25 revised this gist Nov 22, 2017. 1 changed file with 15 additions and 12 deletions.
    27 changes: 15 additions & 12 deletions send_email.py
    Original file line number Diff line number Diff line change
    @@ -39,18 +39,21 @@ def send_email(to, sender='MyCompanyName<[email protected]>', cc=None, bcc=N
    log.exception(str(e))
    finally:
    server.quit()

    item1 = 'kryptonite'
    item2 = 'green clothing'
    #------------------------------------------------------------------------------------------------

    # MAIN

    item1 = 'kryptonite'
    item2 = 'green clothing'

    # generate HTML from template
    html = render_template('default.j2', vars=locals())
    # generate HTML from template
    html = render_template('default.j2', vars=locals())

    to_list = ['[email protected]', '[email protected]']
    sender = 'superman<[email protected]>'
    cc = '[email protected]'
    bcc = '[email protected]'
    subject = 'Meet me for a beatdown'
    to_list = ['[email protected]', '[email protected]']
    sender = 'superman<[email protected]>'
    cc = '[email protected]'
    bcc = '[email protected]'
    subject = 'Meet me for a beatdown'

    # send email to a list of email addresses
    send_email(to_list, sender, cc, bcc, subject, html.encode("utf8"))
    # send email to a list of email addresses
    send_email(to_list, sender, cc, bcc, subject, html.encode("utf8"))
  5. perfecto25 created this gist Nov 22, 2017.
    31 changes: 31 additions & 0 deletions default.j2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    <style type="text/css">
    @font-face {
    font-family: 'Open Sans';
    font-style: normal;
    font-weight: 300;
    src: local('Open Sans Light'), local('OpenSans-Light'), url(http://fonts.gstatic.com/s/opensans/v13/DXI1ORHCpsQm3Vp6mXoaTa-j2U0lmluP9RWlSytm3ho.woff2) format('woff2');
    unicode-range: U+0460-052F, U+20B4, U+2DE0-2DFF, U+A640-A69F;
    }
    .body {
    width: 90%;
    margin: auto;
    font-family: 'Open Sans', 'Helvetica', sans-serif;
    font-size: 14pt;
    color: #075D72;
    }
    a:link { color: #B40057; text-decoration: underline}
    a:visited { color: #542A95; text-decoration: none}
    a:hover { color: #B40057; background-color:#C4FFF9; text-decoration: underline }
    </style>

    <div class="body">
    Hello Marvel heroes, lets have a beat down at Metropolis. DO NOT bring the following items!!

    {{ vars['item1'] }} - this causes Superman to puke<br>
    {{ vars['item2'] }} - this causes Green Lantern to be useless

    <p>
    </div>

    <p><br><br>

    56 changes: 56 additions & 0 deletions send_email.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    def render_template(template, **kwargs):
    ''' renders a Jinja template into HTML '''
    # check if template exists
    if not os.path.exists(template):
    print('No template file present: %s' % template)
    sys.exit()

    import jinja2
    templateLoader = jinja2.FileSystemLoader(searchpath="/")
    templateEnv = jinja2.Environment(loader=templateLoader)
    templ = templateEnv.get_template(template)
    return templ.render(**kwargs)
    #------------------------------------------------------------------------------------------------
    def send_email(to, sender='MyCompanyName<[email protected]>', cc=None, bcc=None, subject=None, body=None):
    ''' sends email using a Jinja HTML template '''
    import smtplib
    # Import the email modules
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    from email.header import Header
    from email.utils import formataddr

    to_list = to + [cc] + [bcc]
    to_list = filter(None, to_list) # remove null emails

    msg = MIMEMultipart('alternative')
    msg['From'] = sender
    msg['Subject'] = subject
    msg['To'] = ','.join(to)
    msg['Cc'] = cc
    msg['Bcc'] = bcc
    msg.attach(MIMEText(body, 'html'))
    server = smtplib.SMTP("127.0.0.1") # or your smtp server
    try:
    log.info('sending email xxx')
    server.sendmail(sender, to_list, msg.as_string())
    except Exception as e:
    log.error('Error sending email')
    log.exception(str(e))
    finally:
    server.quit()

    item1 = 'kryptonite'
    item2 = 'green clothing'

    # generate HTML from template
    html = render_template('default.j2', vars=locals())

    to_list = ['[email protected]', '[email protected]']
    sender = 'superman<[email protected]>'
    cc = '[email protected]'
    bcc = '[email protected]'
    subject = 'Meet me for a beatdown'

    # send email to a list of email addresses
    send_email(to_list, sender, cc, bcc, subject, html.encode("utf8"))