Day 32-SMTP & python anywhere


Posted by pei_______ on 2022-05-12

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


smtplib Documentation

python anywhere


SMTP
Simple Mail Transfer Protocol

email provider = smtp server
Gmail: stmp.gmail.com
Hotmail: smtp.live.com
Yahoo: smtp.mail.yahoo.com

TLS 傳輸層安全性協定 (Trasport Layer Security)
是一種安全協定,目的是為網路通訊提供安全及資料完整性保障


Monday Motivation Message


import smtplib
import datetime as dt
from random import choice

MY_EMAIL = "YOUR EMAIL"
PASSWORD = "YOUR PASSWORD"

# datetime.datetime object
now = dt.datetime.now()
day_of_week = now.weekday()

with open("quotes.txt") as quotes_file:
    message = choice(quotes_file.readlines())


if day_of_week == 3:
    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        connection.login(user=MY_EMAIL, password=PASSWORD)
        connection.sendmail(from_addr=MY_EMAIL,
                            to_addrs="TARGET ADDRESS",
                            msg=f"Monday Motivation\n\n{message}")

from datetime import datetime
import pandas
from random import randint
import smtplib

MY_EMAIL = "YOUR EMAIL"
PASSWORD = "YOUR PASSWORD"
BIRTH_TODAY = []


def print_mail(birth_today):
    with open(f"letter_templates/letter_{randint(1, 3)}.txt") as letter_file:
        new_letter = letter_file.read()
        new_letter = new_letter.replace("[NAME]", birth_today)
        return new_letter


def send_mail(email, birth_letter_msg):
    with smtplib.SMTP("smtp.gmail.com") as connect:
        connect.starttls()
        connect.login(user=MY_EMAIL, password=PASSWORD)
        connect.sendmail(from_addr=MY_EMAIL,
                         to_addrs=email,
                         msg=f"Subject:Happy Birthday\n\n{birth_letter_msg}")


birth_film = pandas.read_csv("birthdays.csv")

today_month = datetime.now().month
today_day = datetime.now().day

birth_this_month = birth_film.name[birth_film.month == today_month].to_list()
birth_this_day = birth_film.name[birth_film.day == today_day].to_list()

for who in birth_this_month:
    if who in birth_this_day:
        BIRTH_TODAY.append(who)

for who in BIRTH_TODAY:
    birth_email = birth_film.email[birth_film.name == who]
    birth_letter = print_mail(who)
    send_mail(birth_email, birth_letter)

#Python #課堂筆記 #100 Days of Code







Related Posts

使用 Gazebo 模擬器控制機器人建立 2D 地圖

使用 Gazebo 模擬器控制機器人建立 2D 地圖

版本控制-GIT基本指令

版本控制-GIT基本指令

變成rule的形狀(3) - Prettier + ESLint

變成rule的形狀(3) - Prettier + ESLint


Comments