Skip to content

Commit eaa2e79

Browse files
committed
fixes #11
1 parent fbf53b5 commit eaa2e79

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

markdown_merge/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,18 @@ def smtp_connection(host, port, user=None, password=None, use_ssl=True, use_tls=
6666
# %% ../nbs/00_core.ipynb
6767
class MarkdownMerge:
6868
"Send templated email merge messages formatted with Markdown"
69-
def __init__(self, addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test=False, hdrs=None, env_from=None):
69+
def __init__(self, addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test=False, hdrs=None, env_from=None, attach=None):
7070
self.addrs,self.from_addr,self.subj,self.msg,self.i = addrs,from_addr,subj,msg,0
7171
self.inserts = [{}]*len(addrs) if inserts is None else inserts
72-
self.smtp_cfg,self.test,self.hdrs,self.env_from = smtp_cfg,test,hdrs,env_from
72+
self.smtp_cfg,self.test,self.hdrs,self.env_from,self.attach = smtp_cfg,test,hdrs,env_from,attach
7373

7474
def send_msgs(self, pause=0.2):
7575
"Send all unsent messages to `addrs` with `pause` secs between each send"
7676
conn = smtp_connection(**self.smtp_cfg)
7777
while self.i < len(self.addrs):
7878
addr,insert = self.addrs[self.i],self.inserts[self.i]
7979
msg = self.msg.format(**insert)
80-
eml = md2email(self.subj, self.from_addr, addr, md=msg, hdrs=self.hdrs)
80+
eml = md2email(self.subj, self.from_addr, addr, md=msg, hdrs=self.hdrs, attach=self.attach)
8181
if self.test: print(f"To: {addr}\n{'-'*40}\n{msg}\n{'='*40}\n")
8282
else:
8383
conn.send_message(eml, from_addr=self.env_from)

nbs/00_core.ipynb

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,18 @@
387387
"#| export\n",
388388
"class MarkdownMerge:\n",
389389
" \"Send templated email merge messages formatted with Markdown\"\n",
390-
" def __init__(self, addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test=False, hdrs=None, env_from=None):\n",
390+
" def __init__(self, addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test=False, hdrs=None, env_from=None, attach=None):\n",
391391
" self.addrs,self.from_addr,self.subj,self.msg,self.i = addrs,from_addr,subj,msg,0\n",
392392
" self.inserts = [{}]*len(addrs) if inserts is None else inserts\n",
393-
" self.smtp_cfg,self.test,self.hdrs,self.env_from = smtp_cfg,test,hdrs,env_from\n",
393+
" self.smtp_cfg,self.test,self.hdrs,self.env_from,self.attach = smtp_cfg,test,hdrs,env_from,attach\n",
394394
"\n",
395395
" def send_msgs(self, pause=0.2):\n",
396396
" \"Send all unsent messages to `addrs` with `pause` secs between each send\"\n",
397397
" conn = smtp_connection(**self.smtp_cfg)\n",
398398
" while self.i < len(self.addrs):\n",
399399
" addr,insert = self.addrs[self.i],self.inserts[self.i]\n",
400400
" msg = self.msg.format(**insert)\n",
401-
" eml = md2email(self.subj, self.from_addr, addr, md=msg, hdrs=self.hdrs)\n",
401+
" eml = md2email(self.subj, self.from_addr, addr, md=msg, hdrs=self.hdrs, attach=self.attach)\n",
402402
" if self.test: print(f\"To: {addr}\\n{'-'*40}\\n{msg}\\n{'='*40}\\n\")\n",
403403
" else:\n",
404404
" conn.send_message(eml, from_addr=self.env_from)\n",
@@ -513,15 +513,37 @@
513513
"source": [
514514
"mm.reset()"
515515
]
516+
},
517+
{
518+
"cell_type": "markdown",
519+
"id": "4cd75daa",
520+
"metadata": {},
521+
"source": [
522+
"### Attachments"
523+
]
524+
},
525+
{
526+
"cell_type": "markdown",
527+
"id": "62351144",
528+
"metadata": {},
529+
"source": [
530+
"Pass `attach` to include file attachments with your email. It can be a single file path or a list of paths:\n",
531+
"\n",
532+
"```python\n",
533+
"mm = MarkdownMerge(addrs, from_addr, 'Subject', msg, smtp_cfg=smtp_cfg, attach='report.pdf')\n",
534+
"```\n",
535+
"\n",
536+
"For multiple attachments:\n",
537+
"\n",
538+
"```python\n",
539+
"mm = MarkdownMerge(addrs, from_addr, 'Subject', msg, smtp_cfg=smtp_cfg, attach=['report.pdf', 'data.csv'])\n",
540+
"```\n",
541+
"\n",
542+
"The MIME type is automatically detected from the file extension. If the type cannot be determined, it defaults to `application/octet-stream`."
543+
]
516544
}
517545
],
518-
"metadata": {
519-
"kernelspec": {
520-
"display_name": "python3",
521-
"language": "python",
522-
"name": "python3"
523-
}
524-
},
546+
"metadata": {},
525547
"nbformat": 4,
526548
"nbformat_minor": 5
527549
}

nbs/index.ipynb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@
128128
"The `test=True` parameter prints the messages instead of sending them."
129129
]
130130
},
131+
{
132+
"cell_type": "markdown",
133+
"id": "d223e6e5",
134+
"metadata": {},
135+
"source": [
136+
"Pass `attach` to include file attachments with your email. It can be a single file path or a list of paths:\n",
137+
"\n",
138+
"```python\n",
139+
"ml = MarkdownMerge(to_addrs, from_addr, 'Subject', msg, smtp_cfg=smtp_cfg, attach='report.pdf')\n",
140+
"```"
141+
]
142+
},
131143
{
132144
"cell_type": "markdown",
133145
"id": "52d9e79f",

0 commit comments

Comments
 (0)