๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป IT·๊ธฐ์ˆ ·ํ†ต๊ณ„

Python์œผ๋กœ ๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ [@gmail]

by nowgeun 2023. 1. 21.
728x90

์˜ค๋Š˜์€ Python์œผ๋กœ ์ด๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

"๊ตณ์ด ์™œ Python์œผ๋กœ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋ฒ•์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๊นŒ?"๋ผ๊ณ  ๋ฌป๋Š”๋‹ค๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค:

 

1.  ๋งŽ์€ ์‚ฌ๋žŒ์—๊ฒŒ ์‰ฝ๊ฒŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1,000๋ช…์˜ ์ด๋ฉ”์ผ์ด ์žˆ๋Š” ์—‘์…€ ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•˜๋ ค๋ฉด ์ง€๋ฃจํ•œ ๋ฐ˜๋ณต ์ž‘์—…์„ ๊ณ„์†ํ•ด์•ผ ํ•˜์ง€๋งŒ, Python์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด 1,000๋ช…์ด๋“  10,000๋ช…์ด๋“  ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์šฐ๋ฆฌ๊ฐ€ ๋ฐ›๋Š” ์ŠคํŒธ๋ฉ”์ผ์€ ์ด๋Ÿฐ ๋ฐฉ์‹์œผ๋กœ ๋ณด๋‚ด์ง‘๋‹ˆ๋‹ค.

 

2. ์Šค์ผ€์ค„๋Ÿฌ๋‚˜ crontab์„ ํ™œ์šฉํ•˜์—ฌ ์ •ํ•ด์ง„ ์‹œ๊ฐ„์— ์ •๊ธฐ์ ์œผ๋กœ ๋ฉ”์ผ ๋ฐœ์†ก์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜น์‹œ ๋งค์ผ ์•„์นจ 8์‹œ์— ํŠน์ • ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ„ ๋‰ด์Šค๋ ˆํ„ฐ๋ฅผ ๋ฐ›์•„๋ณด๊ณ  ๊ณ„์‹ ๊ฐ€์š”?  ํŠน์ •์‹œ๊ฐ„์— ์ •๊ธฐ์ ์œผ๋กœ ๋ฐœ์†ก๋˜๋Š” ๋ฉ”์ผ ๋˜ํ•œ Python ํ˜น์€ ๋‹ค๋ฅธ ์–ธ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋™์œผ๋กœ ๋ณด๋‚ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

 

3. ๋น„์Šทํ•œ ์–‘์‹์˜ ์ด๋ฉ”์ผ์„ ์กฐ๊ฑด ๋ณ„๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์žŠ์–ด๋ฒ„๋ฆฐ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์—์„œ ์ž„์‹œ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ฐœ๊ธ‰๋œ ๋ฉ”์ผ, ํ˜น์€ ํšŒ์›๊ฐ€์ž… ์ค‘์— ๋ณธ์ธ ํ™•์ธ ์š”์ฒญ์„ ํ•˜๋Š” ๋ฉ”์ผ์„ ๋ฐ›์œผ์‹  ์ ์ด ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ๋„ ์•„๋‹ˆ๋ผ๋ฉด ํ•ฉ๊ฒฉ๊ณผ ๋ถˆํ•ฉ๊ฒฉ ๋ฉ”์ผ์„ ๋ฐ›์œผ์‹  ์ ์€ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค. ์ด๋Ÿฐ ๋ฉ”์ผ๋“ค์€ ๋ชจ๋‘ ๋น„์Šทํ•œ ์–‘์‹์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋‚˜, ์•ˆ์— ๋‹ด๊ธด ๋‚ด์šฉ์€ ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. Python์„ ํ™œ์šฉํ•œ๋‹ค๋ฉด ๋น„์Šทํ•œ ์–‘์‹์˜ ์ด๋ฉ”์ผ์„ ์กฐ๊ฑด ๋ณ„๋กœ ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์šฐ์„  Python์„ ์‚ฌ์šฉํ•ด์„œ @gmail.com ์ด๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


์‚ฌ์ „ ์ž‘์—…

๊ตณ์ด ๋‹ค๋ฅธ ๋ฉ”์ผ ์„œ๋ฒ„๋ฅผ ๋†”๋‘๊ณ  gmail์„ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ๋‹ค๋ฅธ ๋ฉ”์ผ๋ง ์„œ๋ฒ„์— ๋Œ€๋น„ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์ด ์ข€ ๋ณต์žกํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. (๊ทธ๋ฆฌ๊ณ  ์ œ ๋ง˜์ž…๋‹ˆ๋‹ค) gmail๋กœ ๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์‚ฌ์ „์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค๋‹ค. 

 

1.   2๋‹จ๊ณ„ ์ธ์ฆ ์„ค์ • (๋งํฌ > [Google์— ๋กœ๊ทธ์ธ] ๋ฉ”๋‰ด ์•„๋ž˜ 2๋‹จ๊ณ„ ์ธ์ฆ์„ ์‚ฌ์šฉ์œผ๋กœ ๋ณ€๊ฒฝ)

2.   ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ (๋งํฌ > [Google์— ๋กœ๊ทธ์ธ] ๋ฉ”๋‰ด ์•„๋ž˜ ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ ํด๋ฆญํ•˜์—ฌ ์ƒ์„ฑ)

์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ƒ์„ฑ ํ›„์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฐฝ์ด ํŒ์—…๋œ๋‹ค.

