azure_functions/send_grid/
attachment.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents an email attachment.
4#[derive(Debug, Default, Clone, Serialize, Deserialize)]
5pub struct Attachment {
6    /// The Base64 encoded content of the attachment.
7    pub content: String,
8    /// The mime type of the attachment (e.g. "image/jpeg").
9    #[serde(rename = "type")]
10    pub mime_type: String,
11    /// The filename of the attachment.
12    pub filename: String,
13    /// The content-disposition of the attachment specifying how you would like the attachment to be displayed.
14    ///
15    /// Supported values are "attachment" or "inline".  Defaults to "attachment".
16    ///
17    /// For example, "inline" results in the attached file being displayed automatically within the message,
18    /// while "attachment" results in the attached file requiring some action to be taken before it is displayed (e.g. opening or downloading the file).
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub disposition: Option<String>,
21    /// The attachment's unique content identifier.
22    ///
23    /// This is used when the disposition is set to "inline" and the attachment is an image, allowing the file to be displayed within the body of your email.
24    ///
25    /// ```html
26    /// <img src="cid:ii_139db99fdb5c3704"></img>
27    /// ```
28    #[serde(skip_serializing_if = "Option::is_none")]
29    pub content_id: Option<String>,
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use serde_json::to_string;
36
37    #[test]
38    fn it_serializes_to_json() {
39        let json = to_string(&Attachment {
40            content: "aGVsbG8gd29ybGQ=".to_owned(),
41            mime_type: "text/plain".to_owned(),
42            filename: "foo.txt".to_owned(),
43            disposition: None,
44            content_id: None,
45        })
46        .unwrap();
47
48        assert_eq!(
49            json,
50            r#"{"content":"aGVsbG8gd29ybGQ=","type":"text/plain","filename":"foo.txt"}"#
51        );
52
53        let json = to_string(&Attachment {
54            content: "aGVsbG8gd29ybGQ=".to_owned(),
55            mime_type: "text/plain".to_owned(),
56            filename: "foo.txt".to_owned(),
57            disposition: Some("inline".to_owned()),
58            content_id: Some("123456".to_owned()),
59        })
60        .unwrap();
61
62        assert_eq!(
63            json,
64            r#"{"content":"aGVsbG8gd29ybGQ=","type":"text/plain","filename":"foo.txt","disposition":"inline","content_id":"123456"}"#
65        );
66    }
67}