From 8795a830dd2e2b91cc18ce094a383604cfe4597c Mon Sep 17 00:00:00 2001 From: jaehwang Date: Thu, 27 Nov 2025 11:14:43 +0900 Subject: [PATCH] smtp poc --- .gitignore | 1 + smtp.ipynb | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 smtp.ipynb diff --git a/.gitignore b/.gitignore index 7e144ff..26f1f2f 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,4 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +data/ \ No newline at end of file diff --git a/smtp.ipynb b/smtp.ipynb new file mode 100644 index 0000000..c8e58ef --- /dev/null +++ b/smtp.ipynb @@ -0,0 +1,115 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "fd42a4ef-69dd-47ab-b6bb-4fdf7587609c", + "metadata": {}, + "outputs": [], + "source": [ + "import smtplib\n", + "from email import encoders\n", + "from email.mime.base import MIMEBase\n", + "from email.mime.multipart import MIMEMultipart\n", + "from email.mime.text import MIMEText\n", + "from email.encoders import encode_base64\n", + "import os\n", + "from dotenv import load_dotenv" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "5ea6bafe-e7ed-421e-94c6-599c77543f8c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "load_dotenv()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "fb91db25-baa0-48c0-b34d-2c3c5d19cb29", + "metadata": {}, + "outputs": [], + "source": [ + "SMTP_SERVER = os.getenv(\"SMTP_SERVER\") # 네이버 smtp, 보내는 서버 기준 smtp 도메인 확인\n", + "SMTP_USER_ID = os.getenv(\"SMTP_USER_ID\") # 송신자 ID / 현재는 SMTP 서버 로그인 + 송신자\n", + "SMTP_USER_PW = os.getenv(\"SMTP_USER_PW\") # 송신자 PW / 네이버의 경우 SMTP전용 PW 발급\n", + "SMTP_PORT = os.getenv(\"SMTP_PORT\")# SMTP PORT, 기본 587" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "e524d0c9-3b73-4c3a-9d0a-b095b57bfdcf", + "metadata": {}, + "outputs": [], + "source": [ + "TARGET_FILE_PATH = \"c:/o2o/smtp-mail-send-test/data/테스트피디엪.pdf\" # 파일 경로\n", + "file_name = \"테스트 pdf.pdf\" # 첨부파일명\n", + "part = MIMEBase('application', \"octet-stream\")\n", + "part.set_payload(open(TARGET_FILE_PATH,\"rb\").read())\n", + "encode_base64(part)\n", + "part.add_header('Content-Disposition', 'attachment', filename=file_name)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6bea233d-d0ad-4765-bdc6-4d66b4a6d9a7", + "metadata": {}, + "outputs": [], + "source": [ + "reciever = \"jhyeu@o2o.kr\" # 수신자\n", + "with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as smtp:\n", + " smtp.starttls()\n", + " smtp.login(SMTP_USER_ID, SMTP_USER_PW)\n", + " content = \"This is an automated message being sent by Python. Python is the mastermind behind this.\"\n", + " msg = MIMEMultipart()\n", + " msg['From'] = SMTP_USER_ID\n", + " msg['Subject'] = '메일 발송 시험'\n", + " msg['To'] = reciever\n", + " \n", + " body = MIMEText(content, _charset='utf-8')\n", + " msg.attach(body)\n", + " msg.attach(part)\n", + " smtp.sendmail(SMTP_USER_ID, reciever, msg.as_string())\n", + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}