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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! Brevo API provider (formerly Sendinblue).
//!
//! For reference: [Brevo API docs](https://developers.brevo.com/reference/sendtransacemail)
//!
//! # Example
//!
//! ```rust,ignore
//! use missive::providers::BrevoMailer;
//!
//! let mailer = BrevoMailer::new("your-api-key");
//! ```
//!
//! ## Provider Options
//!
//! Brevo-specific options can be set via `provider_option`:
//!
//! ```rust,ignore
//! use chrono::{Utc, Duration};
//!
//! let email = Email::new()
//!     .from("sender@example.com")
//!     .to("recipient@example.com")
//!     .subject("Hello")
//!     .provider_option("sender_id", 42)
//!     .provider_option("template_id", 123)
//!     .provider_option("params", json!({"name": "John", "order_id": 456}))
//!     .provider_option("tags", vec!["welcome", "onboarding"])
//!     .provider_option("schedule_at", (Utc::now() + Duration::hours(1)).to_rfc3339());
//! ```
//!
//! ## Provider Options Reference
//!
//! * `sender_id` (integer) - Use a sender ID instead of email address
//! * `template_id` (integer) - ID of the active transactional email template
//! * `params` (map) - Key/value attributes to customize the template
//! * `tags` (list[string]) - Tags for filtering in Brevo dashboard
//! * `schedule_at` (string) - RFC3339 UTC datetime to schedule the email
//!
//! ## Using Template Default Sender
//!
//! When using a template, you can omit the sender and use the template's
//! default sender by setting the from email to "TEMPLATE":
//!
//! ```rust,ignore
//! let email = Email::new()
//!     .from(("", "TEMPLATE"))  // Uses template's default sender
//!     .to("recipient@example.com")
//!     .provider_option("template_id", 123);
//! ```

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

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

const BREVO_BASE_URL: &str = "https://api.brevo.com/v3";
const BREVO_API_ENDPOINT: &str = "/smtp/email";

/// Brevo API email provider.
pub struct BrevoMailer {
    api_key: String,
    base_url: String,
    client: Client,
}

impl BrevoMailer {
    /// Create a new Brevo mailer with the given API key.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: BREVO_BASE_URL.to_string(),
            client: Client::new(),
        }
    }

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

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

    fn build_request(&self, email: &Email) -> Result<BrevoRequest, 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 = BrevoRequest {
            sender: prepare_sender(from, email),
            to: email.to.iter().map(prepare_recipient).collect(),
            cc: if email.cc.is_empty() {
                None
            } else {
                Some(email.cc.iter().map(prepare_recipient).collect())
            },
            bcc: if email.bcc.is_empty() {
                None
            } else {
                Some(email.bcc.iter().map(prepare_recipient).collect())
            },
            reply_to: email.reply_to.first().map(prepare_recipient),
            subject: if email.subject.is_empty() {
                None
            } else {
                Some(email.subject.clone())
            },
            text_content: email.text_body.clone(),
            html_content: email.html_body.clone(),
            template_id: None,
            headers: if email.headers.is_empty() {
                None
            } else {
                Some(email.headers.clone())
            },
            params: None,
            tags: None,
            attachment: None,
            scheduled_at: None,
        };

        // Provider-specific options
        if let Some(template_id) = email.provider_options.get("template_id") {
            request.template_id = template_id.as_i64();
        }
        if let Some(params) = email.provider_options.get("params") {
            if let Some(obj) = params.as_object() {
                request.params = Some(obj.clone().into_iter().collect());
            }
        }
        if let Some(tags) = email.provider_options.get("tags") {
            request.tags = serde_json::from_value(tags.clone()).ok();
        }
        if let Some(schedule_at) = email.provider_options.get("schedule_at") {
            request.scheduled_at = schedule_at.as_str().map(|s| s.to_string());
        }

        // Add attachments
        if !email.attachments.is_empty() {
            request.attachment = Some(
                email
                    .attachments
                    .iter()
                    .map(|a| BrevoAttachment {
                        name: a.filename.clone(),
                        content: a.base64_data(),
                    })
                    .collect(),
            );
        }

        Ok(request)
    }
}

