missive 0.6.2

Compose, deliver, preview, and test emails in Rust - pluggable providers with zero configuration code
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Postmark API provider.
//!
//! # Example
//!
//! ```rust,ignore
//! use missive::providers::PostmarkMailer;
//!
//! let mailer = PostmarkMailer::new("xxxxx-xxxx-xxxx-xxxx-xxxxxx");
//! ```
//!
//! ## Provider Options
//!
//! Postmark-specific options can be set via `provider_option`:
//!
//! ```rust,ignore
//! let email = Email::new()
//!     .from("sender@example.com")
//!     .to("recipient@example.com")
//!     .subject("Hello")
//!     .provider_option("tag", "welcome")
//!     .provider_option("track_opens", true)
//!     .provider_option("track_links", "HtmlAndText")
//!     .provider_option("message_stream", "outbound")
//!     .provider_option("metadata", json!({"user_id": "123"}))
//!     .provider_option("inline_css", true);
//! ```
//!
//! ## Template Support
//!
//! Send emails using Postmark templates:
//!
//! ```rust,ignore
//! // Using template ID
//! let email = Email::new()
//!     .from("sender@example.com")
//!     .to("recipient@example.com")
//!     .provider_option("template_id", 12345)
//!     .provider_option("template_model", json!({
//!         "name": "John",
//!         "product": "Awesome App"
//!     }));
//!
//! // Using template alias
//! let email = Email::new()
//!     .from("sender@example.com")
//!     .to("recipient@example.com")
//!     .provider_option("template_alias", "welcome-email")
//!     .provider_option("template_model", json!({
//!         "name": "John"
//!     }));
//! ```
//!
//! ## Batch Sending
//!
//! Use `deliver_many` for batch sending (up to 500 emails per batch):
//!
//! ```rust,ignore
//! let emails = vec![email1, email2, email3];
//! let results = mailer.deliver_many(&emails).await?;
//! ```

use async_trait::async_trait;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::email::Email;
use crate::error::MailError;
use crate::mailer::{DeliveryResult, Mailer};

const POSTMARK_API_URL: &str = "https://api.postmarkapp.com";

/// Postmark API email provider.
pub struct PostmarkMailer {
    api_token: String,
    client: Client,
    base_url: String,
}

impl PostmarkMailer {
    /// Create a new Postmark mailer with the given server token.
    pub fn new(api_token: impl Into<String>) -> Self {
        Self {
            api_token: api_token.into(),
            client: Client::new(),
            base_url: POSTMARK_API_URL.to_string(),
        }
    }

    /// Create with a custom reqwest client.
    pub fn with_client(api_token: impl Into<String>, client: Client) -> Self {
        Self {
            api_token: api_token.into(),
            client,
            base_url: POSTMARK_API_URL.to_string(),
        }
    }

    /// Set a custom base URL (for testing).
    pub fn base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Check if this email uses a template.
    fn is_template_email(email: &Email) -> bool {
        email.provider_options.contains_key("template_id")
            || email.provider_options.contains_key("template_alias")
    }

    fn build_request(&self, email: &Email) -> Result<PostmarkRequest, MailError> {
        let from = email.from.as_ref().ok_or(MailError::MissingField("from"))?;

        if email.to.is_empty() {
            return Err(MailError::MissingField("to"));
        }

        let mut request = PostmarkRequest {
            from: from.formatted(),
            to: email
                .to
                .iter()
                .map(|a| a.formatted())
                .collect::<Vec<_>>()
                .join(", "),
            subject: if email.subject.is_empty() {
                None
            } else {
                Some(email.subject.clone())
            },
            html_body: email.html_body.clone(),
            text_body: email.text_body.clone(),
            cc: if email.cc.is_empty() {
                None
            } else {
                Some(
                    email
                        .cc
                        .iter()
                        .map(|a| a.formatted())
                        .collect::<Vec<_>>()
                        .join(", "),
                )
            },
            bcc: if email.bcc.is_empty() {
                None
            } else {
                Some(
                    email
                        .bcc
                        .iter()
                        .map(|a| a.formatted())
                        .collect::<Vec<_>>()
                        .join(", "),
                )
            },
            reply_to: email.reply_to.first().map(|a| a.formatted()),
            tag: None,
            track_opens: None,
            track_links: None,
            message_stream: None,
            metadata: None,
            inline_css: None,
            template_id: None,
            template_alias: None,
            template_model: None,
            headers: None,
            attachments: None,
        };

        // Add attachments
        if !email.attachments.is_empty() {
            request.attachments = Some(
                email
                    .attachments
                    .iter()
                    .map(|a| {
                        let content_id = if a.is_inline() {
                            // Postmark requires "cid:" prefix for inline attachments
                            a.content_id.as_ref().map(|cid| format!("cid:{}", cid))
                        } else {
                            None
                        };
                        PostmarkAttachment {
                            name: a.filename.clone(),
                            content: a.base64_data(),
                            content_type: a.content_type.clone(),
                            content_id,
                        }
                    })
                    .collect(),
            );
        }

        // Custom headers
        if !email.headers.is_empty() {
            request.headers = Some(
                email
                    .headers
                    .iter()
                    .map(|(name, value)| PostmarkHeader {
                        name: name.clone(),
                        value: value.clone(),
                    })
                    .collect(),
            );
        }

        // Provider-specific options
        if let Some(tag) = email.provider_options.get("tag") {
            request.tag = tag.as_str().map(|s| s.to_string());
        }
        if let Some(track_opens) = email.provider_options.get("track_opens") {
            request.track_opens = track_opens.as_bool();
        }
        if let Some(track_links) = email.provider_options.get("track_links") {
            request.track_links = track_links.as_str().map(|s| s.to_string());
        }
        if let Some(message_stream) = email.provider_options.get("message_stream") {
            request.message_stream = message_stream.as_str().map(|s| s.to_string());
        }
        if let Some(metadata) = email.provider_options.get("metadata") {
            request.metadata = Some(metadata.clone());
        }
        if let Some(inline_css) = email.provider_options.get("inline_css") {
            request.inline_css = inline_css.as_bool();
        }

        // Template options
        if let Some(template_id) = email.provider_options.get("template_id") {
            request.template_id = template_id.as_i64();
        }
        if let Some(template_alias) = email.provider_options.get("template_alias") {
            request.template_alias = template_alias.as_str().map(|s| s.to_string());
        }
        if let Some(template_model) = email.provider_options.get("template_model") {
            request.template_model = Some(template_model.clone());
        }

        Ok(request)
    }

