Skip to content

Commit

Permalink
Fix mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-oleshkevich committed Aug 13, 2021
1 parent 2fc7e8d commit 934cbe5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ message = EmailMessage(
bcc=['bcc@example.com'],
text_body='Hello world!',
html_body='<b>Hello world!</b>',
)
```

`cc`, `bcc`, `to`, `reply_to` can be either strings or lists of strings.

## Attachments

Use `attach_file` and `attach_content` to add attachments. Also, you can use `Attachment` class for more control.

```python
from mailers import EmailMessage, Attachment

message = EmailMessage(
to='user@localhost',
from_address='from@example.tld',
text_body='Hello world!',
attachments=[
Attachment('CONTENTS', 'file.txt', 'text/plain'),
]
Expand All @@ -66,11 +82,30 @@ message = EmailMessage(
# attachments can be added on demand:
await message.attach_file(path='file.txt')

# attach using a class
message.add_attachment(Attachment('CONTENTS', 'file.txt', 'text/plain'))

# or you may pass attachment contents directory
message.attach_content(file_name='file.txt', content='HERE GO ATTACHMENT CONTENTS', mime_type='text/plain')
```

`cc`, `bcc`, `to`, `reply_to` can be either strings or lists of strings.
## Inline attachments

You can add inline attachments (eg. images) and then reference them in HTML. For that, set `inline=True` and
specify `content_id=SOMEUNIQID` arguments in `attach_*` functions. Then you can reference images in HTML part like
that `<img src="cid:SOMEUNIQID">"`.

```python
from mailers import EmailMessage, Attachment

message = EmailMessage(
to='user@localhost',
from_address='from@example.tld',
html_body='Render me <img src="cid:img1">',
)

await message.attach_file(path='/path/to/image.png', inline=True, content_id='img1')
```

## DKIM signing

Expand Down
17 changes: 7 additions & 10 deletions mailers/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import aiosmtplib
import datetime
import os
import ssl
import sys
import typing as t
from email.message import Message
from typing import Any, List, Optional, Union
from typing import Any, List, Union

from .config import EmailURL

Expand Down Expand Up @@ -109,24 +108,23 @@ def __init__(
self,
host: str = "localhost",
port: int = 25,
user: Optional[str] = None,
password: Optional[str] = None,
use_tls: Optional[bool] = None,
user: str = None,
password: str = None,
use_tls: bool = None,
timeout: int = 10,
key_file: Optional[str] = None,
cert_file: Optional[str] = None,
key_file: str = None,
cert_file: str = None,
):
self._host = host
self._user = user
self._port = port
self._password = password
self._use_tls = use_tls
self._use_tls = use_tls or False
self._timeout = timeout
self._key_file = key_file
self._cert_file = cert_file

async def send(self, message: Message) -> None:
context = ssl.create_default_context()
await aiosmtplib.send(
message,
hostname=self._host,
Expand All @@ -135,7 +133,6 @@ async def send(self, message: Message) -> None:
username=self._user,
password=self._password,
timeout=self._timeout,
tls_context=context,
client_key=self._key_file,
client_cert=self._cert_file,
)
Expand Down

0 comments on commit 934cbe5

Please sign in to comment.