From 622c31868ef53eb9297b41b90304b4b8009e042a Mon Sep 17 00:00:00 2001 From: Jongkook Choi Date: Fri, 26 May 2023 13:35:26 +0900 Subject: [PATCH 1/2] Added commandline options for general SMTP --- README.md | 8 ++++++-- nb2mail/__init__.py | 14 ++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5e21155..13e5f44 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ send it via smtp. ## Installation pip install nb2mail + pip install git+https://github.com/Thessal/nb2mail.git ## Usage @@ -15,6 +16,8 @@ send it via smtp. ("mail") and postprocessor ("SendMailPostProcessor"). Please see the nbconvert documentation and example configuration for more information. +NOTE: BOTH sender and recipient email identities have to be verified to use AWS SES SMTP + ## Example To generate a mail and send it later with another process (eg `sendmail`): @@ -24,8 +27,9 @@ To generate a mail and send it later with another process (eg `sendmail`): ### SMTP Example To convert and send a mail via gmail, you can set the environment variables and declare a postprocessor with `--post`: - - export TO=example@example.ex GMAIL_USER=user GMAIL_PASS="*****" + export FROM=sender@domain.abc TO=receipent@domain.def + export SMTP_ADDR=email-smtp.region.amazonaws.com SMTP_PORT=587 + export SMTP_USER=aws_ses_smtp_auth SMTP_PASS=`echo $SMTP_HASH | base64 -d` jupyter nbconvert --to mail --post=nb2mail.SendMailPostProcessor notebook.ipynb Alternatively, you can configure the SMTP settings in a config file `config.py`: diff --git a/nb2mail/__init__.py b/nb2mail/__init__.py index 61a8807..7a3a513 100644 --- a/nb2mail/__init__.py +++ b/nb2mail/__init__.py @@ -154,12 +154,14 @@ def from_notebook_node(self, nb, resources=None, **kw): class SendMailPostProcessor(PostProcessorBase): + sender = Unicode(os.getenv("FROM", ''), help="Sender address").tag(config=True) recipient = Unicode(os.getenv("TO", ''), help="Recipient address").tag(config=True) - smtp_user = Unicode(os.getenv("GMAIL_USER", ''), help="SMTP User" ).tag(config=True) - smtp_pass = Unicode(os.getenv("GMAIL_PASS", ''), help="SMTP pass" ).tag(config=True) - smtp_addr = Unicode("smtp.gmail.com", help="SMTP addr" ).tag(config=True) - smtp_port = Int(587, help="SMTP port" ).tag(config=True) - + smtp_user = Unicode(os.getenv("SMTP_USER", ''), help="SMTP User" ).tag(config=True) + smtp_pass = Unicode(os.getenv("SMTP_PASS", ''), help="SMTP pass" ).tag(config=True) + smtp_addr = Unicode(os.getenv("SMTP_ADDR", ''), help="SMTP addr" ).tag(config=True) + smtp_port = Int(os.getenv("SMTP_PORT", '587'), help="SMTP port" ).tag(config=True) + smtp_port = smtp_port.default_value + def postprocess(self, input): " Heavily borrowed from https://www.mkyong.com/python/how-do-send-email-in-python-via-smtplib/ " smtpserver = smtplib.SMTP(self.smtp_addr,self.smtp_port) @@ -180,6 +182,6 @@ def postprocess(self, input): # Set To header from config email['To'] = self.recipient - smtpserver.sendmail(self.smtp_user, self.recipient.split(','), email.as_string()) + smtpserver.sendmail(self.sender, self.recipient.split(','), email.as_string()) smtpserver.close() From 22e7d41c7456771421b9b4a6bb376dc9de140b12 Mon Sep 17 00:00:00 2001 From: Choi Jongkook Date: Fri, 26 May 2023 13:40:34 +0900 Subject: [PATCH 2/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 13e5f44..fce9d97 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ send it via smtp. ## Installation - pip install nb2mail pip install git+https://github.com/Thessal/nb2mail.git ## Usage @@ -27,6 +26,7 @@ To generate a mail and send it later with another process (eg `sendmail`): ### SMTP Example To convert and send a mail via gmail, you can set the environment variables and declare a postprocessor with `--post`: + export FROM=sender@domain.abc TO=receipent@domain.def export SMTP_ADDR=email-smtp.region.amazonaws.com SMTP_PORT=587 export SMTP_USER=aws_ses_smtp_auth SMTP_PASS=`echo $SMTP_HASH | base64 -d`