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

4.6 KiB
Raw Blame History

In [1]:
import os
import smtplib
from dotenv import load_dotenv
In [2]:
# need to recall when edited
load_dotenv()
Out[2]:
True
In [3]:
smtp_server = os.getenv("SMTP_SRV")
smtp_port = os.getenv("SMTP_PORT")
smtp_user = os.getenv("SMTP_USER")
smtp_pass = os.getenv("SMTP_PASS")
In [4]:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(smtp_user, smtp_pass)
Out[4]:
(235, b'2.7.0 Authentication successful')
In [5]:
email_to = os.getenv("EMAIL_TO")
email_from = os.getenv("EMAIL_FROM")

print(f"email to: {email_to}")
print(f"email from: {email_from}")


# senders email, recipients email, and email message.
server.sendmail(email_from, email_to, "This is a test email sent from Python")
email to: cheeloychang@gmail.com
email from: spareclass@spareclass.com
---------------------------------------------------------------------------
SMTPDataError                             Traceback (most recent call last)
Cell In[5], line 9
      5 print(f"email from: {email_from}")
      8 # senders email, recipients email, and email message.
----> 9 server.sendmail(email_from, email_to, "This is a test email sent from Python")

File ~/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/smtplib.py:897, in SMTP.sendmail(self, from_addr, to_addrs, msg, mail_options, rcpt_options)
    895     else:
    896         self._rset()
--> 897     raise SMTPDataError(code, resp)
    898 #if we got here then somebody got our mail
    899 return senderrs

SMTPDataError: (550, b'5.7.0 From address is not one of your addresses')