๐Ÿ”ฅ TIL - Day 24

Kim Dae Hyunยท2021๋…„ 10์›” 9์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
27/93

๐Ÿ“Œ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋žŒ ๋ณด๋‚ด๊ธฐ

ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ ์ค‘์— ๋งค์ผ ์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „๋‹ฌํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋žŒ์„ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ด๋ฉ”์ผ, ์นด์นด์˜คํ†ก, ๋ฌธ์ž ๋“ฑ์ด ์žˆ๋Š”๋ฐ ๋‚˜๋Š” ๋ฌด๋ฃŒ๋กœ ๊ฐ€์žฅ ๊ฐ€๋ณ๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ด๋ฉ”์ผ์„ ์„ ํƒํ–ˆ๋‹ค.

Flask๋Š” ๋ฉ”์ผ ์ „์†ก์„ ์œ„ํ•ด Flask-Mail ๋ชจ๋“ˆ์„ ์ œ๊ณตํ•œ๋‹ค.

pip install Flask-Mail

์„ค์น˜ํ•œ Flask-Mail ๋ชจ๋“ˆ์„ import ํ•œ๋‹ค.

from flask_mail import Mail, Message

๋ฉ”์ผ ์ „์†ก์— ์žˆ์–ด ๊ธฐ๋ณธ์ ์ธ ์„ค์ •์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค

app = Flask(__name__)

app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'mail_id'
app.config['MAIL_PASSWORD'] = 'mail_password'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True

mail = Mail(app)

๊ฐ„๋‹จํ•˜๊ฒŒ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ดค๋‹ค.
test mail ์ด๋ผ๋Š” ์ œ๋ชฉ์œผ๋กœ zbeld123@gmail๊ณผ zbeld123@naver.com๋กœ ์ด๋ฉ”์ผ์„ ์ „์†กํ•˜๋„๋ก ํ–ˆ๋‹ค.
๋‚ด์šฉ์€ Message์˜ html์„ ํ†ตํ•ด ๊ตฌ์„ฑํ–ˆ๋‹ค.

@app.route('/')
def test():  
    msg = Message("test mail", sender='zbeld123@gmail.com', recipients=["zbeld123@naver.com", "zbeld123@gmail.com"])
    msg.html = '<h1>TEST</h1>'

    mail.send(msg)
    return "ok"

ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋Š” Good!


์ด์ œ ์‚ฌ์šฉ์ž์˜ ๋™์˜๋ฅผ ๋ฐ›์•„ ์ด๋ฉ”์ผ์„ ์ˆ˜์ง‘ํ•˜๊ณ  ์Šค์ผ€์ค„๋Ÿฌ๋ฅผ ํ†ตํ•ด ํŠน์ • ์‹œ๊ฐ„์— API๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜์—ฌ ์ด๋ฉ”์ผ๋กœ ์ „์†กํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ๋‚ด์ผ ๋งˆ๋ฌด๋ฆฌํ•˜์ž !

profile
์ข€ ๋” ์ฒœ์ฒœํžˆ ๊นŒ๋จน๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค. ๐Ÿง

0๊ฐœ์˜ ๋Œ“๊ธ€