logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

Python email 보내는 예제 코드

by digipine posted Aug 27, 2024
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

Python에서 이메일을 보내기 위해서는 smtplib 모듈을 사용하면 됩니다. 이 모듈은 SMTP (Simple Mail Transfer Protocol)를 통해 이메일을 보낼 수 있는 기능을 제공합니다. 아래는 Gmail SMTP 서버를 사용하여 이메일을 보내는 예제 코드입니다.

 

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# 이메일 보내기 함수
def send_email(sender_email, receiver_email, subject, body, smtp_server, smtp_port, sender_password):
    # MIMEMultipart 객체 생성
    msg = MIMEMultipart()
    msg['From'] = sender_email
    msg['To'] = receiver_email
    msg['Subject'] = subject

    # 이메일 본문 추가
    msg.attach(MIMEText(body, 'plain'))

    try:
        # SMTP 서버에 연결
        server = smtplib.SMTP(smtp_server, smtp_port)
        server.starttls()  # TLS(Transport Layer Security) 시작
        server.login(sender_email, sender_password)  # 로그인

        # 이메일 전송
        server.send_message(msg)
        print("Email sent successfully!")

    except Exception as e:
        print(f"Failed to send email: {e}")

    finally:
        server.quit()  # 서버 연결 종료

# 메인 실행 로직
if __name__ == "__main__":
    sender_email = "your_email@gmail.com"       # 발신자 이메일 주소
    receiver_email = "receiver_email@example.com"  # 수신자 이메일 주소
    subject = "Test Email from Python"            # 이메일 제목
    body = "This is a test email sent from a Python script."  # 이메일 본문

    # Gmail SMTP 서버 설정
    smtp_server = "smtp.gmail.com"
    smtp_port = 587  # TLS 포트 번호

    # 발신자 이메일 계정 비밀번호 (또는 앱 비밀번호)
    sender_password = "your_password"

    # 이메일 전송 함수 호출
    send_email(sender_email, receiver_email, subject, body, smtp_server, smtp_port, sender_password)

주요 구성 요소 설명:

  1. smtplib: 이메일을 보내기 위해 사용되는 Python 모듈입니다. SMTP 서버에 연결하여 이메일을 전송할 수 있습니다.
  2. email.mime.multipart.MIMEMultipart: 멀티파트 이메일 메시지를 구성하는 객체입니다. 이메일의 다양한 부분 (예: 본문, 첨부 파일)을 조합할 수 있습니다.
  3. email.mime.text.MIMEText: 이메일 본문을 텍스트로 작성하기 위해 사용됩니다.

코드 실행 방법:

  1. 발신자 이메일 계정 정보 입력:

    • sender_email: 발신자의 이메일 주소.
    • sender_password: 발신자 이메일 계정의 비밀번호 또는 앱 비밀번호(Gmail의 경우 앱 비밀번호를 사용하는 것을 권장).
  2. 수신자 이메일 주소 입력: receiver_email 변수에 수신자의 이메일 주소를 입력합니다.

  3. 이메일 서버 정보 설정:

    • smtp_server: 이메일을 보낼 SMTP 서버 주소(Gmail의 경우 smtp.gmail.com).
    • smtp_port: SMTP 서버 포트 번호(Gmail의 경우 587).
  4. 코드 실행: 이 코드를 실행하면 지정된 이메일 주소로 메일이 전송됩니다.

참고:

  • Gmail을 사용하려면 "보안 수준이 낮은 앱의 액세스"를 허용하거나, 2단계 인증을 설정한 후 앱 비밀번호를 생성하여 sender_password에 사용해야 합니다.
  • 회사나 다른 이메일 제공자를 사용하는 경우 해당 SMTP 서버 및 포트를 확인해야 합니다.
TAG •

  1. No Image 20Jan
    by lizard2019
    2025/01/20 by lizard2019
    Views 84 

    rmix test application 배포

  2. No Image 05Nov
    by lizard2019
    2024/11/05 by lizard2019
    Views 538 

    윈도우에서 패스워드 입력 실패로 잠금 상태인지 확인 하는 방법

  3. No Image 30Aug
    by digipine
    2024/08/30 by digipine
    Views 502 

    OpenSSL Build for Windows

  4. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 991 

    Rapid JSON 간단 사용법

  5. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 782 

    Direct X 11에서 그래픽 카드의 정보 가져오는 예제

  6. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 805 

    Python Slack 메시지 발송하는 예제

  7. No Image 27Aug
    by digipine
    2024/08/27 by digipine
    Views 614 

    Python email 보내는 예제 코드

  8. No Image 08Aug
    by digipine
    2024/08/08 by digipine
    Views 501 

    UDP 핀홀 트래버설 과정 요약, UDP pinhole traversal

  9. No Image 08Aug
    by digipine
    2024/08/08 by digipine
    Views 515 

    NAT 상태에서 P2P 통신하는 방법

  10. Windows Visual Studio 2022 OpenSSL Build 방법

  11. No Image 28Mar
    by digipine
    2024/03/28 by digipine
    Views 732 

    Visual Studio 단축키 정리

  12. 프로그래밍 언어 순위 2023년

  13. No Image 11Aug
    by digipine
    2023/08/11 by digipine
    Views 839 

    이벤트 텍소노미(Event Taxonomy)란 무엇인가요?

  14. Bitbucket에서 SSH 키 등록하고 사용하는 방법 (맥/리눅스)

  15. No Image 12May
    by digipine
    2023/05/12 by digipine
    Views 852 

    FFServer RTSP Audio Server Config

  16. No Image 15Feb
    by lizard2019
    2023/02/15 by lizard2019
    Views 867 

    OBS Studio for Http Interface EXE

  17. No Image 06Oct
    by digipine
    2022/10/06 by digipine
    Views 1496 

    xcode xib encountered an error communicating with ibagent-ios 해결

  18. No Image 25Sep
    by lizard2019
    2022/09/25 by lizard2019
    Views 1357 

    XCode 사용시 git ignore 로 xcuserstate 충돌 해결하기, .gitignore에 등록했는데도 동작안할때 해결방법

  19. No Image 05Sep
    by digipine
    2022/09/05 by digipine
    Views 1408 

    MAC Screen Sharing을 위한 VNC 접속을 위한 Port 변경 방법

  20. No Image 26Jan
    by digipine
    2022/01/26 by digipine
    Views 1679 

    Phabricator Ubuntu Installation Guide

Board Pagination Prev 1 2 3 4 5 6 Next
/ 6

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5