Molybdenum의 개발기록

[TIL] 4일차_IMAP 본문

TIL

[TIL] 4일차_IMAP

Molybdenum_j 2023. 2. 28. 15:15

▶ SMTP(Simple Mail Transfer Protocol) -> 간단하게 메일을 보내기 위한 약속

  1. sMTP 메일 서버를 연결한다.
  2. SMTP 메일 서버에 로그인한다.
  3. SMTP 메일 서버로 메일을 보낸다.
import smtplib
from email.message import EmailMessage
import imghdr -> 이미지 파일의 확장자가 변경되면 자동으로 변경될 수 있도록함

SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 465

message = EmailMessage()
message.set_content("복습중입니다.")

message["Subject"]="이것은 제목입니다." -> header에 들어감
message["From"] = "이메일계정"
message["To"] = "받는이메일계정"

with open("codelion.png","rb") as image:
    image_file = image.read()

image_type = imghdr.what('codelion',image_file)
message.add_attachment(image_file,maintype='image',subtype='image_type')

smtplib.SMTP.SSL(SMTP_SERVER,SMTP_PORT) -> GMAIL은 SSL을 요구함
smtp.login("이메일계정","비밀번호")
smtp.send_message()
smtp.quit()

 

 

▶ MINE

  1. 이메일을 만든다.
  2. 이메일에 내용을 담는다.
  3. 발신자, 수신자를 설정한다.

- rb = read binary / wb = write binary / ab = append binary

- add_attaachment(...) = 메일 내용안에 일반적인 텍스트가 아닌 mixed 타입의 메일일 경우 사용

  1. image
  2. maintype
  3. subtype

 

 

▶정규표현식

= 문자열에서 나타나는 특정패턴을 조건으로 주고 문자열이 적합한지 유무를 판단

codelion.example@ gmial .

^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$

 

^/$ = 시작과 끝
[a-zA-Z0-9.+_-]+ = a부터 z까지, A부터 Z까지, 0부터 9까지, ., +, _, - 가 1회 이상 반복된다.
@ = 그뒤에 @가 붙는다.
[a-zA-Z0-9]+ = a부터 z까지, A부터 Z까지, 0부터 9까지 가 1회 이상 반복된다.
\. = 그뒤에 .이 온다.
[a-zA-Z]{2,3} = a부터 z까지 A부터 Z까지가 최소 2회, 최대 3번 반복된다.
import smtplib
from email.message import EmailMessage
import imghdr -> 이미지 파일의 확장자가 변경되면 자동으로 변경될 수 있도록함
import re
SMTP_SERVER = "smtp.gmail.com"
SMTP_PORT = 465

def sendEmail(addr):
    reg = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$"
    if bool(re.match(reg,"codelion.example@gmail.com")): -> 내용이 있다면 true 없다면 none
        smtp.send_message()
        print("정상적인 이메일 주소입니다.")
    else:
         print("유효한 이메일 주소가 아닙니다."
message = EmailMessage()
message.set_content("복습중입니다.")

message["Subject"]="이것은 제목입니다." -> header에 들어감
message["From"] = "이메일계정"
message["To"] = "받는이메일계정"

with open("codelion.png","rb") as image:
    image_file = image.read()

image_type = imghdr.what('codelion',image_file)
message.add_attachment(image_file,maintype='image',subtype='image_type')

smtplib.SMTP.SSL(SMTP_SERVER,SMTP_PORT) -> GMAIL은 SSL을 요구함
smtp.login("이메일계정","비밀번호")

sendEmail("받는이메일계정")
smtp.quit()

'TIL' 카테고리의 다른 글

[TIL] 6일차_PYTHON_02  (0) 2023.02.28
[TIL] 6일차_PYTHON_01  (0) 2023.02.28
[TIL] 4일차_GoogleTranslate  (0) 2023.02.28
[TIL] 4일차_API 날씨 정보 받아오기  (0) 2023.02.28
[TIL] 4일차_실시간 검색어 Crawling  (0) 2023.02.28
Comments