0

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()
Bhavesh R
  • 23
  • 5
  • Edit - Okay seems you can generate the file, you need to be able to email it. Is it by chance on a Microsoft Server running IIS ? Cause there might be some easy ASP Classic code you use, since it has a built in CDOSYS through the web client directly. I'm not familiar enough with Python to know if it has a built in system to handle emails or if you will have to build one from scratch, that would be unfortunate. – easleyfixed Oct 25 '22 at 18:17
  • Got an solution , updated answer is displayed above – Bhavesh R Oct 25 '22 at 19:37

1 Answers1

0

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..."""
kgrvamsi
  • 31
  • 5
  • Found a soln , this is bit complex solution . Thank you – Bhavesh R Oct 25 '22 at 19:24
  • The email templating will help you reduce the hard coding of the strings in the function as per your solution and can have the flexibility to update the template any way you want or have multiple templates as needed in the future. – kgrvamsi Oct 25 '22 at 20:01