markdown-merge 0.1.4__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of markdown-merge might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markdown_merge
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: Send email using markdown
5
5
  Home-page: https://github.com/AnswerDotAI/markdown_merge
6
6
  Author: Jeremy Howard
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.6"
2
+ from .core import *
@@ -12,11 +12,13 @@ from fastcore.utils import *
12
12
  from email.mime.multipart import MIMEMultipart
13
13
  from email.mime.text import MIMEText
14
14
  from email.mime.base import MIMEBase
15
+ from email.policy import EmailPolicy
15
16
  from email import encoders
16
17
 
17
18
  from contextlib import contextmanager
18
19
  from markdown import markdown
19
20
  from email.headerregistry import Address
21
+ from email.header import Header
20
22
  from time import sleep
21
23
 
22
24
  # %% ../nbs/00_core.ipynb
@@ -36,10 +38,11 @@ def attach_file(msg, f):
36
38
  msg.attach(part)
37
39
 
38
40
  # %% ../nbs/00_core.ipynb
39
- def create_multipart_msg(subj, from_addr, to_addrs, md=None, html=None, attach=None):
41
+ def create_multipart_msg(subj, from_addr, to_addrs, md=None, html=None, attach=None, hdrs=None):
40
42
  "Create a multipart email with markdown text and HTML"
41
- msg = MIMEMultipart('alternative')
43
+ msg = MIMEMultipart('alternative', policy=EmailPolicy())
42
44
  msg['Subject'],msg['From'] = subj,str(from_addr)
45
+ for k,v in (hdrs or {}).items(): msg[k]=v
43
46
  msg['To'] = ', '.join([str(a) for a in listify(to_addrs)])
44
47
  if md: msg.attach(MIMEText(md, 'plain'))
45
48
  if html: msg.attach(MIMEText(html, 'html'))
@@ -47,39 +50,43 @@ def create_multipart_msg(subj, from_addr, to_addrs, md=None, html=None, attach=N
47
50
  return msg
48
51
 
49
52
  # %% ../nbs/00_core.ipynb
50
- def md2email(subj, from_addr, to_addrs, md, attach=None):
53
+ def md2email(subj, from_addr, to_addrs, md, attach=None, hdrs=None):
51
54
  "Create a multipart email from markdown"
52
55
  html = markdown(md)
53
- return create_multipart_msg(subj, from_addr, to_addrs, md=md, html=html, attach=attach)
56
+ return create_multipart_msg(subj, from_addr, to_addrs, md=md, html=html, attach=attach, hdrs=hdrs)
54
57
 
55
58
  # %% ../nbs/00_core.ipynb
56
- @contextmanager
57
59
  def smtp_connection(host, port, user=None, password=None, use_ssl=True, use_tls=False):
58
- "Context manager for SMTP connection"
60
+ "Create and return an SMTP connection"
59
61
  conn = smtplib.SMTP_SSL(host, port) if use_ssl else smtplib.SMTP(host, port)
60
62
  if use_tls and not use_ssl: conn.starttls()
61
63
  if user and password: conn.login(user, password)
62
- try: yield conn
63
- finally: conn.quit()
64
+ return conn
64
65
 
65
66
  # %% ../nbs/00_core.ipynb
66
67
  class MarkdownMerge:
67
68
  "Send templated email merge messages formatted with Markdown"
68
- def __init__(self, addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test=False):
69
+ def __init__(self, addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test=False, hdrs=None):
69
70
  self.addrs,self.from_addr,self.subj,self.msg,self.i = addrs,from_addr,subj,msg,0
70
71
  self.inserts = [{}]*len(addrs) if inserts is None else inserts
71
- self.smtp_cfg,self.test = smtp_cfg,test
72
+ self.smtp_cfg,self.test,self.hdrs = smtp_cfg,test,hdrs
72
73
 
73
- def send_msgs(self, pause=0.5):
74
+ def send_msgs(self, pause=0.2):
74
75
  "Send all unsent messages to `addrs` with `pause` secs between each send"
75
- with smtp_connection(**self.smtp_cfg) as conn:
76
- while self.i < len(self.addrs):
77
- addr,insert = self.addrs[self.i],self.inserts[self.i]
78
- msg = self.msg.format(**insert)
79
- eml = md2email(self.subj, self.from_addr, addr, md=msg)
80
- if self.test: print(f"To: {addr}\n{'-'*40}\n{msg}\n{'='*40}\n")
81
- else: conn.send_message(eml); sleep(pause)
82
- self.i += 1
83
- if self.i%100==0: print(self.i)
76
+ conn = smtp_connection(**self.smtp_cfg)
77
+ while self.i < len(self.addrs):
78
+ addr,insert = self.addrs[self.i],self.inserts[self.i]
79
+ msg = self.msg.format(**insert)
80
+ eml = md2email(self.subj, self.from_addr, addr, md=msg, hdrs=self.hdrs)
81
+ if self.test: print(f"To: {addr}\n{'-'*40}\n{msg}\n{'='*40}\n")
82
+ else:
83
+ conn.send_message(eml)
84
+ sleep(pause)
85
+ self.i += 1
86
+ if self.i%100==0:
87
+ print(self.i)
88
+ conn.quit()
89
+ conn = smtp_connection(**self.smtp_cfg)
90
+ conn.quit()
84
91
 
85
92
  def reset(self): self.i=0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markdown_merge
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: Send email using markdown
5
5
  Home-page: https://github.com/AnswerDotAI/markdown_merge
6
6
  Author: Jeremy Howard
@@ -1,7 +1,7 @@
1
1
  [DEFAULT]
2
2
  repo = markdown_merge
3
3
  lib_name = markdown_merge
4
- version = 0.1.4
4
+ version = 0.1.6
5
5
  min_python = 3.10
6
6
  license = apache2
7
7
  black_formatting = False
@@ -1,2 +0,0 @@
1
- __version__ = "0.1.4"
2
- from .core import *
File without changes
File without changes
File without changes
File without changes