Skip to main content

example/
example.rs

1use flowmailer::{auth, Attachment, Client, MailAddress, MailBuilder};
2
3#[tokio::main]
4async fn main() {
5    static CLIENT_ID: &str = "[ID]";
6    static CLIENT_SECRET: &str = "[SECRET]";
7    static ACCOUNT_ID: &str = "[ACCOUNT ID]";
8    let auth = auth::Auth::new(
9        auth::ClientSecret::new(CLIENT_SECRET.to_string()),
10        auth::ClientId::new(CLIENT_ID.to_string()),
11        auth::AccountId::new(ACCOUNT_ID.to_string()),
12    );
13    let flowmailer = Client::new(auth);
14
15    // Example 1: Template email with attachment
16    let pdf_bytes: Vec<u8> = vec![/* PDF file bytes would go here */];
17    let mailbuilder = MailBuilder::new_template(
18        MailAddress::new("sender@example.com"),
19        MailAddress::new("receiver@example.com"),
20        "[FLOW SELECTOR]",
21    )
22    // Set variables within the template.
23    .set_data(serde_json::json!({
24        "code": "123456"
25    }))
26    .expect("set data")
27    // Set the subject of the mail.
28    .set_subject("test_flowmailer_template_mail")
29    // Add an attachment
30    .add_attachment(
31        Attachment::builder()
32            .filename("report.pdf")
33            .content_type("application/pdf")
34            .content_bytes(&pdf_bytes)
35            .build(),
36    );
37    // Sends the mail.
38    mailbuilder.send(&flowmailer).await.expect("failed to send");
39
40    // Example 2: Plain text email with multiple attachments
41    let image_bytes: Vec<u8> = vec![/* Image bytes would go here */];
42    let text_mail = MailBuilder::new_text(
43        MailAddress::new("sender@example.com"),
44        MailAddress::new("receiver@example.com"),
45        "Hello! Please find the attached files.",
46    )
47    .set_subject("Files attached")
48    .add_attachment(
49        Attachment::builder()
50            .filename("document.pdf")
51            .content_type("application/pdf")
52            .content_bytes(&pdf_bytes)
53            .build(),
54    )
55    .add_attachment(
56        Attachment::builder()
57            .filename("photo.jpg")
58            .content_type("image/jpeg")
59            .content_bytes(&image_bytes)
60            .build(),
61    );
62    text_mail.send(&flowmailer).await.expect("failed to send");
63
64    // Example 3: HTML email with inline image
65    let html_mail = MailBuilder::new_hmtl(
66        MailAddress::new("sender@example.com"),
67        MailAddress::new("receiver@example.com"),
68        r#"<html><body><h1>Hello!</h1><img src="cid:logo123"/></body></html>"#,
69    )
70    .set_subject("HTML with inline image")
71    .add_attachment(
72        Attachment::builder()
73            .filename("logo.png")
74            .content_type("image/png")
75            .content_bytes(&image_bytes)
76            .content_id("<logo123>")
77            .disposition_related() // Use "related" for inline images
78            .build(),
79    );
80    html_mail.send(&flowmailer).await.expect("failed to send");
81}