Files
jupyter/smtp_send.ipynb
2025-08-22 00:25:39 +08:00

135 lines
4.6 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "0cbbf99b-5a86-47fa-828c-0e85b271b215",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import smtplib\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87c5df80-c9f8-4fe8-a9a0-41e91fe22fe6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# need to recall when edited\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "544fc052-7e2d-4434-a168-74c3c32739a9",
"metadata": {},
"outputs": [],
"source": [
"smtp_server = os.getenv(\"SMTP_SRV\")\n",
"smtp_port = os.getenv(\"SMTP_PORT\")\n",
"smtp_user = os.getenv(\"SMTP_USER\")\n",
"smtp_pass = os.getenv(\"SMTP_PASS\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "9109a375-d6b7-4ebf-b955-1447a26f2cf7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(235, b'2.7.0 Authentication successful')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"server = smtplib.SMTP(smtp_server, smtp_port)\n",
"server.starttls()\n",
"server.login(smtp_user, smtp_pass)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1c2fb0f0-b18b-44af-a65c-0ff001e48114",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"email to: cheeloychang@gmail.com\n",
"email from: spareclass@spareclass.com\n"
]
},
{
"ename": "SMTPDataError",
"evalue": "(550, b'5.7.0 From address is not one of your addresses')",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mSMTPDataError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[5]\u001b[39m\u001b[32m, line 9\u001b[39m\n\u001b[32m 5\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33memail from: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00memail_from\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m 8\u001b[39m \u001b[38;5;66;03m# senders email, recipients email, and email message.\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m9\u001b[39m \u001b[43mserver\u001b[49m\u001b[43m.\u001b[49m\u001b[43msendmail\u001b[49m\u001b[43m(\u001b[49m\u001b[43memail_from\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43memail_to\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mThis is a test email sent from Python\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[36mFile \u001b[39m\u001b[32m~/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/smtplib.py:897\u001b[39m, in \u001b[36mSMTP.sendmail\u001b[39m\u001b[34m(self, from_addr, to_addrs, msg, mail_options, rcpt_options)\u001b[39m\n\u001b[32m 895\u001b[39m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[32m 896\u001b[39m \u001b[38;5;28mself\u001b[39m._rset()\n\u001b[32m--> \u001b[39m\u001b[32m897\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m SMTPDataError(code, resp)\n\u001b[32m 898\u001b[39m \u001b[38;5;66;03m#if we got here then somebody got our mail\u001b[39;00m\n\u001b[32m 899\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m senderrs\n",
"\u001b[31mSMTPDataError\u001b[39m: (550, b'5.7.0 From address is not one of your addresses')"
]
}
],
"source": [
"email_to = os.getenv(\"EMAIL_TO\")\n",
"email_from = os.getenv(\"EMAIL_FROM\")\n",
"\n",
"print(f\"email to: {email_to}\")\n",
"print(f\"email from: {email_from}\")\n",
"\n",
"\n",
"# senders email, recipients email, and email message.\n",
"server.sendmail(email_from, email_to, \"This is a test email sent from Python\")"
]
}
],
"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.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}