파이썬/파이썬(python) 중급

[32-3 파이썬] 파이썬으로 긍정 명언 이메일 보내기

Olivia-BlackCherry 2022. 9. 25. 11:58

1. 요일 확인하기 

weekday는 0~6까지의 숫자, integer타입으로 출력된다. 

import datetime
now = datetime.datetime.now()
weekday= now.weekday()
print(type(weekday))

 

 

2. 파일 생성하기

명언이 담겨있는 quotes.txt파일을 읽기 모드로 readlines로 불러와

리스트 타입으로 저장하고,

random모듈을 이용하여 하나의 문장만 선택한다.

import random
with open(file="quotes.txt") as f:
    data = f.readlines()
    pick_one_quotes = random.choice(data)

quotes.txt

"There is no traffic jam along the extra mile." - Roger Staubach
"Motivation is a fire from within. If someone else tries to light that fire under you, chances are it will burn very briefly." - Stephen R. Covey
"Success is liking yourself, liking what you do, and liking how you do it." - Maya Angelou
"Only put off until tomorrow what you are willing to die having left undone." - Pablo Picasso
"There are two types of people who will tell you that you cannot make a difference in this world: those who are afraid to try and those who are afraid you will succeed." - Ray Goforth
"Everything you want is on the other side of fear." - Jack Canfield
"Life is 10% what happens to us and 90% how we react to it." - Dennis P. Kimbro
"You are never too old to set another goal or to dream a new dream." - C.S. Lewis

 

 

3. smtp 함수 생성하기

def send_msg():
    with smtplib.SMTP("smtp.naver.com", port=587) as connection:
        connection.starttls()
        connection.login(user="oliviacodingschool", password='1234')
        msg = MIMEText(pick_one_quotes)
        msg['From'] = "oliviacodingschool@naver.com"
        msg['Subject'] = '제목: 메일 발송 시험'
        msg['To'] = "oliviacodingschool@gmail.com"

        connection.sendmail(from_addr="oliviacodingschool@naver.com",
                            to_addrs="oliviacodingschool@gmail.com",
                            msg=msg.as_string())

 

 

4. 조건에 맞다면 함수 실행하기

if weekday == 6:
    send_msg()

 

 

<최종코드>

import smtplib
from email.mime.text import MIMEText

def send_msg():
    with smtplib.SMTP("smtp.naver.com", port=587) as connection:
        connection.starttls()
        connection.login(user="oliviacodingschool", password='1234')
        msg = MIMEText(pick_one_quotes)
        msg['From'] = "oliviacodingschool@naver.com"
        msg['Subject'] = '제목: 메일 발송 시험'
        msg['To'] = "oliviacodingschool@gmail.com"

        connection.sendmail(from_addr="oliviacodingschool@naver.com",
                            to_addrs="oliviacodingschool@gmail.com",
                            msg=msg.as_string())

import datetime
import random
now = datetime.datetime.now()
weekday= now.weekday()
print(type(weekday))

with open(file="quotes.txt") as f:
    data = f.readlines()
    pick_one_quotes = random.choice(data)

if weekday == 6:
    send_msg()

 

 

긍정명언 참고 사이트

https://www.positivityblog.com/monday-motivation-quotes/

 

101 Monday Motivation Quotes (+ My 5 Favorite Tips for a Great Week)

Get a serious boost of Monday motivation to pick yourself up and start moving forward this week.

www.positivityblog.com