flows_connector_dsi/
gmail.rs

1pub struct OutboundData {
2    fields: String,
3    content: String,
4}
5
6impl OutboundData {
7    /// Set content of email, this field is required.
8    pub fn content<S: Into<String>>(mut self, content: S) -> OutboundData {
9        self.content = content.into();
10        self
11    }
12
13    /// Set sender of email, this field is optional.
14    pub fn sender<S: Into<String>>(mut self, sender: S) -> OutboundData {
15        self.fields
16            .push_str(&format!("Sender: {}\r\n", sender.into()));
17        self
18    }
19
20    /// Set subject of email, this field is optional.
21    pub fn subject<S: Into<String>>(mut self, subject: S) -> OutboundData {
22        self.fields
23            .push_str(&format!("Subject: {}\r\n", subject.into()));
24        self
25    }
26
27    /// Build outbound data in `RFC 2822` form.
28    pub fn build(self) -> Result<String, String> {
29        if self.content.is_empty() {
30            return Err("OutboundData build failed: Content is empty".to_string());
31        }
32
33        Ok(format!(
34            "{}\r\n{}",
35            self.fields,
36            self.content.replace("\n", "\r\n")
37        ))
38    }
39}
40
41/// Send an email via GMail.
42///
43/// ```rust
44/// outbound("ho-229@example.com")
45///     .subject("Hi")
46///     .sender("ho-229")
47///     .content("Hello world!")
48///     .build()
49/// ```
50pub fn outbound<S: Into<String>>(email: S) -> OutboundData {
51    OutboundData {
52        fields: format!("From: me\r\nTo: {}\r\n", email.into()),
53        content: String::new(),
54    }
55}