Skip to content

Commit

Permalink
Merge pull request #11 from idanre1/master
Browse files Browse the repository at this point in the history
python 3 version
  • Loading branch information
nfultz authored Jan 4, 2023
2 parents 0264f28 + c84e987 commit d1a17d4
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ To generate a mail and send it later with another process (eg `sendmail`):

jupyter nbconvert --execute --to mail notebook.ipynb

### SMTP Example
To convert and send a mail via gmail, you can set the environment
variables and declare a postprocessor with `--post`:

Expand All @@ -43,6 +44,10 @@ and then run:

jupyter nbconvert --config config.py demo.ipynb

### 3rd party email distributor Example
Instead of using SMTP to send emails, one can use 3rd party provider.
This is an example for using [mailgun](examples/mailgun.ipynb) as a 3rd party provider

## Configuring Mail Headers

In the notebook metadata, you can set mail headers by adding a `nb2mail` block:
Expand Down
172 changes: 172 additions & 0 deletions examples/mailgun.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working example\n",
"Convert jupyter notebook to email message and mail it with mailgun"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert jupyter to .mail file\n",
"1. `jupyter nbconvert --to mail test.ipynb`\n",
"2. run this notebook"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mailgun credentials\n",
"```python\n",
"{\n",
" \"From\":\"from mail example\",\n",
" \"To\" : \"to mail example\",\n",
" \"MAILGUN_URL\" : \"mailgun sending url\",\n",
" \"MAILGUN_KEY\" : \"key secret\"\n",
"}\n",
"```"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add addresses to mime message "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from email import message_from_file\n",
"from email.mime.message import MIMEMessage\n",
"import json\n",
"def mail_prep(subject, filename, cred='cred_mail.json'):\n",
"\t'''\n",
"\tAdd subject\n",
"\tsubject : subject of the mail thread\n",
"\tfilename : .mail filename (already exists mime filename)\n",
"\tcred (dict): mailgun credentials file\n",
"\t'''\n",
"\tcred_dict = json.load(open(cred))\n",
"\n",
"\tmsg = MIMEMessage(message_from_file(open(filename)))\n",
"\tmsg['Subject'] = subject\n",
"\tmsg['From'] = cred_dict['From']\n",
"\tmsg['To'] = cred_dict['To']\n",
"\t\n",
"\t# filename='outgoing.msg'\n",
"\t# with open(filename, 'wb') as f:\n",
"\t# \tf.write(bytes(msg))\n",
"\treturn msg"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"subject='testing 12345678'\n",
"modified_msg=mail_prep(subject, 'mail/test.mail')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Send mailgun raw mime message"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"\n",
"def mailgun_send(msg, cred='cred_mail.json'):\n",
"\t'''\n",
"\tfilename : abc.msg email filename\n",
"\tcred (dict): mailgun credentials file\n",
"\t'''\n",
"\tcred_dict = json.load(open(cred))\n",
"\n",
"\treturn requests.post(\n",
"\t\tf\"{cred_dict['MAILGUN_URL']}.mime\",\n",
"\t\tauth=(\"api\", cred_dict['MAILGUN_KEY']),\n",
"\t\tdata={\"to\": cred_dict['To']},\n",
"\t\tfiles={\"message\": bytes(msg)}\n",
"\t)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<Response [200]>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mailgun_send(modified_msg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "py3env",
"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.10.6"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "0f130d28ea552d45a6da6317af870e1a4a314464082fb72b4db9d63620c1bd57"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
11 changes: 9 additions & 2 deletions nb2mail/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __init__(self, config=None, **kw):
config : config
User configuration instance.
"""
if sys.version_info[0] == 3:
# https://stackoverflow.com/questions/27376907/how-to-detect-python-version-2-or-3-in-script
self.template_file=os.path.join(os.path.dirname(__file__), "templates", "mail.tpl")
super(MailExporter, self).__init__(config=config, **kw)
self.register_filter('basename_attach', basename_attach)
self.register_filter('data_attach', data_attach)
Expand All @@ -72,9 +75,13 @@ def _file_extension_default(self):

@default('template_file')
def _template_file_default(self):
return 'mail.tpl'
return "mail.tpl"

output_mimetype = 'multipart/mixed'
if sys.version_info[0] == 2:
# https://stackoverflow.com/questions/27376907/how-to-detect-python-version-2-or-3-in-script
output_mimetype = 'multipart/mixed'
else:
output_mimetype = ''

@default('raw_mimetypes')
def _raw_mimetypes_default(self):
Expand Down

0 comments on commit d1a17d4

Please sign in to comment.