|
387 | 387 | "#| export\n", |
388 | 388 | "class MarkdownMerge:\n", |
389 | 389 | " \"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", |
391 | 391 | " self.addrs,self.from_addr,self.subj,self.msg,self.i = addrs,from_addr,subj,msg,0\n", |
392 | 392 | " 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", |
394 | 394 | "\n", |
395 | 395 | " def send_msgs(self, pause=0.2):\n", |
396 | 396 | " \"Send all unsent messages to `addrs` with `pause` secs between each send\"\n", |
397 | 397 | " conn = smtp_connection(**self.smtp_cfg)\n", |
398 | 398 | " while self.i < len(self.addrs):\n", |
399 | 399 | " addr,insert = self.addrs[self.i],self.inserts[self.i]\n", |
400 | 400 | " 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", |
402 | 402 | " if self.test: print(f\"To: {addr}\\n{'-'*40}\\n{msg}\\n{'='*40}\\n\")\n", |
403 | 403 | " else:\n", |
404 | 404 | " conn.send_message(eml, from_addr=self.env_from)\n", |
|
513 | 513 | "source": [ |
514 | 514 | "mm.reset()" |
515 | 515 | ] |
| 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 | + ] |
516 | 544 | } |
517 | 545 | ], |
518 | | - "metadata": { |
519 | | - "kernelspec": { |
520 | | - "display_name": "python3", |
521 | | - "language": "python", |
522 | | - "name": "python3" |
523 | | - } |
524 | | - }, |
| 546 | + "metadata": {}, |
525 | 547 | "nbformat": 4, |
526 | 548 | "nbformat_minor": 5 |
527 | 549 | } |
0 commit comments