Day 032

AWESOMee·2022년 2월 26일
0

Udemy Python Bootcamp

목록 보기
32/64
post-thumbnail

Udemy Python Bootcamp Day 032

SMTP (smtplib)

Simple Mail Transfer Protocol
SMTP is basically the postman who knows how to handle the email and take itto various post offices and eventually put it into recipient's computer.

# from gmail to yahoo
import smtplib

my_email = "*********@gmail.com"
password = "*********"

with smtplib.SMTP("smtp.gmail.com") as connection:
    connection.starttls()  # this line basically will make this connection secure.
    connection.login(user=my_email, password=password)
    connection.sendmail(
        from_addr=my_email, 
        to_addrs="*********@yahoo.com",
        msg="Subject:Hello\n\nThis is the body of my email"
    )

미쳤다.. 개신기함
에러 발생하면 gmail 보안 2단계 인증 해제 해야함
subject입력 안하면 spam으로 간주되니 참고

# from yahoo to gamil
import smtplib

my_email = "*********@yahoo.com"
password = "*********"

with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
    connection.starttls()
    connection.login(user=my_email, password=password)
    connection.sendmail(
        from_addr=my_email,
        to_addrs="*********@gmail.com",
        msg="Subject:Hello\n\nThis is the body of my email"
    )

에러 발생 시 yahoo 보안 앱 비밀번호 생성하여 입력

Datetime Module

import datetime as dt

now = dt.datetime.now()
year = now.year
month = now.month
day_of_week = now.weekday()
print(day_of_week)

date_of_birth = dt.datetime(year=2008, month=5, day=25)
print(date_of_birth)

#output
5 (today:Saturday) Monday is 0.
2008-05-25 00:00:00 (hour 이하는 default값 00:00:00)

Automated Birthday Wisher Project

  • dictionary comprehension
  • reading CSVs
  • creating dataframes from pandas
  • creating tuples
  • working with dictionarys, file paths, opening files, reaplacing files, reading them
  • writing and sending email
  • datatime module

1. Update the birthdays.csv with your friends & family's details.

birthdays.csv

name,email,year,month,day
******,***********@gmail.com,****,**,**
******,***********@gmail.com,****,*,**

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

# HINT 1: Create a tuple from today's month and day using datetime. 
today = dt.datetime.now()
today_tuple = (today.month, today.day)

# HINT 2: Use pandas to read the birthdays.csv
data = pandas.read_csv("birthdays.csv")

# HINT 3: Use dictionary comprehension to create a dictionary from birthday.csv that is formated like this:
birthdays_dict = {(data_row["month"], data_row["day"]): data_row for (index, data_row) in data.iterrows()}

#HINT 4: Then you could compare and see if today's month/day tuple matches one of the keys in birthday_dict like this:
if today_tuple in birthdays_dict:

Hint3에서 도저히 어떻게 해야할지를 몰라했는데,,
생각보다 간단했다..

3. Pick a random letter and replace the [NAME]

    birthday_person = birthdays_dict[today_tuple]
    # HINT 1: Think about the relative file path to open each letter.
    # HINT 2: Use the random module to get a number between 1-3 to pick a random letter.
    file_path = f"letter_templates/letter_{random.randint(1,3)}.txt"
    # HINT 3: Use the replace() method to replace [NAME] with the actual name. https://www.w3schools.com/python/ref_string_replace.asp
    with open(file_path) as letter_file:
        contents = letter_file.read()
        contents.replace("[NAME]", birthday_person["name"])

4. Send the letter generated in step 3 to that person's email address.

my_email = "**********@yahoo.com"
password = "***********"

    # HINT 1: Gmail(smtp.gmail.com), Yahoo(smtp.mail.yahoo.com), Hotmail(smtp.live.com), Outlook(smtp-mail.outlook.com)
    with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
        # HINT 2: Remember to call .starttls()
        connection.starttls()
        # HINT 3: Remember to login to your email service with email/password. Make sure your security setting is set to allow less secure apps.
        connection.login(MY_EMAIL, MY_PASSWORD)
        connection.sendmail(
            from_addr=MY_EMAIL,
            # HINT 4: The message should have the Subject: Happy Birthday then after \n\n The Message Body.
            to_addrs=birthday_person["email"],
            msg=f"Subject:Happy Birthday\n\n{contents}"
        )

FINAL

import datetime as dt
import pandas
import random
import smtplib

MY_EMAIL = "***********@yahoo.com"
MY_PASSWORD = "*************"

today = dt.datetime.now()
today_tuple = (today.month, today.day)

data = pandas.read_csv("birthdays.csv")
birthdays_dict = {(data_row["month"], data_row["day"]): data_row for (index, data_row) in data.iterrows()}

if today_tuple in birthdays_dict:
    birthday_person = birthdays_dict[today_tuple]
    file_path = f"letter_templates/letter_{random.randint(1,3)}.txt"
    with open(file_path) as letter_file:
        contents = letter_file.read()
        contents = contents.replace("[NAME]", birthday_person["name"])

    with smtplib.SMTP("smtp.mail.yahoo.com") as connection:
        connection.starttls()
        connection.login(MY_EMAIL, MY_PASSWORD)
        connection.sendmail(
            from_addr=MY_EMAIL,
            to_addrs=birthday_person["email"],
            msg=f"Subject:Happy Birthday\n\n{contents}"
        )
profile
개발을 배우는 듯 하면서도

0개의 댓글