import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from python_postgres import get_data_unknown
from datetime import datetime

# Set up email message
sender_email = 'scott_wright@currently.com'
bcc_email = 'swright@homestbk.com'

# Set up SMTP server connection
smtp_server = 'smtp.mail.att.net'
smtp_port = 587
smtp_username = 'scott_wright@currently.com'
smtp_password = 'jwbtbbqqwdkocrno'
smtp_connection = smtplib.SMTP(smtp_server, smtp_port)
smtp_connection.starttls()
smtp_connection.login(smtp_username, smtp_password)


data=get_data_unknown()
for item in data:
        receiver_email = item[3]
        name = item[2]
        date = item[1]
        today = datetime.today()
        todays_formatted_date = today.strftime("%m/%d").replace("0", "", 1).replace("/0", "/")
        #print(todays_formatted_date)
        #print(date)
        
        if date == todays_formatted_date:
        #Add the text content of the email
                message_template =f'''<html>
                                <body>
                                        <p>{name},</p>
                                        <p>I'm a local community banker (with all the bank mergers few of us remain), please let me know if there's any reason to set up a call, even for a quick introduction.  If you're looking for a second banking option, community banking relationship, or even investment property financing that your current bank may not provide, please don't hesitate to contact me.</p><br><br>
                                        
                                        <p>Regards,</p>
                                        <p>Scott Wright</p>
                                        <p>Vice President, Commercial Lending</p>
                                        <p>swright@homestbk.com or scott_wright@currently.com</p>
                                        <p>Home State Bank</p>
                                        <p>Crystal Lake, IL</p>
                                        <p>815-788-3475 office | 847-532-3886 cell/text</p>
                                        <p><img src="cid:image1"></p><br>
                                        <p>NOTICE: This electronic mail message and any files transmitted
                                        with it are intended exclusively for the individual or entity to
                                        which it is addressed. The message, together with any attachment, may
                                        contain confidential and/or privileged information. Any unauthorized
                                        review, use, printing, saving, copying, disclosure or distribution is strictly prohibited.
                                        If you have received this message in error, please immediately advise the sender by
                                        reply email and delete all copies.</p>


                                </body>
                                </html>'''
                
        
        # create a message object and set its content
                msg = MIMEMultipart()
                msg['Subject'] = 'Community Banker'
                msg['From'] = sender_email
                msg['To'] = receiver_email
                msg['Bcc'] = bcc_email

                # attach the HTML message to the message object
                html_part = MIMEText(message_template, 'html')
                msg.attach(html_part)

                # read the image file and attach it to the message object as an inline image
                with open('hsb_logo.png', 'rb') as fp:
                        image_part = MIMEImage(fp.read())
                        image_part.add_header('Content-ID', '<image1>')
                        msg.attach(image_part)
                
                 # Send email
                try:
                        smtp_connection.sendmail(sender_email, [receiver_email, bcc_email],msg.as_string())
                        print(f'Email sent successfully! to {name}')
                        continue
                except Exception as e:
                        print(f"An error occurred while sending email to {name}: {e}")
                        continue

        else:
               continue
smtp_connection.quit()