3. ์ƒ์„ฑ๋œ ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์–ตํ•ด์คฌ๋‹ค๊ฐ€ ์•„๋ž˜ Python ์ฝ”๋“œ์— ๋ณต๋ถ™ ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์†Œ์Šค์ฝ”๋“œ

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณต๋ถ™ํ•˜์‹  ๋’ค, ์œ„์—์„œ ์ƒ์„ฑํ•œ ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ง€๋ฉ”์ผ ๊ณ„์ • ID๋กœ ๋Œ€์ฒด๋ฅผ ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ œ๋ชฉ์€ subject ๋ณ€์ˆ˜์—, ๋ณธ๋ฌธ์€ body์— ์ž‘์„ฑํ•ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค! 

 

์ผ๋ฐ˜ Plain Text๋กœ ๋ณด๋‚ด๊ธฐ

import smtplib
from email.mime.text import MIMEText

def send_email(subject, body, ID, PWD, receiver_list):
    """
    subject: ๋ฉ”์ผ ์ œ๋ชฉ
    body: ๋ฉ”์ผ ๋ณธ๋ฌธ
    ID: ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ
    PWD: ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฉ”์ผ ๋น„๋ฐ€๋ฒˆํ˜ธ
    receiver_list: ๋ฐ›๋Š” ์‚ฌ๋žŒ๋“ค์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ
    """

    msg = MIMEText(body)
    msg['From'] = ID
    msg['To'] = ', '.join(receiver_list)
    msg['Subject'] = subject
    
    smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465) # 465๋Š” ํฌํŠธ๋„˜๋ฒ„ (์ˆ˜์ •ํ•„์š”์—†์Œ)
    smtp_server.login(ID, PWD)
    smtp_server.sendmail(ID, receiver_list, msg.as_string())
    smtp_server.quit()
    
    
ID = '๋ณด๋‚ด๋Š”์‚ฌ๋žŒ@gmail.com'                     # ๋ฐœ์‹ ์ž ์ง€๋ฉ”์ผ ์ฃผ์†Œ
PWD = 'ABCDEFGHIJKLMNOP'                      # ๋ฐœ์‹ ์ž ๋น„๋ฐ€๋ฒˆํ˜ธ
receiver_list = ['๋ณด๋‚ด๋Š”์‚ฌ๋žŒ@gmail.com']        # ์ˆ˜์‹ ์ž ๋ฆฌ์ŠคํŠธ
subject = "์ž๋™ ์ด๋ฉ”์ผ ๋ฐœ์†ก ํ…Œ์ŠคํŠธ"             # ์ด๋ฉ”์ผ ์ œ๋ชฉ
body = "Python์œผ๋กœ ์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ. ์„ฑ๊ณต!"        # ์ด๋ฉ”์ผ ๋ณธ๋ฌธ

send_email(subject, body, ID, PWD, receiver_list)

 

 

HTML๋กœ ๋ณด๋‚ด๊ธฐ

def send_email(subject, html_path, ID, PWD, receiver_list):
    """
    subject: ๋ฉ”์ผ ์ œ๋ชฉ
    body: ๋ฉ”์ผ ๋ณธ๋ฌธ
    ID: ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ
    PWD: ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ์˜ ์ด๋ฉ”์ผ ๋น„๋ฐ€๋ฒˆํ˜ธ
    receiver_list: ๋ฐ›๋Š” ์‚ฌ๋žŒ๋“ค์˜ ์ด๋ฉ”์ผ ์ฃผ์†Œ
    """
    html = open(html_path, 'r', encoding='utf-8') 
    msg = MIMEMultipart("related")   
    msg['From'] = ID
    msg['To'] = ', '.join(receiver_list)
    msg['Subject'] = subject
    
    msg.attach(MIMEText(html.read(), "html"))
    
    smtp_server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    smtp_server.login(ID, PWD)
    smtp_server.sendmail(ID, receiver_list, msg.as_string())
    smtp_server.quit()


ID = '๋ณด๋‚ด๋Š”์‚ฌ๋žŒ@gmail.com'
PWD = 'ABCDEFGHIJKLMNOP'
receiver_list = ['๋ณด๋‚ด๋Š”์‚ฌ๋žŒ@gmail.com']
subject = "Happy Birthday!!"
html_path = './Cake.html'

 

์‹คํ–‰๊ฒฐ๊ณผ

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์ผ์ด ๋ฐœ์†ก์ด ๋ฉ๋‹ˆ๋‹ค.

 

์ผ๋ฐ˜ Plain Text

 

์ฐธ ์‰ฝ์ฅฌ?

 

 

HTML

์ด๋ฏธ์ง€๊ฐ€ ๋„ˆ๋ฌด ์ปค์„œ ์ž˜๋ ธ์Šต๋‹ˆ๋‹ค... ์•„๋ฌดํŠผ html ํ˜•์‹์œผ๋กœ๋„ ์ด๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ !

 

+ ์ถ”๊ฐ€

jinja2๋ฅผ ํ™œ์šฉํ•œ html template์„ ๋งŒ๋“ ๋‹ค๋ฉด ํ™œ์šฉํ•œ๋‹ค๋ฉด ๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ๋งˆ๋‹ค ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅธ ๋งž์ถคํ˜• ์ด๋ฉ”์ผ์„ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  email ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•œ๋‹ค๋ฉด ์ฒจ๋ถ€ํŒŒ์ผ์„ ๋ณด๋‚ธ๋‹ค๋˜์ง€, ์ด๋ฏธ์ง€๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฉ”์ผ์„ ๋ณด๋‚ด๋Š” ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ดํ›„ ํฌ์ŠคํŒ…์—์„œ๋Š” ๋” ๋งŽ์€ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์˜ค๋Š˜์€ ์—ฌ๊ธฐ๊นŒ์ง€! ๐Ÿ˜

 

(์ฝ”๋“œ ๋ฐ ์œ„ ํฌ์ŠคํŒ… ๊ด€๋ จ ๋ฌธ์˜๊ฐ€ ์žˆ์œผ์‹œ๋ฉด ์•„๋ž˜ ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š”!)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€