/// Check if sender should use template default (email is "TEMPLATE").
fn is_template_sender(from: &crate::Address) -> bool {
    from.email == "TEMPLATE"
}

fn prepare_sender(from: &crate::Address, email: &Email) -> Option<BrevoSender> {
    // When from email is "TEMPLATE", don't send sender - use template default
    if is_template_sender(from) {
        return None;
    }

    // Check for sender_id provider option
    if let Some(sender_id) = email.provider_options.get("sender_id") {
        if let Some(id) = sender_id.as_i64() {
            return Some(BrevoSender {
                id: Some(id),
                email: Some(from.email.clone()),
                name: None,
            });
        }
    }

    Some(BrevoSender {
        id: None,
        email: Some(from.email.clone()),
        name: from.name.clone(),
    })
}

fn prepare_recipient(addr: &crate::Address) -> BrevoRecipient {
    BrevoRecipient {
        email: addr.email.clone(),
        name: addr.name.clone(),
    }
}

#[async_trait]
impl Mailer for BrevoMailer {
    async fn deliver(&self, email: &Email) -> Result<DeliveryResult, MailError> {
        let request = self.build_request(email)?;
        let url = format!("{}{}", self.base_url, BREVO_API_ENDPOINT);

        let response = self
            .client
            .post(&url)
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .header("User-Agent", format!("missive/{}", crate::VERSION))
            .header("Api-Key", &self.api_key)
            .json(&request)
            .send()
            .await?;

        let status = response.status();

        if status.is_success() {
            let result: BrevoResponse = response.json().await?;
            Ok(DeliveryResult::with_response(
                result.message_id,
                serde_json::json!({ "provider": "brevo" }),
            ))
        } else {
            let error: BrevoError = response.json().await.unwrap_or(BrevoError {
                code: "unknown".to_string(),
                message: "Unknown error".to_string(),
            });
            Err(MailError::provider_with_status(
                "brevo",
                format!("[{}] {}", error.code, error.message),
                status.as_u16(),
            ))
        }
    }

