Skip to content

Commit

Permalink
Make from email option on transactional emails, closed #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeckennedy committed Jan 29, 2024
1 parent a833b42 commit 4da1e06
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
listmonk.delete_subscriber(subscriber.email)

to_email = 'SUBSCRIBER_EMAIL_ON_YOUR_LIST'
from_email = 'APPROVED_OUTBOUND_EMAIL_ON_DOMAIN'
from_email = 'APPROVED_OUTBOUND_EMAIL_ON_DOMAIN' # Optional
template_id = 3 # *Transactional* template ID from your listmonk instance.
template_data = {'order_id': 1772, 'shipping_date': 'Next week'}
if to_email != 'SUBSCRIBER_EMAIL_ON_YOUR_LIST':
Expand Down
14 changes: 9 additions & 5 deletions listmonk/impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from listmonk import models, urls # noqa: F401

__version__ = '0.1.5'
__version__ = '0.1.6'

from listmonk.errors import ValidationError, OperationNotAllowedError

Expand Down Expand Up @@ -513,14 +513,15 @@ def block_subscriber(subscriber: models.Subscriber) -> models.Subscriber:

# region def delete_subscriber(email: Optional[str] = None, overriding_subscriber_id: Optional[int] = None) -> bool

def send_transactional_email(subscriber_email: str, template_id: int, from_email: str, template_data: dict,
def send_transactional_email(subscriber_email: str, template_id: int,
from_email: Optional[str] = None, template_data: Optional[dict] = None,
messenger_channel: str = 'email', content_type: str = 'markdown') -> bool:
"""
Send a transactional email through Listmonk to the recipient.
Args:
subscriber_email: The email address to send the email to (they must be a subscriber of *some* list on your server).
template_id: The template ID to use for the email. It must be a "transactional" not campaign template.
from_email: The from address for the email. Must be a valid from address on your email sending provider (e.g. SendGrid).
from_email: The from address for the email. Can be omitted to use default email at your output provider.
template_data: A dictionary of merge parameters for the template, available in the template as {{ .Tx.Data.* }}.
messenger_channel: Default is "email", if you have SMS or some other channel, you can use it here.
content_type: Email format options include html, markdown, and plain.
Expand All @@ -534,12 +535,15 @@ def send_transactional_email(subscriber_email: str, template_id: int, from_email

body_data = {
'subscriber_email': subscriber_email,
'from_email': from_email,
'template_id': template_id,
'data': template_data,
'data': template_data or {},
'messenger': messenger_channel,
'content_type': content_type,
}

if from_email is not None:
body_data['from_email'] = from_email

try:
url = f"{url_base}{urls.send_tx}"
resp = httpx.post(url, json=body_data, headers=core_headers, follow_redirects=True)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies = [
"httpx",
"pydantic",
]
version = "0.1.5"
version = "0.1.6"


[project.urls]
Expand Down

0 comments on commit 4da1e06

Please sign in to comment.