J0hnMilt0n

J0hnMilt0n

Reverse Engineer | Android Modder

21 May 2022

Automated Birthday Wisher

Objectives: Send email using python code.

Libraries: smtplib, datetime, pandas

Code: Automated Birthday Wisher


How email works?

For example, we have alice and bob as sender and recipient separately.

sender: alice@gmail.com

recipient: bob@yahoo.com

alice@gmail.com -> Gmail Mail Server -> Yahoo Mail Server -> bob's computer

Gmail Mail Server will recieve alice’s message and Yahoo Mail Server will store the message until bob logs on to yahoo.com which download the message. This process relies on a protocol called “SMTP”(Simple Mail Transfer Protocol).

Imagine these mail servers as post office and bob’s computer being the mail box. SMTP is the postman who handles the mail.

Let’s Get Started

In python, we’ll be using smtplib.

import smtplib

my_email = "alice@gmail.com"
password = "P@ssw0rd!"

connection = smtplib.SMTP("smtp.gmail.com") # google SMTP information for your email provider
connection.starttls() # use TLS to secure our connection to email server
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email, to_addrs="bob@yahoo.com", msg="Hello")
connection.close()

You might get errors. Because by default, gmail doesn’t let anybody access your email account. In order to use python code sends the email, we have to lower the security boundry. So, please create a new email account. DON’T USE PRIMARY EMAIL ACCOUNT!

Username(upper right)
    -> Manage your Google Account 
    -> Security 
    -> Signing in to Google --> "Use your phone to sign in" and "2-Step Verification" are Off 
    -> Less secure app access --> On

After you successfully sent the email, you probably notice that it goes to the spam folder. That’s because it doesn’t have a subject headline.

# update this line
connection.sendmail(
    from_addr=my_email,
    to_addrs="bob@yahoo.com"
    msg="Subject:Hello\n\nThis is the body of my email."
)

Just like file opening, we can update the code to avoid using close().

with smtplib.SMTP("smtp.gmail.com") as connection:
    # connection stuff

Set time schedule

Try datetime module.

import datetime as dt

now = dt.datetime.now() # current date and time
year = now.year
month = now.month
day_of_week = now.weekday()

date_of_birth = dt.datetime(year=1970, month=1, day=1)

practice: Monday Motivational Quotes

  • Use the datetime module to obtain the current day of the week.
  • Open the quotes.txt file and obtain a list of quotes.
  • Use the random module to pick a random quote from your list of quotes.
  • Use the smtplib to send the email to yourself.

challenge: Automated Birthday Wisher

  1. Put contacts into birthday.csv file.

  2. Check if today matches a birthday in the birthday.csv file.

Use tuple for today’s month and day.

Use pandas to read the birthdays.csv file.

Create a dictionary for contacts’ birthday.

Check if it matches today’s month/day.

  1. If there is a match, pick a random letter.

  2. Send the letter.

Move it to Cloud

Upload it to a server and create a cron job.


Categories

Tags