Auto emailer using python

Blogs

Data Validation- Using ADF for Data Migration
July 24, 2023
Common Problems in Apache Spark(5s) – Part 1
July 31, 2023

Auto emailer using python

Sending email manually can be replaced using python
Here is demo on how to send automated reminders emails with Python based on reading a file and checking due date of the customer to pay their bills and sending a alert mail
We are using smpt server to send mails. To send the mailsecurely using tls
Adding the html version for mail text. This converts the message into a multipart/alternative
container, with the original text message as the first part and the new html
message as the second part.

#import necesary libraries
import pandas as pd
from datetime import date
from dateutil import parser
import smtplib
from email.message import EmailMessage
from email.utils import formataddr

#Here we are using gmail to send mails

PORT = 587
EMAIL_SERVER = “smtp.gmail.com”

#Adding the sender mail
sender_email = ‘xyz@gmail.com’
password_email = ‘xyz123#’

# using send mail function with passing the details from reading file and sending email
#
def send_email(subject, receiver_email, name, due_date, invoice_no, amount):
# Create the base text message.
msg = EmailMessage()
msg[“Subject”] = subject
msg[“From”] = formataddr((“Mobile Bill Remainder”, f”{sender_email}”))
msg[“To”] = receiver_email

msg.set_content(
f”””

Hi {name},

I hope you are well.

I just wanted to drop you a quick note to remind you that {amount} USD in respect of our invoice {invoice_no} is due for payment on {due_date}.

I would be really grateful if you could confirm that everything is on track for payment.

Best regards

YOUR NAME

“””,
subtype=”html”,
)

with smtplib.SMTP(EMAIL_SERVER, PORT) as server:
server.starttls()
server.login(sender_email, password_email)
server.sendmail(sender_email, receiver_email, msg.as_string())

if __name__ == “__main__”:

#reading a csv file using dataframe to check the customer due date
df=pd.read_csv(“/customer.csv”,parse_dates=[‘due_date’])

present=date.today()

#defining a funtion to read values in df and pass them to the sen mail function

def read_file(df):
for _,row in df.iterrows():

present=date.today()
if  row[‘due_date’].date()  < present :
send_email(
subject=f’Due for Invoice No: {row[“invoice_no”]}’,
name=row[‘name’],
receiver_email=row[’email’],
due_date=row[‘due_date’].strftime(‘%d %b %y’),
invoice_no=row[‘invoice_no’],
amount=row[‘amount’] )
return ‘mail sent to cust’

res=read_file(df)
print(res)


pooja.k

Leave a Reply

Your email address will not be published. Required fields are marked *