Skip to content

Commit 15e8014

Browse files
committed
first pass mailgun
1 parent a29912e commit 15e8014

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

emailer_lib/egress.py

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22
import base64
3+
import os
34

45
from email.mime.multipart import MIMEMultipart
56
from email.mime.text import MIMEText
@@ -164,24 +165,100 @@ def send_intermediate_email_with_yagmail(i_email: IntermediateEmail):
164165
raise NotImplementedError
165166

166167

167-
def send_intermediate_email_with_mailgun(i_email: IntermediateEmail):
168+
def send_intermediate_email_with_mailgun(
169+
api_key: str,
170+
domain: str,
171+
sender: str,
172+
i_email: IntermediateEmail,
173+
):
168174
"""
169175
Send an Intermediate Email object via Mailgun.
170176
171177
Parameters
172178
----------
179+
api_key
180+
Mailgun API key (found in account settings)
181+
domain
182+
Your verified Mailgun domain (e.g., "mg.yourdomain.com")
183+
sender
184+
Email address to send from (must be authorized in your domain)
173185
i_email
174186
IntermediateEmail object containing the email content and attachments
175187
176188
Returns
177189
-------
178-
None
190+
Response
191+
Response from Mailgun API
192+
193+
Raises
194+
------
195+
Exception
196+
If the Mailgun API returns an error
179197
198+
Examples
199+
--------
200+
```python
201+
email = IntermediateEmail(
202+
html="<p>Hello world</p>",
203+
subject="Test Email",
204+
recipients=["[email protected]"],
205+
)
206+
207+
response = send_intermediate_email_with_mailgun(
208+
api_key="your-api-key",
209+
domain="mg.yourdomain.com",
210+
211+
i_email=email
212+
)
213+
```
214+
180215
Notes
181216
-----
182-
This function is a placeholder and has not been implemented yet.
217+
Requires the `mailgun` package: `pip install mailgun`
183218
"""
184-
raise NotImplementedError
219+
from mailgun.client import Client
220+
221+
# Create Mailgun client
222+
client = Client(auth=("api", api_key))
223+
224+
# Prepare the basic email data
225+
data = {
226+
"from": sender,
227+
"to": i_email.recipients,
228+
"subject": i_email.subject,
229+
"html": i_email.html,
230+
}
231+
232+
# Add text content if available
233+
if i_email.text:
234+
data["text"] = i_email.text
235+
236+
# Prepare files for attachments
237+
files = []
238+
239+
# Handle inline images (embedded in HTML with cid:)
240+
for image_name, image_base64 in i_email.inline_attachments.items():
241+
img_bytes = base64.b64decode(image_base64)
242+
# Use 'inline' for images referenced in HTML with cid:
243+
files.append(("inline", (image_name, img_bytes)))
244+
245+
# Handle external attachments
246+
for filename in i_email.external_attachments:
247+
with open(filename, "rb") as f:
248+
file_data = f.read()
249+
250+
# Extract just the filename (not full path) for the attachment name
251+
basename = os.path.basename(filename)
252+
files.append(("attachment", (basename, file_data)))
253+
254+
# Send the message using Mailgun client
255+
response = client.messages.create(
256+
data=data,
257+
files=files if files else None,
258+
domain=domain
259+
)
260+
261+
return response
185262

186263

187264
def send_intermediate_email_with_smtp(

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ dev = [
3232
"griffe",
3333
]
3434

35+
mailgun = [
36+
"mailgun"
37+
]
38+
3539
docs = [
3640
"redmail>=0.6.0",
3741
"jupyter",

0 commit comments

Comments
 (0)