SMTP Relay 전송 - Pringo SMTP 인터페이스를 이용한 이메일 전송 - Python
- 작성일2024/04/29 20:50
- 조회 3,517
Pringo SMTP 인터페이스를 이용한 SMTP Relay 메일 전송 - Python
다음 절차는 Python을 사용하여 Pringo SMTP Relay서버로 이메일을 보내는 방법입니다.
시작하기 전에
다음 사항을 준비하세요.
- Python 설치 - Python은 여기에서 다운로드할 수 있습니다.
- Python SMTP 라이브러리 설치 - 다음 명령을 실행하여 설치할 수 있습니다:
pip install smtplib
Pringo SMTP를 사용하여 이메일 보내기
다음 Python 코드를 사용하여 Pringo SMTP 서버를 통해 이메일을 보낼 수 있습니다:
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # 이메일 보내기 함수 정의 def send_email(): # SMTP 서버 설정 smtp_server = "smtp.pringoTest.com" # 샘플 주소 이며, 실제주소와 다릅니다. smtp_port = 587 # smtp_user = "your_username" # 단독서버로 IP인증으로 가능함 # smtp_password = "your_password" # 단독서버로 IP인증으로 가능함 # 이메일 설정 from_email = "sender@example.com" to_email = "recipient@example.com" subject = "Pringo SMTP 테스트 이메일" body = """
Pringo SMTP Relay서버 테스트 이메일 전송
이 이메일은 Pringo SMTP 인터페이스를 사용하여 보냈습니다.
import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText def send_email(subject, body, to_email, from_email, smtp_server, smtp_port, login, password): # Create a multipart message msg = MIMEMultipart() msg['From'] = from_email msg['To'] = to_email msg['Subject'] = subject # Attach the message body msg.attach(MIMEText(body, 'plain')) try: # Set up the SMTP server server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # Secure the connection server.login(login, password) # Send the email server.send_message(msg) server.quit() print(f"Email sent successfully to {to_email}") except Exception as e: print(f"Failed to send email. Error: {e}") if __name__ == "__main__": subject = "Test Email" body = "This is a test email sent from Python." to_email = "recipient@example.com" from_email = "your_email@gmail.com" smtp_server = "smtp.gmail.com" smtp_port = 587 login = "your_email@gmail.com" password = "your_email_password" send_email(subject, body, to_email, from_email, smtp_server, smtp_port, login, password)
python send_email.py
""" # 이메일 메시지 작성 msg = MIMEMultipart() msg['From'] = from_email msg['To'] = to_email msg['Subject'] = subject msg.attach(MIMEText(body, 'html')) # SMTP 서버에 연결하고 이메일 보내기 try: server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() server.login(smtp_user, smtp_password) server.sendmail(from_email, to_email, msg.as_string()) server.quit() print("이메일이 성공적으로 전송되었습니다!") except Exception as e: print(f"이메일 전송에 실패하였습니다. 오류: {e}") # 함수 호출하여 이메일 보내기 send_email()
코드 설명
- SMTP 서버 설정: Pringo SMTP 서버 주소와 포트 번호, 사용자 이름, 비밀번호를 설정합니다.
- 이메일 설정: 발신자 이메일 주소, 수신자 이메일 주소, 이메일 제목 및 본문을 설정합니다. 본문은 HTML 형식으로 작성됩니다.
- 이메일 메시지 작성: MIME 멀티파트 메시지를 생성하여 이메일 본문을 추가합니다.
- SMTP 서버에 연결하고 이메일 보내기: SMTP 서버에 연결한 후 로그인하고, 이메일을 전송합니다. 연결 종료 후 성공 또는 실패 메시지를 출력합니다.
위 단계를 따라 Pringo SMTP 인터페이스를 통해 Python으로 이메일을 전송할 수 있습니다. 추가 도움이 필요하면 Pringo 지원팀에 문의하십시오.