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_birthday
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_birthday()
for item in data:
        receiver_email = 'swright@homestbk.com'
        name = item[2]
        send_to_address= item[3]
        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>Hope all is well, my records show today is your birthday.</p>
                                        <p>Happy birthday, have a great day!<br><br>
                                        <p>{send_to_address}</p>
                                        <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'] = 'Happy Birthday!'
                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],msg.as_string())
                        print(f'Email sent successfully! to {name}')
                except Exception as e:
                        print(f"An error occurred while sending email to {name}: {e}")

        else:
               continue
smtp_connection.quit()