    /// Send multiple emails in a single API call using Brevo's messageVersions.
    ///
    /// Global parameters (from first email): sender, attachments, tags, scheduled_at
    /// Per-email parameters: to, cc, bcc, subject, content, template_id, params, headers, reply_to
    async fn deliver_many(&self, emails: &[Email]) -> Result<Vec<DeliveryResult>, MailError> {
        if emails.is_empty() {
            return Ok(vec![]);
        }

        let first_email = &emails[0];
        let from = first_email
            .from
            .as_ref()
            .ok_or(MailError::MissingField("from"))?;

        // Build batch request with messageVersions
        let batch_request = BrevoBatchRequest {
            sender: prepare_sender(from, first_email),
            subject: if first_email.subject.is_empty() {
                None
            } else {
                Some(first_email.subject.clone())
            },
            text_content: first_email.text_body.clone(),
            html_content: first_email.html_body.clone(),
            template_id: first_email
                .provider_options
                .get("template_id")
                .and_then(|v| v.as_i64()),
            tags: first_email
                .provider_options
                .get("tags")
                .and_then(|v| serde_json::from_value(v.clone()).ok()),
            attachment: if first_email.attachments.is_empty() {
                None
            } else {
                Some(
                    first_email
                        .attachments
                        .iter()
                        .map(|a| BrevoAttachment {
                            name: a.filename.clone(),
                            content: a.base64_data(),
                        })
                        .collect(),
                )
            },
            scheduled_at: first_email
                .provider_options
                .get("schedule_at")
                .and_then(|v| v.as_str().map(|s| s.to_string())),
            message_versions: emails.iter().map(prepare_message_version).collect(),
        };

        // If first email has no subject but template_id, that's fine
        if batch_request.subject.is_none() && batch_request.template_id.is_none() {
            // Check if any email has a subject
            if !emails.iter().any(|e| !e.subject.is_empty()) {
                return Err(MailError::MissingField("subject"));
            }
        }

        let url = format!("{}{}", self.base_url, BREVO_API_ENDPOINT);

        let response = self
            .client
            .post(&url)
            .header("Accept", "application/json")
            .header("Content-Type", "application/json")
            .header("User-Agent", format!("missive/{}", crate::VERSION))
            .header("Api-Key", &self.api_key)
            .json(&batch_request)
            .send()
            .await?;

        let status = response.status();

        if status.is_success() {
            let result: BrevoBatchResponse = response.json().await?;
            Ok(result
                .message_ids
                .into_iter()
                .map(|id| {
                    DeliveryResult::with_response(id, serde_json::json!({ "provider": "brevo" }))
                })
                .collect())
        } else {
            let error: BrevoError = response.json().await.unwrap_or(BrevoError {
                code: "unknown".to_string(),
                message: "Unknown error".to_string(),
            });
            Err(MailError::provider_with_status(
                "brevo",
                format!("[{}] {}", error.code, error.message),
                status.as_u16(),
            ))
        }
    }

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

fn prepare_message_version(email: &Email) -> BrevoMessageVersion {
    BrevoMessageVersion {
        to: email.to.iter().map(prepare_recipient).collect(),
        cc: if email.cc.is_empty() {
            None
        } else {
            Some(email.cc.iter().map(prepare_recipient).collect())
        },
        bcc: if email.bcc.is_empty() {
            None
        } else {
            Some(email.bcc.iter().map(prepare_recipient).collect())
        },
        reply_to: email.reply_to.first().map(prepare_recipient),
        subject: if email.subject.is_empty() {
            None
        } else {
            Some(email.subject.clone())
        },
        text_content: email.text_body.clone(),
        html_content: email.html_body.clone(),
        template_id: email
            .provider_options
            .get("template_id")
            .and_then(|v| v.as_i64()),
        headers: if email.headers.is_empty() {
            None
        } else {
            Some(email.headers.clone())
        },
        params: email
            .provider_options
            .get("params")
            .and_then(|v| v.as_object().map(|obj| obj.clone().into_iter().collect())),
    }
}

// ============================================================================
// Brevo API Types
// ============================================================================

#[derive(Debug, Serialize)]
struct BrevoSender {
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

#[derive(Debug, Serialize)]
struct BrevoRecipient {
    email: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct BrevoRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    sender: Option<BrevoSender>,
    to: Vec<BrevoRecipient>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cc: Option<Vec<BrevoRecipient>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    bcc: Option<Vec<BrevoRecipient>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reply_to: Option<BrevoRecipient>,
    #[serde(skip_serializing_if = "Option::is_none")]
    subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    text_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    html_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    template_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    headers: Option<std::collections::HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    params: Option<serde_json::Map<String, serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    attachment: Option<Vec<BrevoAttachment>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    scheduled_at: Option<String>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct BrevoBatchRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    sender: Option<BrevoSender>,
    #[serde(skip_serializing_if = "Option::is_none")]
    subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    text_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    html_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    template_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    attachment: Option<Vec<BrevoAttachment>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    scheduled_at: Option<String>,
    message_versions: Vec<BrevoMessageVersion>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct BrevoMessageVersion {
    to: Vec<BrevoRecipient>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cc: Option<Vec<BrevoRecipient>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    bcc: Option<Vec<BrevoRecipient>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reply_to: Option<BrevoRecipient>,
    #[serde(skip_serializing_if = "Option::is_none")]
    subject: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    text_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    html_content: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    template_id: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    headers: Option<std::collections::HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    params: Option<serde_json::Map<String, serde_json::Value>>,
}

#[derive(Debug, Serialize)]
struct BrevoAttachment {
    name: String,
    content: String, // Base64 encoded
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct BrevoResponse {
    message_id: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct BrevoBatchResponse {
    #[serde(default)]
    message_ids: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct BrevoError {
    code: String,
    message: String,
}