79426524

Date: 2025-02-10 08:58:36
Score: 3.5
Natty:
Report link

Yes, it was not clear I attach the entire code that it works until you are not embedding an html file.

from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

import pandas as pd
import sqlalchemy
import smtplib

from jinja2 import Template

# Read the Jinja2 email template
with open("C:/tmp/plotly_graph.html", "r") as file:
    template_str = file.read()

jinja_template = Template(template_str)

import glob
folder_dir="C:\\Users\\n.restaino\\PycharmProjects\\pythonProject\\.venv\\"
image_path="C:\\Users\\n.restaino\\PycharmProjects\\pythonProject\\.venv\\image1.png"
if __name__ == "__main__":
    # Connection details
    # Connection details
    user = 'a'
    pw = 'a'
    host = '0.0.0.10'
    port = '1521'
    db = 'a.a.com'


    engine = sqlalchemy.create_engine('oracle+cx_oracle://' + user + ':' + pw + '@' + host + ':' + port + '/?service_name=' + db)
    my_query="""SELECT sum(FTR_VALORE) val, AN_RAGSOC1 cli FROM fatrig JOIN ancf ON cf_cod=ftr_clfo
                WHERE ftr_ese='2025'
                GROUP BY AN_RAGSOC1 
                ORDER BY sum(ftr_valore) desc
                FETCH FIRST 20 ROWS ONLY"""
    df = pd.read_sql(my_query, engine)
    ax = df.plot.bar(x='cli', y='val', rot=60, figsize=(30, 20))

    fig = ax.get_figure()

    fig.savefig(image_path)



email_user = '[email protected]'
password_user = '1111'
email_send = '[email protected]'
subject = 'Python'

msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject

email_content = jinja_template.render(msg)
msg.attach(MIMEText(email_content, "html"))


body = """<h1> Sales Report </h1> {df.to_html()}
                    <img src="cid:image1">
                    {% include "C:/tmp/plotly_graph.html" %}
        """
msg.attach(MIMEText(body,'html'))

msgRoot = MIMEMultipart('mixed')
msgAlternative = MIMEMultipart('mixed')
i=1



# iterate over files in
# that directory
for images in glob.iglob(f'{folder_dir}/*'):

    # check if the image ends with png
    if (images.endswith(".png")):
        attachment =open(images,'rb')
        part = MIMEBase('application','octet-stream')
        part.set_payload((attachment).read())
        encoders.encode_base64(part)

        part.add_header('Content-Disposition',"attachment; filename= "+images)
        part.add_header('Content-ID', 'image1' )
        i = i + 1
        msg.attach(part)
        part = MIMEBase('application','octet-stream')
        part.set_payload((attachment).read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition',"attachment; filename= "+images)
        msg.attach(part)



text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user, password_user)

server.sendmail(email_user,email_send,text)
server.quit()

Let me know if I can add other details

Thanks in advance

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • RegEx Blacklisted phrase (3): Thanks in advance
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Nixi80