-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from idanre1/master
python 3 version
- Loading branch information
Showing
3 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters