---
title: MarkdownMerge
description: API details
---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

```python
from fastcore.test import test_eq
```

## Utility functions

[source](https://github.com/AnswerDotAI/markdown_merge/blob/master/markdown_merge/core.py#L27)

### get\_addr

```python
def get_addr(
    email, name=None
):
```

*Convert `email` and optional `name` into an email `Address` object*

Specify `from_addr` and `to_addrs` as either a string, or an `Address` object (created with [`get_addr`](core.md#get_addr)). Note the `to_addrs` is a list.

```python
from_addr = get_addr('from@example.com', 'Jeremy Howard')
to_addrs = [get_addr('to@example.com', 'Jeręmy Hòwärd')]
print(from_addr)
print(to_addrs[0])
```

```output
Jeremy Howard <from@example.com>
Jeręmy Hòwärd <to@example.com>
```

[source](https://github.com/AnswerDotAI/markdown_merge/blob/master/markdown_merge/core.py#L32)

### attach\_file

```python
def attach_file(
    msg, f
):
```

*Attach file `f` to message `msg`*

```python
msg = MIMEMultipart(policy=EmailPolicy())
attach_file(msg, '../LICENSE')
part = msg.get_payload()[0]
test_eq(part.get_content_type(), 'application/octet-stream')
test_eq(part['Content-Disposition'], 'attachment; filename=LICENSE')
assert len(part.get_payload())>20
```

[source](https://github.com/AnswerDotAI/markdown_merge/blob/master/markdown_merge/core.py#L43)

### create\_multipart\_msg

```python
def create_multipart_msg(
    subj, from_addr, to_addrs, md=None, html=None, attach=None, hdrs=None
):
```

*Create a multipart email with markdown text and HTML*

```python
msg = create_multipart_msg('Test Subject', from_addr, to_addrs, hdrs={'atest':'foo'},
                           md='**Bold text**', html='<b>Bold text</b>')
test_eq(msg['Subject'], 'Test Subject')
test_eq(msg['From'], str(from_addr))
test_eq(msg['atest'], 'foo')
test_eq(len(msg.get_payload()), 2)
test_eq(msg.get_payload()[0].get_content_type(), 'text/plain')
test_eq(msg.get_payload()[1].get_content_type(), 'text/html')
assert '@' in msg['To']
```

[source](https://github.com/AnswerDotAI/markdown_merge/blob/master/markdown_merge/core.py#L56)

### md2email

```python
def md2email(
    subj, from_addr, to_addrs, md, attach=None, hdrs=None
):
```

*Create a multipart email from markdown*

```python
test_msg2 = md2email('Test md2email', 'support@answer.ai', 'j@answer.ai', '**Markdown** test with _attachment_', attach='../pyproject.toml')
test_eq(test_msg2['Subject'], 'Test md2email')
payload = test_msg2.get_payload()
test_eq(len(payload), 3)
test_eq(payload[2]['Content-Disposition'], 'attachment; filename=pyproject.toml')
```

The basic email body is the plain text message (note that the template variables in `{}` will be filled in by [`MarkdownMerge`](core.md#markdownmerge)):

```python
print(payload[0].get_payload())
```

```output
**Markdown** test with _attachment_
```

Most email software is set up to display the HTML version:

```python
from IPython.display import HTML
```

```python
html = payload[1].get_payload()
HTML(html)
```

```python
att = payload[2].get_payload()
```

```python
import base64
```

```python
decoded = base64.b64decode(payload[2].get_payload())
print(decoded.decode('utf-8')[:35])
```

```output
[build-system]
requires = ["setupto
```

[source](https://github.com/AnswerDotAI/markdown_merge/blob/master/markdown_merge/core.py#L62)

### smtp\_connection

```python
def smtp_connection(
    host, port, user=None, password=None, use_ssl:bool=True, use_tls:bool=False
):
```

*Create and return an SMTP connection*

```python
servernm='email-smtp.us-west-2.amazonaws.com'
username=os.getenv('SES_SMTP_USER')
password=os.getenv('SES_SMTP_PASS')
```

```python
smtp_cfg = dict(host=servernm, port=587, user=username, password=password, use_ssl=False, use_tls=True)
test_msg = create_multipart_msg('Test from stdlib', 'support@answer.ai', 'j@answer.ai', md='**Test message**', html='<b>Test message</b>')
# try:
#     conn = smtp_connection(**smtp_cfg)
#     conn.send_message(test_msg)
# finally: conn.quit()
```

[source](https://github.com/AnswerDotAI/markdown_merge/blob/master/markdown_merge/core.py#L70)

### MarkdownMerge

```python
def MarkdownMerge(
    addrs, from_addr, subj, msg, smtp_cfg=None, inserts=None, test:bool=False, hdrs=None, env_from=None, attach=None
):
```

*Send templated email merge messages formatted with Markdown*

Your message should be in [markdown](https://daringfireball.net/projects/markdown/syntax) format. It will be converted into a two part email, containing both a plain text and an HTML part, so recipients will see whatever format they're set as their preference for viewing mail. Anything in curly brackets `{}` will be replaced with the contents of the inserts dictionary for that address. If there are no bracketed variables to replace, then you don't need to pass any inserts.

```python
msg = "**Hello {name}!**\n\nYour special number is: *{num}*"
```

`inserts` is a list of dictionaries. For each dictionary, the keys should match the bracketed names in your email template, and the values will be filled in to those sections.

```python
inserts = [{'name': 'Jeremy', 'num': 42}, {'name': 'Rachel', 'num': 7}]
```

```python
mm = MarkdownMerge(['aaa@answer.ai', 'bbb@answer.ai'], 'from@answer.ai', 'Test merge',
                   msg, smtp_cfg=smtp_cfg, inserts=inserts, test=True)
```

```python
mm.send_msgs()
```

```output
To: aaa@answer.ai
----------------------------------------
**Hello Jeremy!**

Your special number is: *42*
========================================

To: bbb@answer.ai
----------------------------------------
**Hello Rachel!**

Your special number is: *7*
========================================
```

Use `pause` to avoid sending too many messages too quickly; many SMTP servers restrict sending speed to avoid abuse. If you get an error during sending (e.g. "too many messages"), then wait an hour or so, then continue sending, using a larger `pause` value.

**NB**: You can just call `send_msgs` again when resending, since the successfully sent message count is saved, and those messages are not re-sent (unless you call `reset`). This includes test sends, therefore you should run reset after a test send.

To reset the counter to `0`, call `reset`:

```python
mm.reset()
```

### Attachments

Pass `attach` to include file attachments with your email. It can be a single file path or a list of paths:

```python
mm = MarkdownMerge(addrs, from_addr, 'Subject', msg, smtp_cfg=smtp_cfg, attach='report.pdf')
```

For multiple attachments:

```python
mm = MarkdownMerge(addrs, from_addr, 'Subject', msg, smtp_cfg=smtp_cfg, attach=['report.pdf', 'data.csv'])
```

The MIME type is automatically detected from the file extension. If the type cannot be determined, it defaults to `application/octet-stream`.
