2018년 8월 26일 일요일

Python에서 email 다루기


email과 smtplib를 이용



from email.mime.multipart import MIMEMultipart
from smtplib import SMTP_SSL

SMTP_SERVER = "smtp.naver.com"
SMTP_PORT = "465"
SMTP_USER = ""
SMTP_PASSWORD = ""

def send_mail(name, addr, subject, contents, attachment=False):
    msg = MIMEMultipart("alternative")
    text = MIMEText(contents)
    msg.attach(text)
    if attachment:
        from email.mime.base import MIMEBase
        from email import encoders
        msg = MIMEMultipart('mixed')
        try:
            file_data = MIMEBase('application', 'octet-steam')
            with oepn(attachment, 'rb') as f:
                file_contents = f.read()
                file_data.set_payload(file_contents)
                encoders.encode_base64(file_data)
                from os.path import basename
                filename = basename(attachment)
                fild_data.add_header('Content-Disposition', 'attachment', filename=filename)
                msg.attach(file_data)
        except e:
            print("Somthing wrong while attach a file")
    smtp = SMTP_SSL(SMTP_SERVER, SMTP_PORT)
    smtp.login(SMTP_USER, SMTP_PASSWORD)
    msg['From'] = name
    msg['To'] = addr
    msg['Subject'] = subject
    smtp.sendmail('chinst@naver.com', addr, msg.as_string())
    smtp.close()

def main():
    global SMTP_USER, SMTP_PASSWORD
    SMTP_USER = str(input('Enter User ID : '))
    SMTP_PASSWORD = str(input('Enter Password : '))
    send_mail('TEST MAIL', 'chinst@naver.com', 'TEST mail from Python', 'This is a test mail.\nIs this OK?')



if __name__ == '__main__':
    main()

댓글 없음:

댓글 쓰기

200926.가오리코스 라이딩

9/26 골절인지 아닌지 확인 안됨. 이후 미세골절여부 확인 핸드폰을 드는 것도 어려움 9/29 x ray 다시 찍지 않고 이후 재 방문 요청 ...