Struct MessageBuilder

Source
pub struct MessageBuilder(/* private fields */);
Expand description

Represents a builder for SendGrid messages.

Implementations§

Source§

impl MessageBuilder

Source

pub fn new() -> MessageBuilder

Creates a new message builder.

Source

pub fn to<T>(self, email: T) -> MessageBuilder
where T: Into<String>,

Appends the given “to” email address to the first personalization of the message.

§Examples

let message = MessageBuilder::new().to("foo@example.com").finish();

assert_eq!(message.personalizations[0].to[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].to[0].name, None);
Source

pub fn to_with_name<T, U>(self, email: T, name: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends the given “to” email address to the first personalization of the message.

§Examples

let message = MessageBuilder::new().to_with_name("foo@example.com", "Peter").finish();

assert_eq!(message.personalizations[0].to[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].to[0].name, Some("Peter".to_owned()));
Source

pub fn tos<T>(self, addresses: T) -> MessageBuilder
where T: IntoIterator<Item = EmailAddress>,

Appends the given “to” email addresses to the first personalization of the message.

§Examples

let message = MessageBuilder::new().tos(
    vec![
        EmailAddress::new("foo@example.com"),
        EmailAddress::new_with_name("bar@example.com", "Peter"),
    ]).finish();

assert_eq!(message.personalizations[0].to[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].to[0].name, None);
assert_eq!(message.personalizations[0].to[1].email, "bar@example.com");
assert_eq!(message.personalizations[0].to[1].name, Some("Peter".to_owned()));
Source

pub fn cc<T>(self, email: T) -> MessageBuilder
where T: Into<String>,

Appends the given “cc” email address to the first personalization of the message.

§Examples

let message = MessageBuilder::new().cc("foo@example.com").finish();

assert_eq!(message.personalizations[0].cc[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].cc[0].name, None);
Source

pub fn cc_with_name<T, U>(self, email: T, name: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends the given “cc” email address to the first personalization of the message.

§Examples

let message = MessageBuilder::new().cc_with_name("foo@example.com", "Peter").finish();

assert_eq!(message.personalizations[0].cc[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].cc[0].name, Some("Peter".to_owned()));
Source

pub fn ccs<T>(self, addresses: T) -> MessageBuilder
where T: IntoIterator<Item = EmailAddress>,

Appends the given “cc” email addresses to the first personalization of the message.

§Examples

let message = MessageBuilder::new().ccs(
    vec![
        EmailAddress::new("foo@example.com"),
        EmailAddress::new_with_name("bar@example.com", "Peter"),
    ]).finish();

assert_eq!(message.personalizations[0].cc[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].cc[0].name, None);
assert_eq!(message.personalizations[0].cc[1].email, "bar@example.com");
assert_eq!(message.personalizations[0].cc[1].name, Some("Peter".to_owned()));
Source

pub fn bcc<T>(self, email: T) -> MessageBuilder
where T: Into<String>,

Appends the given “bcc” email address to the first personalization of the message.

§Examples

let message = MessageBuilder::new().bcc("foo@example.com").finish();

assert_eq!(message.personalizations[0].bcc[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].bcc[0].name, None);
Source

pub fn bcc_with_name<T, U>(self, email: T, name: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends the given “bcc” email address to the first personalization of the message.

§Examples

let message = MessageBuilder::new().bcc_with_name("foo@example.com", "Peter").finish();

assert_eq!(message.personalizations[0].bcc[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].bcc[0].name, Some("Peter".to_owned()));
Source

pub fn bccs<T>(self, addresses: T) -> MessageBuilder
where T: IntoIterator<Item = EmailAddress>,

Appends the given “bcc” email addresses to the first personalization of the message.

§Examples

let message = MessageBuilder::new().bccs(
    vec![
        EmailAddress::new("foo@example.com"),
        EmailAddress::new_with_name("bar@example.com", "Peter"),
    ]).finish();

assert_eq!(message.personalizations[0].bcc[0].email, "foo@example.com");
assert_eq!(message.personalizations[0].bcc[0].name, None);
assert_eq!(message.personalizations[0].bcc[1].email, "bar@example.com");
assert_eq!(message.personalizations[0].bcc[1].name, Some("Peter".to_owned()));
Source

pub fn subject<T>(self, subject: T) -> MessageBuilder
where T: Into<String>,

Sets the subject for the first personalization of the message.

§Examples

let message = MessageBuilder::new().subject("hello world!").finish();

assert_eq!(message.personalizations[0].subject, Some("hello world!".to_owned()));
Source

pub fn header<T, U>(self, key: T, value: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends a header to the first personalization of the message.

§Examples

let message = MessageBuilder::new().header("foo", "bar").finish();

assert_eq!(message.personalizations[0].headers.get("foo").map(String::as_str), Some("bar"));
Source

pub fn headers<T>(self, headers: T) -> MessageBuilder
where T: IntoIterator<Item = (String, String)>,

Appends multiple headers to the first personalization of the message.

§Examples

let mut headers = HashMap::new();
headers.insert("foo".to_owned(), "bar".to_owned());
headers.insert("bar".to_owned(), "baz".to_owned());

let message = MessageBuilder::new().headers(headers).finish();

assert_eq!(message.personalizations[0].headers.get("foo").map(String::as_str), Some("bar"));
assert_eq!(message.personalizations[0].headers.get("bar").map(String::as_str), Some("baz"));
Source

pub fn substitution<T, U>(self, key: T, value: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends a substitution to the first personalization of the message.

§Examples

let message = MessageBuilder::new().substitution("foo", "bar").finish();

assert_eq!(message.personalizations[0].substitutions.get("foo").map(String::as_str), Some("bar"));
Source

pub fn substitutions<T>(self, substitutions: T) -> MessageBuilder
where T: IntoIterator<Item = (String, String)>,

Appends multiple substitutions to the first personalization of the message.

§Examples

let mut substitutions = HashMap::new();
substitutions.insert("foo".to_owned(), "bar".to_owned());
substitutions.insert("bar".to_owned(), "baz".to_owned());

let message = MessageBuilder::new().substitutions(substitutions).finish();

assert_eq!(message.personalizations[0].substitutions.get("foo").map(String::as_str), Some("bar"));
assert_eq!(message.personalizations[0].substitutions.get("bar").map(String::as_str), Some("baz"));
Source

pub fn template_data(self, data: Value) -> MessageBuilder

Sets the template data for the first personalization of the message.

§Examples

let message = MessageBuilder::new().template_data(json!({ "foo": "bar" })).finish();

assert_eq!(to_string(message.personalizations[0].template_data.as_ref().unwrap()).unwrap(), r#"{"foo":"bar"}"#);
Source

pub fn custom_arg<T, U>(self, key: T, value: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends a custom argument to the first personalization of the message.

§Examples

let message = MessageBuilder::new().custom_arg("foo", "bar").finish();

assert_eq!(message.personalizations[0].custom_args.get("foo").map(String::as_str), Some("bar"));
Source

pub fn custom_args<T>(self, args: T) -> MessageBuilder
where T: IntoIterator<Item = (String, String)>,

Appends multiple custom arguments to the first personalization of the message.

§Examples

let mut args = HashMap::new();
args.insert("foo".to_owned(), "bar".to_owned());
args.insert("bar".to_owned(), "baz".to_owned());

let message = MessageBuilder::new().custom_args(args).finish();

assert_eq!(message.personalizations[0].custom_args.get("foo").map(String::as_str), Some("bar"));
assert_eq!(message.personalizations[0].custom_args.get("bar").map(String::as_str), Some("baz"));
Source

pub fn send_at(self, timestamp: i64) -> MessageBuilder

Sets the “send at” timestamp for the first personalization of the message.

Note: This trait uses a Unix timestamp. A handy Unix timestamp converter can be found at unixtimestamp.com/

§Examples

let message = MessageBuilder::new().send_at(1555890183).finish();

assert_eq!(message.personalizations[0].send_at, Some(1555890183));
Source

pub fn from<T>(self, email: T) -> MessageBuilder
where T: Into<String>,

Sets the “from” email address for the message.

§Examples

let message = MessageBuilder::new().from("foo@example.com").finish();

assert_eq!(message.from.as_ref().unwrap().email, "foo@example.com");
assert_eq!(message.from.as_ref().unwrap().name, None);
Source

pub fn from_with_name<T, U>(self, email: T, name: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Sets the “from” email address for the message.

§Examples

let message = MessageBuilder::new().from_with_name("foo@example.com", "Peter").finish();

assert_eq!(message.from.as_ref().unwrap().email, "foo@example.com");
assert_eq!(message.from.as_ref().unwrap().name, Some("Peter".to_owned()));
Source

pub fn global_subject<T>(self, subject: T) -> MessageBuilder
where T: Into<String>,

Sets the default global subject for all personalizations of the message.

§Examples

let message = MessageBuilder::new().global_subject("hello world").finish();

assert_eq!(message.subject, Some("hello world".to_owned()));
Source

pub fn content<T>(self, text: T) -> MessageBuilder
where T: Into<String>,

Adds a text content (with MIME type “text/plain”) to the message.

§Examples

let message = MessageBuilder::new().content("hello world").finish();

assert_eq!(message.contents[0].mime_type, "text/plain");
assert_eq!(message.contents[0].value, "hello world");
Source

pub fn content_with_type<T, U>(self, content: T, mime_type: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Adds a content with the given MIME type to the message.

§Examples

let message = MessageBuilder::new().content_with_type("hello world", "text/plain").finish();

assert_eq!(message.contents[0].mime_type, "text/plain");
assert_eq!(message.contents[0].value, "hello world");
Source

pub fn contents<T>(self, contents: T) -> MessageBuilder
where T: IntoIterator<Item = Content>,

Adds the given content to the message.

§Examples

let message = MessageBuilder::new()
    .contents(
        vec![
            Content{ mime_type: "text/plain".to_owned(), value: "hello world".to_owned() }
        ])
    .finish();

assert_eq!(message.contents[0].mime_type, "text/plain");
assert_eq!(message.contents[0].value, "hello world");
Source

pub fn attachment<T, U, V>( self, filename: T, mime_type: U, content: V, ) -> MessageBuilder
where T: Into<String>, U: Into<String>, V: Into<String>,

Adds an attachment to the message.

SendGrid expects the content argument to be Base 64 encoded. In this example, “hello world” is encoded as “aGVsbG8gd29ybGQ=”

§Examples

let message = MessageBuilder::new().attachment("hello.txt", "text/plain", "aGVsbG8gd29ybGQ=").finish();

assert_eq!(message.attachments[0].filename, "hello.txt");
assert_eq!(message.attachments[0].mime_type, "text/plain");
assert_eq!(message.attachments[0].content, "aGVsbG8gd29ybGQ=");
Source

pub fn inline_attachment<T, U, V, W>( self, filename: T, mime_type: U, content: V, content_id: W, ) -> MessageBuilder
where T: Into<String>, U: Into<String>, V: Into<String>, W: Into<String>,

Adds an attachment to the message.

SendGrid expects the content argument to be Base 64 encoded. In this example, “hello world” is encoded as “aGVsbG8gd29ybGQ=”

§Examples

let message = MessageBuilder::new().inline_attachment("hello.jpg", "image/jpeg", "aGVsbG8gd29ybGQ=", "img_139db99fdb5c3704").finish();

assert_eq!(message.attachments[0].filename, "hello.jpg");
assert_eq!(message.attachments[0].mime_type, "image/jpeg");
assert_eq!(message.attachments[0].content, "aGVsbG8gd29ybGQ=");
assert_eq!(message.attachments[0].disposition, Some("inline".to_owned()));
assert_eq!(message.attachments[0].content_id, Some("img_139db99fdb5c3704".to_owned()));
Source

pub fn attachments<T>(self, attachments: T) -> MessageBuilder
where T: IntoIterator<Item = Attachment>,

Adds multiple attachments to the message.

SendGrid expects the content argument to be Base 64 encoded. In this example, “hello world” is encoded as “aGVsbG8gd29ybGQ=”

§Examples
use azure_functions::send_grid::Attachment;

let message = MessageBuilder::new()
    .attachments(
        vec![
            Attachment{ filename: "hello.txt".to_owned(), mime_type: "text/plain".to_owned(), content: "aGVsbG8gd29ybGQ=".to_owned(), ..Default::default() }
        ])
    .finish();

assert_eq!(message.attachments[0].filename, "hello.txt");
assert_eq!(message.attachments[0].mime_type, "text/plain");
assert_eq!(message.attachments[0].content, "aGVsbG8gd29ybGQ=");
Source

pub fn template_id<T>(self, id: T) -> MessageBuilder
where T: Into<String>,

Sets the template id for the message.

§Examples
use azure_functions::send_grid::Attachment;

let message = MessageBuilder::new().template_id("id").finish();

assert_eq!(message.template_id, Some("id".to_owned()));
Source

pub fn section<T, U>(self, key: T, value: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends a section substitution to the message.

§Examples

let message = MessageBuilder::new().section("foo", "bar").finish();

assert_eq!(message.sections.get("foo").map(String::as_str), Some("bar"));
Source

pub fn sections<T>(self, sections: T) -> MessageBuilder
where T: IntoIterator<Item = (String, String)>,

Appends multiple sections to the message.

§Examples
use std::collections::HashMap;

let mut sections = HashMap::new();
sections.insert("foo".to_owned(), "bar".to_owned());
sections.insert("bar".to_owned(), "baz".to_owned());

let message = MessageBuilder::new().sections(sections).finish();

assert_eq!(message.sections.get("foo").map(String::as_str), Some("bar"));
assert_eq!(message.sections.get("bar").map(String::as_str), Some("baz"));
Source

pub fn category<T>(self, category: T) -> MessageBuilder
where T: Into<String>,

Appends a category to the message.

§Examples

let message = MessageBuilder::new().category("foo").finish();

assert_eq!(message.categories[0], "foo");
Source

pub fn categories<T>(self, categories: T) -> MessageBuilder
where T: IntoIterator<Item = String>,

Appends multiple categories to the message.

§Examples

let message = MessageBuilder::new().categories(vec!["foo".to_owned(), "bar".to_owned(), "baz".to_owned()]).finish();

assert_eq!(message.categories[0], "foo");
assert_eq!(message.categories[1], "bar");
assert_eq!(message.categories[2], "baz");
Source

pub fn global_header<T, U>(self, key: T, value: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends a global header for all personalizations of the message.

§Examples

let message = MessageBuilder::new().global_header("foo", "bar").finish();

assert_eq!(message.headers.get("foo").map(String::as_str), Some("bar"));
Source

pub fn global_headers<T>(self, headers: T) -> MessageBuilder
where T: IntoIterator<Item = (String, String)>,

Appends multiple global headers for all personalizations of the message.

§Examples
use std::collections::HashMap;

let mut headers = HashMap::new();
headers.insert("foo".to_owned(), "bar".to_owned());
headers.insert("bar".to_owned(), "baz".to_owned());

let message = MessageBuilder::new().global_headers(headers).finish();

assert_eq!(message.headers.get("foo").map(String::as_str), Some("bar"));
assert_eq!(message.headers.get("bar").map(String::as_str), Some("baz"));
Source

pub fn global_custom_arg<T, U>(self, key: T, value: U) -> MessageBuilder
where T: Into<String>, U: Into<String>,

Appends a global custom argument for all personalizations of the message.

§Examples

let message = MessageBuilder::new().global_custom_arg("foo", "bar").finish();

assert_eq!(message.custom_args.get("foo").map(String::as_str), Some("bar"));
Source

pub fn global_custom_args<T>(self, args: T) -> MessageBuilder
where T: IntoIterator<Item = (String, String)>,

Appends multiple global custom arguments for all personalizations of the message.

§Examples
use std::collections::HashMap;

let mut args = HashMap::new();
args.insert("foo".to_owned(), "bar".to_owned());
args.insert("bar".to_owned(), "baz".to_owned());

let message = MessageBuilder::new().global_custom_args(args).finish();

assert_eq!(message.custom_args.get("foo").map(String::as_str), Some("bar"));
assert_eq!(message.custom_args.get("bar").map(String::as_str), Some("baz"));
Source

pub fn global_send_at(self, timestamp: i64) -> MessageBuilder

Sets the global “send at” timestamp for all personalizations of the message.

Note: This trait uses a Unix timestamp. A handy Unix timestamp converter can be found at unixtimestamp.com

§Examples

let message = MessageBuilder::new().global_send_at(1555890183).finish();

assert_eq!(message.send_at, Some(1555890183));
Source

pub fn batch_id<T>(self, id: T) -> MessageBuilder
where T: Into<String>,

Sets the batch id for the message.

§Examples

let message = MessageBuilder::new().batch_id("HkJ5yLYULb7Rj8GKSx7u025ouWVlMgAi").finish();

assert_eq!(message.batch_id.unwrap(), "HkJ5yLYULb7Rj8GKSx7u025ouWVlMgAi");
Source

pub fn finish(self) -> SendGridMessage

Consumes the builder and returns the SendGrid message.

Trait Implementations§

Source§

impl Default for MessageBuilder

Source§

fn default() -> MessageBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more