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
use errors::SendgridResult;

use std::collections::HashMap;

use reqwest::{Client, StatusCode};
use reqwest::header::{Authorization, Bearer, ContentType, Headers, UserAgent};

use data_encoding::base64;

use serde_json;

const V3_API_URL: &'static str = "https://api.sendgrid.com/v3/mail/send";

/// Just a redefinition of a map to store string keys and values.
pub type SGMap = HashMap<String, String>;

/// Used to send a V3 message body.
pub struct V3Sender {
    api_key: String,
}

/// The main structure for a V3 API mail send call. This is composed of many other smaller
/// structures used to add lots of customization to your message.
#[derive(Serialize)]
pub struct SGMailV3 {
    from: Email,
    subject: String,
    content: Vec<Content>,
    personalizations: Vec<Personalization>,

    #[serde(skip_serializing_if = "check_encode")]
    attachments: Option<Vec<Attachment>>,
}

/// An email with a required address and an optional name field.
#[derive(Clone, Serialize)]
pub struct Email {
    email: String,

    #[serde(skip_serializing_if = "check_encode")]
    name: Option<String>,
}

/// The body of an email with the content type and the message.
#[derive(Clone, Serialize)]
pub struct Content {
    #[serde(rename = "type")]
    content_type: String,
    value: String,
}

/// A personalization block for a V3 message. It has to at least contain one email as a to
/// address. All other fields are optional.
#[derive(Serialize)]
pub struct Personalization {
    to: Vec<Email>,

    #[serde(skip_serializing_if = "check_encode")]
    cc: Option<Vec<Email>>,

    #[serde(skip_serializing_if = "check_encode")]
    bcc: Option<Vec<Email>>,

    #[serde(skip_serializing_if = "check_encode")]
    subject: Option<String>,

    #[serde(skip_serializing_if = "check_encode")]
    headers: Option<SGMap>,

    #[serde(skip_serializing_if = "check_encode")]
    substitutions: Option<SGMap>,

    #[serde(skip_serializing_if = "check_encode")]
    custom_args: Option<SGMap>,

    #[serde(skip_serializing_if = "check_encode")]
    send_at: Option<u64>,
}

/// An attachment block for a V3 message. Content and filename are required. If the
/// mime_type is unspecified, the email will use Sendgrid's default for attachments
/// which is 'application/octet-stream'.
#[derive(Serialize)]
pub struct Attachment {
    content: String,

    filename: String,

    #[serde(rename = "type", skip_serializing_if = "check_encode")]
    mime_type: Option<String>,

    #[serde(skip_serializing_if = "check_encode")]
    disposition: Option<String>,

    #[serde(skip_serializing_if = "check_encode")]
    content_id: Option<String>,
}

// Checks if a value in the V3 message should be added to the JSON or not.
fn check_encode<T>(value: &Option<T>) -> bool {
    match *value {
        Some(_) => false,
        None => true,
    }
}

impl V3Sender {
    /// Construct a new V3 message sender.
    pub fn new(api_key: String) -> V3Sender {
        V3Sender { api_key: api_key }
    }

    /// Send a V3 message and return the status code or an error from the request.
    pub fn send(&self, mail: &SGMailV3) -> SendgridResult<StatusCode> {
        let client = Client::new()?;
        let mut headers = Headers::new();
        headers.set(Authorization(Bearer { token: self.api_key.to_owned() }));
        headers.set(ContentType::json());
        headers.set(UserAgent::new("sendgrid-rs"));

        let body = mail.gen_json();
        let res = client.post(V3_API_URL)?.headers(headers).body(body).send()?;
        Ok(res.status())
    }
}

impl SGMailV3 {
    /// Construct a new V3 message.
    pub fn new() -> SGMailV3 {
        SGMailV3 {
            from: Email::new(),
            subject: String::new(),
            content: Vec::new(),
            personalizations: Vec::new(),
            attachments: None,
        }
    }

    /// Set the from address.
    pub fn set_from(&mut self, from: Email) {
        self.from = from;
    }

    /// Set the subject.
    pub fn set_subject(&mut self, subject: &str) {
        self.subject = String::from(subject);
    }

    /// Add content to the message.
    pub fn add_content(&mut self, content: Content) {
        self.content.push(content);
    }

    /// Add a personalization to the message.
    pub fn add_personalization(&mut self, p: Personalization) {
        self.personalizations.push(p);
    }

    /// Add an attachment to the message.
    pub fn add_attachment(&mut self, a: Attachment) {
        match self.attachments {
            None => {
                let mut attachments = Vec::new();
                attachments.push(a);
                self.attachments = Some(attachments);
            }
            Some(ref mut attachments) => attachments.push(a),
        };
    }

    fn gen_json(&self) -> String {
        serde_json::to_string(self).unwrap()
    }
}

impl Email {
    /// Construct a new email type.
    pub fn new() -> Email {
        Email {
            email: String::new(),
            name: None,
        }
    }

    /// Set the address for this email.
    pub fn set_email(&mut self, email: &str) {
        self.email = String::from(email);
    }

    /// Set an optional name.
    pub fn set_name(&mut self, name: &str) {
        self.name = Some(String::from(name));
    }
}

impl Content {
    /// Construct a new content type.
    pub fn new() -> Content {
        Content {
            content_type: String::new(),
            value: String::new(),
        }
    }

    /// Set the type of this content.
    pub fn set_content_type(&mut self, content_type: &str) {
        self.content_type = String::from(content_type);
    }

    /// Set the corresponding message for this content.
    pub fn set_value(&mut self, value: &str) {
        self.value = String::from(value);
    }
}

impl Personalization {
    /// Construct a new personalization block for this message.
    pub fn new() -> Personalization {
        Personalization {
            to: Vec::new(),
            cc: None,
            bcc: None,
            subject: None,
            headers: None,
            substitutions: None,
            custom_args: None,
            send_at: None,
        }
    }

    /// Add a to field.
    pub fn add_to(&mut self, to: Email) {
        self.to.push(to);
    }

    /// Add a CC field.
    pub fn add_cc(&mut self, cc: Email) {
        match self.cc {
            None => {
                let mut ccs = Vec::new();
                ccs.push(cc);
                self.cc = Some(ccs);
            }
            Some(ref mut c) => {
                c.push(cc);
            }
        }
    }
}

impl Attachment {
    /// Construct a new attachment for this message.
    pub fn new() -> Attachment {
        Attachment {
            content: String::new(),
            filename: String::new(),
            mime_type: None,
            disposition: None,
            content_id: None,
        }
    }

    /// The raw body of the attachment.
    pub fn set_content(&mut self, c: &[u8]) {
        self.content = base64::encode(c);
    }

    /// Sets the filename for the attachment.
    pub fn set_filename(&mut self, filename: &str) {
        self.filename = filename.into();
    }

    /// Set an optional mime type. Sendgrid will default to 'application/octet-stream'
    /// if unspecified.
    pub fn set_mime_type(&mut self, mime: &str) {
        self.mime_type = Some(String::from(mime));
    }
}