    async fn send_request(
        &self,
        url: &str,
        body: &impl Serialize,
    ) -> Result<reqwest::Response, MailError> {
        Ok(self
            .client
            .post(url)
            .header("X-Postmark-Server-Token", &self.api_token)
            .header("Content-Type", "application/json")
            .header("Accept", "application/json")
            .header("User-Agent", format!("missive/{}", crate::VERSION))
            .json(body)
            .send()
            .await?)
    }

    fn parse_response(_status: reqwest::StatusCode, result: PostmarkResponse) -> DeliveryResult {
        DeliveryResult::with_response(
            result.message_id,
            serde_json::json!({
                "provider": "postmark",
                "submitted_at": result.submitted_at,
            }),
        )
    }

    fn parse_error(status: reqwest::StatusCode, error: PostmarkError) -> MailError {
        MailError::provider_with_status(
            "postmark",
            format!("[{}] {}", error.error_code, error.message),
            status.as_u16(),
        )
    }
}

#[async_trait]
impl Mailer for PostmarkMailer {
    async fn deliver(&self, email: &Email) -> Result<DeliveryResult, MailError> {
        let request = self.build_request(email)?;

        // Use template endpoint if template_id or template_alias is set
        let url = if Self::is_template_email(email) {
            format!("{}/email/withTemplate", self.base_url)
        } else {
            format!("{}/email", self.base_url)
        };

        let response = self.send_request(&url, &request).await?;
        let status = response.status();

        if status.is_success() {
            let result: PostmarkResponse = response.json().await?;
            Ok(Self::parse_response(status, result))
        } else {
            let error: PostmarkError = response.json().await.unwrap_or(PostmarkError {
                error_code: 0,
                message: "Unknown error".to_string(),
            });
            Err(Self::parse_error(status, error))
        }
    }

    async fn deliver_many(&self, emails: &[Email]) -> Result<Vec<DeliveryResult>, MailError> {
        if emails.is_empty() {
            return Ok(vec![]);
        }

        // Check if any emails use templates
        let has_templates = emails.iter().any(Self::is_template_email);

        // Build requests
        let requests: Vec<PostmarkRequest> = emails
            .iter()
            .map(|email| self.build_request(email))
            .collect::<Result<Vec<_>, _>>()?;

        // Use appropriate batch endpoint
        let url = if has_templates {
            format!("{}/email/batchWithTemplates", self.base_url)
        } else {
            format!("{}/email/batch", self.base_url)
        };

        // For template batch, wrap in Messages object
        let response = if has_templates {
            let batch = PostmarkTemplateBatchRequest { messages: requests };
            self.send_request(&url, &batch).await?
        } else {
            self.send_request(&url, &requests).await?
        };

        let status = response.status();

        if status.is_success() {
            let results: Vec<PostmarkBatchResponse> = response.json().await?;
            Ok(results
                .into_iter()
                .map(|r| {
                    DeliveryResult::with_response(
                        r.message_id,
                        serde_json::json!({
                            "provider": "postmark",
                            "error_code": r.error_code,
                            "message": r.message,
                            "to": r.to,
                            "submitted_at": r.submitted_at,
                        }),
                    )
                })
                .collect())
        } else {
            let error: PostmarkError = response.json().await.unwrap_or(PostmarkError {
                error_code: 0,
                message: "Unknown error".to_string(),
            });
            Err(Self::parse_error(status, error))
        }
    }

    fn provider_name(&self) -> &'static str {
        "postmark"
    }
}

// ============================================================================
// Postmark API Types
// ============================================================================

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkRequest {
    from: String,
    to: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    html_body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    text_body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cc: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    bcc: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reply_to: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tag: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    track_opens: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    track_links: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    message_stream: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    metadata: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    inline_css: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    template_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    template_alias: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    template_model: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    headers: Option<Vec<PostmarkHeader>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    attachments: Option<Vec<PostmarkAttachment>>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkHeader {
    name: String,
    value: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkAttachment {
    name: String,
    content: String, // Base64 encoded
    content_type: String,
    #[serde(rename = "ContentID", skip_serializing_if = "Option::is_none")]
    content_id: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkTemplateBatchRequest {
    messages: Vec<PostmarkRequest>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkResponse {
    #[serde(rename = "MessageID")]
    message_id: String,
    submitted_at: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkBatchResponse {
    #[serde(rename = "MessageID")]
    message_id: String,
    error_code: i32,
    message: String,
    #[serde(default)]
    to: String,
    #[serde(default)]
    submitted_at: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct PostmarkError {
    error_code: i32,
    message: String,
}