sendgrid_async/sendgrid/
message.rs

1use data_encoding::BASE64;
2use serde::Serialize;
3/// The main structure for a V3 API mail send call. This is composed of many other smaller
4/// structures used to add lots of customization to your message.
5#[derive(Default, Serialize)]
6pub struct Message {
7    from: Address,
8    subject: String,
9    personalizations: Vec<super::Personalization>,
10
11    #[serde(skip_serializing_if = "Option::is_none")]
12    content: Option<Vec<Content>>,
13
14    #[serde(skip_serializing_if = "Option::is_none")]
15    attachments: Option<Vec<Attachment>>,
16
17    #[serde(skip_serializing_if = "Option::is_none")]
18    template_id: Option<String>,
19}
20
21impl Message {
22    /// Construct a new V3 message.
23    pub fn new() -> Message {
24        Message::default()
25    }
26
27    /// Set the from address.
28    pub fn set_from(mut self, from: Address) -> Message {
29        self.from = from;
30        self
31    }
32
33    /// Set the subject.
34    pub fn set_subject(mut self, subject: &str) -> Message {
35        self.subject = String::from(subject);
36        self
37    }
38
39    /// Set the template id.
40    pub fn set_template_id(mut self, template_id: &str) -> Message {
41        self.template_id = Some(String::from(template_id));
42        self
43    }
44
45    /// Add content to the message.
46    pub fn add_content(mut self, c: Content) -> Message {
47        match self.content {
48            None => {
49                let mut content = Vec::new();
50                content.push(c);
51                self.content = Some(content);
52            }
53            Some(ref mut content) => content.push(c),
54        };
55        self
56    }
57
58    /// Add a personalization to the message.
59    pub fn add_personalization(mut self, p: super::Personalization) -> Message {
60        self.personalizations.push(p);
61        self
62    }
63
64    /// Add an attachment to the message.
65    pub fn add_attachment(mut self, a: Attachment) -> Message {
66        match self.attachments {
67            None => {
68                let mut attachments = Vec::new();
69                attachments.push(a);
70                self.attachments = Some(attachments);
71            }
72            Some(ref mut attachments) => attachments.push(a),
73        };
74        self
75    }
76}
77/// An email with a required address and an optional name field.
78#[derive(Clone, Default, Serialize)]
79pub struct Address {
80    email: String,
81
82    #[serde(skip_serializing_if = "Option::is_none")]
83    name: Option<String>,
84}
85
86impl Address {
87    /// Construct a new email type.
88    pub fn new() -> Address {
89        Address::default()
90    }
91
92    /// Set the address for this email.
93    pub fn set_email(mut self, email: &str) -> Address {
94        self.email = String::from(email);
95        self
96    }
97
98    /// Set an optional name.
99    pub fn set_name(mut self, name: &str) -> Address {
100        self.name = Some(String::from(name));
101        self
102    }
103}
104
105/// The body of an email with the content type and the message.
106#[derive(Clone, Default, Serialize)]
107pub struct Content {
108    #[serde(rename = "type")]
109    content_type: String,
110    value: String,
111}
112
113impl Content {
114    /// Construct a new content type.
115    pub fn new() -> Content {
116        Content::default()
117    }
118
119    /// Set the type of this content.
120    pub fn set_content_type(mut self, content_type: &str) -> Content {
121        self.content_type = String::from(content_type);
122        self
123    }
124
125    /// Set the corresponding message for this content.
126    pub fn set_value(mut self, value: &str) -> Content {
127        self.value = String::from(value);
128        self
129    }
130}
131
132/// An attachment block for a V3 message. Content and filename are required. If the
133/// mime_type is unspecified, the email will use Sendgrid's default for attachments
134/// which is 'application/octet-stream'.
135#[derive(Default, Serialize)]
136pub struct Attachment {
137    content: String,
138
139    filename: String,
140
141    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
142    mime_type: Option<String>,
143
144    #[serde(skip_serializing_if = "Option::is_none")]
145    disposition: Option<String>,
146
147    #[serde(skip_serializing_if = "Option::is_none")]
148    content_id: Option<String>,
149}
150
151impl Attachment {
152    /// Construct a new attachment for this message.
153    pub fn new() -> Attachment {
154        Attachment::default()
155    }
156
157    /// The raw body of the attachment.
158    pub fn set_content(mut self, c: &[u8]) -> Attachment {
159        self.content = BASE64.encode(c);
160        self
161    }
162
163    /// The base64 body of the attachment.
164    pub fn set_base64_content(mut self, c: &str) -> Attachment {
165        self.content = String::from(c);
166        self
167    }
168
169    /// Sets the filename for the attachment.
170    pub fn set_filename(mut self, filename: &str) -> Attachment {
171        self.filename = filename.into();
172        self
173    }
174
175    /// Set an optional mime type. Sendgrid will default to 'application/octet-stream'
176    /// if unspecified.
177    pub fn set_mime_type(mut self, mime: &str) -> Attachment {
178        self.mime_type = Some(String::from(mime));
179        self
180    }
181}