Untitled

 avatar
unknown
plain_text
a month ago
7.9 kB
2
Indexable
import pandas as pd
import smtplib
import tkinter as tk
import os
import threading
import queue
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from docx import Document
from tkinter import filedialog
from tkinter import messagebox
from dotenv import load_dotenv

# Function to read text from a Word document
def extract_text_from_word(docx_file_path):
    doc = Document(docx_file_path)
    extracted_text = ""

    for para in doc.paragraphs:
        para_text = para.text
        extracted_text += para_text + "\n"

    return extracted_text

# Function to convert a DataFrame to styled HTML
def dataframe_to_styled_html(df, merge_first_row=False):
    table_style = (
        "border-collapse: collapse; width: 100%; font-family: Arial, sans-serif; "
        "font-size: 14px; text-align: center; line-height: 1.5; border: 1px solid #ddd; font-weight: normal;"
    )
    header_style = (
        "background-color: #6A0DAD; color: white; padding: 10px; "
        "border: 1px solid #ddd; font-weight: bold; font-family: Arial, sans-serif; font-size: 14px;"
    )
    cell_style = (
        "padding: 10px; border: 1px solid #ddd; font-family: Arial, sans-serif; "
        "font-size: 14px; line-height: 1.5; color: black; font-weight: normal;"
    )
    row_alt_style = "background-color: #f9f9f9;"

    html_table = f'<table style="{table_style}">'

    if merge_first_row:
        html_table += f'<tr><td colspan="{len(df.columns)}" style="{header_style}">{df.columns[0]}</td></tr>'
    else:
        html_table += "<tr>"
        for col in df.columns:
            html_table += f'<th style="{header_style}">{col}</th>'
        html_table += "</tr>"

    for i, row in df.iterrows():
        row_style = row_alt_style if i % 2 == 1 else ""
        html_table += f'<tr style="{row_style}">'
        for cell in row:
            if pd.isna(cell):
                cell_content = ""
            else:
                cell_content = cell
            html_table += f'<td style="{cell_style}">{cell_content}</td>'
        html_table += "</tr>"

    html_table += "</table>"
    return html_table

# Updated email body HTML
email_body_html_template = """
<html>
    <body style="font-family: Arial, sans-serif; font-size: 14px; line-height: 1.5; color: black; margin: 0; padding: 0; font-weight: normal;">
        <table width="100%" style="border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.5; color: black; font-weight: normal;">
            <tr>
                <td align="center" style="padding: 20px; background-color: #f9f9f9;">
                    <table width="600px" style="border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.5; color: black; font-weight: normal;">
                        <!-- Header Section -->
                        <tr>
                            <td style="padding: 20px; text-align: center; font-size: 18px; font-weight: bold; color: #6A0DAD;">
                                Wealth Management Update
                            </td>
                        </tr>

                        <!-- Content Section -->
                        <tr>
                            <td style="padding: 20px; text-align: left; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.5; color: black; font-weight: normal;">
                                {{EMAIL_TEXT}}
                            </td>
                        </tr>

                        <!-- Table 1 -->
                        <tr>
                            <td style="padding: 20px;">
                                {{TABLE_1}}
                            </td>
                        </tr>

                        <!-- Table 2 -->
                        <tr>
                            <td style="padding: 20px;">
                                {{TABLE_2}}
                            </td>
                        </tr>

                        <!-- Footer Section -->
                        <tr>
                            <td style="padding: 20px; text-align: center; font-size: 12px; color: gray; font-weight: normal;">
                                This email is intended for informational purposes only. For further details, contact us at <a href="mailto:support@example.com" style="color: #6A0DAD; text-decoration: none; font-weight: normal;">support@example.com</a>.
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </body>
</html>
"""

# Function to send emails
def send_bulk_emails_with_text_and_tables(
    sender_email,
    sender_password,
    smtp_server,
    smtp_port,
    email_list_file,
    table1_file,
    table2_file,
    text_file,
    log_queue,
    log_file
):
    log_queue.put("The email-sending process has started. Please wait...")
    log_serial_number = 1

    with open(log_file, 'a') as log_file_obj:
        try:
            email_data = pd.read_excel(email_list_file)
            if "Email" not in email_data.columns or "Salutations" not in email_data.columns or "CC" not in email_data.columns:
                log_queue.put("The email list file must contain columns: 'Email', 'Salutations', and 'CC'.")
                return

            email_details = email_data[["Email", "Salutations", "CC"]]

            if text_file.endswith(".docx"):
                email_text = extract_text_from_word(text_file)
            else:
                log_queue.put("Unsupported file format. Use a .docx file for the text.")
                return

            table1_df = pd.read_excel(table1_file)
            table2_df = pd.read_excel(table2_file)

            table1_html = dataframe_to_styled_html(table1_df)
            table2_html = dataframe_to_styled_html(table2_df, merge_first_row=True)

            email_body_html = email_body_html_template.replace("{{EMAIL_TEXT}}", email_text).replace("{{TABLE_1}}", table1_html).replace("{{TABLE_2}}", table2_html)

            with smtplib.SMTP(smtp_server, smtp_port) as server:
                server.starttls()
                server.login(sender_email, sender_password)

                for _, row in email_details.iterrows():
                    try:
                        recipients = row["Email"].split(",")
                        salutation = row["Salutations"]
                        cc_list = row["CC"].split(",") if pd.notna(row["CC"]) else []

                        personalized_text = email_body_html.replace("{{Salutations}}", salutation)

                        msg = MIMEMultipart()
                        msg["From"] = sender_email
                        msg["To"] = ", ".join(recipients)
                        msg["Cc"] = ", ".join(cc_list)
                        msg["Subject"] = "Your Wealth Management Summary"

                        msg.attach(MIMEText(personalized_text, "html"))

                        all_recipients = recipients + cc_list
                        server.sendmail(sender_email, all_recipients, msg.as_string())

                        log_entry = f"{log_serial_number}. Email sent successfully to {', '.join(recipients)}, CC: {', '.join(cc_list)}"
                        log_queue.put(log_entry)
                        log_file_obj.write(log_entry + "\n")

                    except Exception as e:
                        log_entry = f"{log_serial_number}. Failed to send email to {', '.join(recipients)}: {e}"
                        log_queue.put(log_entry)
                        log_file_obj.write(log_entry + "\n")

                    log_serial_number += 1

        except Exception as e:
            log_entry = f"Failed to connect to the SMTP server: {e}"
            log_queue.put(log_entry)
            log_file_obj.write(log_entry + "\n")

    log_queue.put("The email-sending process has completed successfully.")

# Other functions and UI remain the same
Leave a Comment