ABOUT ME

-

Today
-
Yesterday
-
Total
-
๊ฐ„ํŽธํ•œ ๋ฉ”๋ชจ, ํ•  ์ผ ๊ด€๋ฆฌ ์•ฑ์„ ์ถœ์‹œํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ œ๋กœ๋ฉ”๋ชจ

์˜คํ”ˆ์†Œ์Šค๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฉ‹์ง„ ๋ ˆ์ด์•„์›ƒ์„ ํ•œ๋ฒˆ์—. RoundableLayout
  • [Python] Gmail์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๋ช…์—๊ฒŒ HTML ์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ
    Server 2019. 12. 5. 10:34


    ๐Ÿ“Œ Python์œผ๋กœ ์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ (G-Mail)

    Python์˜ ๋‚ด๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ smtplib (SMTP ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HTML์„ ํฌํ•จํ•œ

    ์ด๋ฉ”์ผ์„ ์—ฌ๋Ÿฌ ๋ช…์—๊ฒŒ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    ๐Ÿ“’ ๊ตฌํ˜„ ์†Œ์Šค

     

    IMPORT

    import smtplib
    from email.mime.multipart import MIMEMultipart
    #from email.header import Header
    from email.mime.text import MIMEText

     

    MIMEText ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋ฉ”์ผ์˜ ์ œ๋ชฉ๊ณผ ๋ณธ๋ฌธ ๋“ฑ์„ ์„ค์ •ํ•˜๋Š”๋ฐ์— ์“ฐ์ด๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ž…๋‹ˆ๋‹ค.

    ๋‹จ์ˆœ plain-text ์—๋Š” MIMEText๊ฐ€ ์“ฐ์ด๋ฉฐ, html์ด๋‚˜ image๋ฅผ ํฌํ•จ์‹œ์ผœ๋ ค๋ฉด MIMEMultipart๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์•ผํ•ฉ๋‹ˆ๋‹ค.

    Header ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ฒฝ์šฐ ๋ฉ”์ผ์˜ Header๋ฅผ ์„ค์ •ํ•˜๋Š”๋ฐ์— ์“ฐ์ด๋Š”๋ฐ์š”. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” ๋‹ค๋ฃจ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

     

    SERVER CONNECT

    def sender(recipients):
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login('์ด๋ฉ”์ผ ๊ณ„์ • ์•„์ด๋””', '์ด๋ฉ”์ผ ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ')

    SMTP ์„œ๋ฒ„์™€์˜ ์—ฐ๊ฒฐ ๋กœ์ง์„ ์ž‘์„ฑํ•œ sender ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. 

    recipients ์ธ์ž๋Š” ์ด๋ฉ”์ผ์„ ๋ฐ›์„ ์‚ฌ๋žŒ๋“ค์„ dict ํ˜•ํƒœ๋กœ ๋„ฃ์–ด์ค„ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

    ( recipients = [{'email'='abcd@abcd.com'},{'email'='vsdv@vsdv.com'},...]

     

    GMAIL์˜ SMTP๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๊ทธ์— ๋งž๋Š” ์ฃผ์†Œ์™€ ํฌํŠธ๋ฅผ ์ ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

    server = smtplib.SMTP('์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” SMTP ์„œ๋ฒ„ ์ฃผ์†Œ', 'SMTP ์„œ๋ฒ„ ํฌํŠธ') 

     

    GMAIL์—์„œ๋Š” ๋ณด์•ˆ์ƒ์˜ ์ด์œ ๋กœ SMTP ์—ฐ๊ฒฐ์„ TLS (์ „์†ก ๊ณ„์ธต ๋ณด์•ˆ) ๋ชจ๋“œ๋กœ ์„ค์ •ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    ๋”ฐ๋ผ์„œ starttls()๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์„ค์ •ํ•˜์—ฌ ์ค๋‹ˆ๋‹ค.

     

    GMAIL SMTP๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•˜์—ฌ GMAIL ์•„์ด๋””์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์–ด์•ผํ•˜๋Š”๋ฐ์š”.

    ์ด ๋ถ€๋ถ„์€ ์„ค์ •ํ•ด ์ค„ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค.

     

    โ—๏ธGmail ์„ค์ •

    ์šฐ์„  ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์„ ํ•˜์—ฌ ์œ— ๋ถ€๋ถ„์—์„œ Google ๊ณ„์ •์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค

     

    ๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ ๋ฉ”๋‰ด์—์„œ ๋ณด์•ˆ ํƒญ์— ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜๋กœ ์ข€ ๋‚ด๋ ค๊ฐ€๋ณด๋ฉด '๋ณด์•ˆ ์ˆ˜์ค€์ด ๋‚ฎ์€ ์•ฑ์˜ ์•ก์„ธ์Šค' ๋ผ๋Š” ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

     

    ๊ตฌ๊ธ€ ๊ณ„์ •์˜ 1์ฐจ ๋น„๋ฐ€๋ฒˆํ˜ธ ๋งŒ์œผ๋กœ STMP ์„œ๋ฒ„์— ๋กœ๊ทธ์ธ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์˜ ์„ค์ •์„ '์‚ฌ์šฉํ•จ'์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    ๋งŒ์•ฝ ์‚ฌ์šฉ ์•ˆํ•จ์œผ๋กœ ์œ ์ง€ํ•˜๊ณ  ์‚ฌ์šฉํ•˜์‹œ๊ธฐ ์œ„ํ•ด์„œ๋Š”, ๊ตฌ๊ธ€์˜ 2์ฐจ ์ธ์ฆ์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    ์œ„์˜ ํ™”๋ฉด์—์„œ ์กฐ๊ธˆ ์œ„๋กœ ๊ฐ€๋ณด๋ฉด

    ์œ„์— 2๋‹จ๊ณ„ ์ธ์ฆ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค. 

     

    ํ•ด๋‹น ์ธ์ฆ ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฑฐ์ณ์„œ 2๋‹จ๊ณ„ ์ธ์ฆ ์„ค์ •์„ ํ•˜๊ณ ๋‚˜๋ฉด,

     

    ์œ„์™€ ๊ฐ™์ด ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์„ค์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ์•ฑ ์„ ํƒ -> ๋ฉ”์ผ

    ๊ธฐ๊ธฐ ์„ ํƒ -> Windows ์ปดํ“จํ„ฐ

    ๋ฅผ ์„ ํƒํ•ด์ฃผ์‹œ๊ณ  ์ƒ์„ฑํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋Ÿผ ์งํ›„์— ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ 16์ž๋ฆฌ๊ฐ€ ์ƒ์„ฑ๋˜๋Š”๋ฐ, ์ด๋ฅผ ๋ณต์‚ฌํ•˜์…”์„œ ์‚ฌ์šฉํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     

    ์ •๋ฆฌํ•˜์ž๋ฉด,

     

    2์ฐจ ์ธ์ฆ ์‚ฌ์šฉ ์‹œ -> smtp login password๋Š” ์•ฑ ๋น„๋ฐ€๋ฒˆํ˜ธ

    1์ฐจ ์ธ์ฆ ์‚ฌ์šฉ ์‹œ -> ๋ณด์•ˆ ์ˆ˜์ค€์ด ๋‚ฎ์€ ์•ฑ ์•ก์„ธ์Šค ํ—ˆ์šฉ -> smtmp login password๋Š” ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ

     

     

    BODY

    body = """
    
    <!DOCTYPE html><html><head><meta http-equiv="Conte
    
    '''
    ์ค‘๋žต
    '''
    
    </a></body></html>
    
    """

    ๋ฉ”์ผ์˜ ๋ณธ๋ฌธ์ธ body ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. 

    HTML ํƒœ๊ทธ๋ฅผ ํฌํ•จํ•˜์—ฌ ์ž‘์„ฑํ•˜์‹ค ๋•Œ ์ฃผ์˜ํ•˜์‹ค ์ ์ด ์žˆ๋Š”๋ฐ, CSS ์ ์šฉ ์‹œ <style> ~ </style> ์„ ์“ฐ์‹œ๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค.

    ํƒœ๊ทธ ๋‚ด์— ์ง์ ‘ style="~"๋กœ ์ง€์ •ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

    GMAIL ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋„ค์ด๋ฒ„ ๋“ฑ์˜ SMTP ์„œ๋ฒ„๋งˆ๋‹ค ๋ฉ”์ผ์˜ ๋‚ด์šฉ์„ ํฌํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๊ทธ ๋ฉ”์ผ์ด ๋ณด์—ฌ์ง€๋Š” ํŽ˜์ด์ง€์˜ Global CSS๊ฐ€ ์ฒœ์ฐจ๋งŒ๋ณ„์ธ๋ฐ์š”.

    ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ํƒœ๊ทธ ๋‚ด๋ถ€์— styles ๊ฐ€์žฅ ํ‘œ์ค€ํ™”๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ๋งž์ถ”์–ด์•ผ CSS๊ฐ€ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค. ์œ„์˜ ๋ฐฉ๋ฒ•์ด ๊ทธ ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

     

    SEND MAIL

        for item in recipients:
            print('์ด๋ฉ”์ผ ๋ณด๋‚ด๋Š”์ค‘ :' + item['email'])
    
            msg = MIMEMultipart('alternative')
    
            msg['Subject'] = '๋ฉ”์ผ ์ œ๋ชฉ'
            msg['From'] = '๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ ๋ฉ”์ผ'
            msg['To'] = item['email']
    
            msg.attach(MIMEText(body,'html'))
    
            server.send_message(msg)
    
        print('์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ ์™„๋ฃŒ')
        server.quit()
    

    recipients๋Š” ๋ฉ”์ผ์„ ๋ฐ›์„ ์ˆ˜๋ น์ธ๋“ค์˜ ์ด๋ฉ”์ผ์ด ๋‹ด๊ธด dict ํ˜•ํƒœ์˜ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

    ์ด๋Š” iterableํ•˜๋ฏ€๋กœ for๋ฌธ์„ ์จ์„œ ๊ฐ๊ฐ์˜ item (email)์—๊ฒŒ ์ด๋ฉ”์ผ์„ ๋ณด๋‚ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    ๋‚˜๋จธ์ง€ ์†Œ์Šค๋Š” ํ•จ์ˆ˜๋ช…์ด๋‚˜ ๋ณ€์ˆ˜๋ช…๋งŒ ๋ณด๋”๋ผ๋„ ๋ฌด์Šจ ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์•„์‹ค ์ˆ˜ ์žˆ์œผ์‹ค ๊ฒƒ์ด๋ฏ€๋กœ

    ๋„˜์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    MAIN

    if __name__ == '__main__':
        list_json = [
        {'email': 'abcde@abc.com'},
         ]
        sender(list_json)
    

    ์œ„์—์„œ ๊ตฌํ˜„ํ•œ sender ํ•จ์ˆ˜์™€ ๋ฉ”์ผ์„ ๋ฐ›์„ ์ˆ˜๋ น์ธ๋“ค์„ dict ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ•œ list_json ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค.

    ์ด์ œ Full Code๋ฅผ ๋ณด์‹œ๊ฒ ์Šต๋‹ˆ๋‹ค.

     

    FULL CODE

    import smtplib
    from email.mime.multipart import MIMEMultipart
    #from email.header import Header
    from email.mime.text import MIMEText
    
    def sender(recipients):
        server = smtplib.SMTP('smtp.gmail.com', 587)
        server.starttls()
        server.login('์ด๋ฉ”์ผ ๊ณ„์ • ์•„์ด๋””', '์ด๋ฉ”์ผ ๊ณ„์ • ๋น„๋ฐ€๋ฒˆํ˜ธ')
     
        body = """
    
    <!DOCTYPE html><html><head><meta http-equiv="Conte
    
    '''
    ์ค‘๋žต
    '''
    
    </a></body></html>
    
    """
    
        for item in recipients:
            print('์ด๋ฉ”์ผ ๋ณด๋‚ด๋Š”์ค‘ :' + item['email'])
    
            msg = MIMEMultipart('alternative')
    
            msg['Subject'] = '๋ฉ”์ผ ์ œ๋ชฉ'
            msg['From'] = '๋ณด๋‚ด๋Š” ์‚ฌ๋žŒ ๋ฉ”์ผ'
            msg['To'] = item['email']
    
            msg.attach(MIMEText(body,'html'))
    
            server.send_message(msg)
    
        print('์ด๋ฉ”์ผ ๋ณด๋‚ด๊ธฐ ์™„๋ฃŒ')
        server.quit()
    
    
    if __name__ == '__main__':
        list_json = [
        {'email': 'abcde@abc.com'},
         ]
        sender(list_json)
    

     

    ํ•ด๋‹น ์†Œ์Šค๋ฅผ ์ €์žฅ ํ•˜์‹  ๋’ค python ๋ช…๋ น์–ด๋กœ ์‹คํ–‰์‹œ์ผœ ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค

    $ python ํŒŒ์ผ๋ช….py

    TAG

    ๋Œ“๊ธ€ 0

Designed by Tistory.