How to send prettytable through email using python

605 Views Asked by At

I have created a prettytable in python and I have to send the output of prettytable through email

env = "Dev"
cost = 25.3698
line = [env, "${:,.2f}".format(cost)]
totalcostofenv = PrettyTable(['Environment', 'Cost'])
totalcostofenv.add_row(line)

print(totalcostofenv)

Below attached is the output : Table Output

Can anyone help me to solve this?

This was my question asked and I found an solution , Below displayed is my code:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import os
from prettytable import PrettyTable

env = "Dev"
cost = 25.3698
line = [env, "${:,.2f}".format(cost)]
totalcostofenv = PrettyTable(['Environment', 'Cost'])
totalcostofenv.add_row(line)

print(totalcostofenv)

print(totalcostofenv.get_html_string())

def trigger_email():
    my_message = totalcostofenv.get_html_string()
    text = "Hi!"
    html = """\
    <html>
        <head>
        <style>
            table, th, td {
                border: 1px solid black;
                border-collapse: collapse;
            }
            th, td {
                padding: 5px;
                text-align: left;    
            }    
        </style>
        </head>
    <body>
    <p>Cost Usage of Plantd Environemnts<br>
       %s
    </p>
    </body>
    </html>
    """ % (my_message)

    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')

    msg = MIMEMultipart()
    from_addr = "from-address"
    mail_password = os.environ.get('gmail-pass')
    to_addr = "to-address"
    msg.attach(part1)
    msg.attach(part2)
    

    try:

        smtp = smtplib.SMTP('smtp.gmail.com',587)
        smtp.starttls()
        smtp.login(from_addr , mail_password) 
        smtp.sendmail(from_addr , to_addr , msg.as_string())
        print('Mail sent')

    except:
        print('Mail not sent')

trigger_email()
1

There are 1 best solutions below

2
kgrvamsi On

You can use MJML templating like this

<mjml>
  <mj-head>
    <mj-title>Set the title, usually for accessibility tools</mj-title>
    <mj-preview>Set inbox preview text here, otherwise it might be something nonsensical</mj-preview>
    <mj-attributes>
      <mj-all font-family="Helvetica, Arial, sans-serif"></mj-all>
      <mj-text font-weight="400" font-size="16px" color="#4A4A4A" line-height="24px" />
      <mj-section padding="0px"></mj-section>
    </mj-attributes>
  </mj-head>
  <mj-body>
     {{table}}
  </mj-body>
</mjml>

Code:

import pystache

# read in the email template, remember to use the compiled HTML version!
email_template = (Path() / 'email_template.html').read_text()

# Logic
env = "Dev"
cost = 25.3698
line = [env, "${:,.2f}".format(cost)]
totalcostofenv = PrettyTable(['Environment', 'Cost'])
totalcostofenv.add_row(line)

# Pass in values for the template using a dictionary
template_params = {'table': totalcostofenv }

# Attach the message to the Multipart Email
final_email_html = pystache.render(email_template, template_params)
message.attach(MIMEText(final_email_html), 'html')

"""Continue with sending..."""