use lazy_static::lazy_static;
use reqwest::Client;
lazy_static! {
static ref POST_MARK_FROM_EMAIL: &'static str = "something@gmail.com";
static ref POST_MARK_BASE_URL: &'static str = "https://api.postmarkapp.com";
static ref POST_MARK_API_TOKEN: &'static str = "YOUR_TOKEN_HERE";
}
pub struct EmailClient {
http_client: Client,
sender: &'static str,
}
#[derive(serde::Serialize)]
struct SendEmailRequest<'a> {
from: &'a str,
to: &'a str,
subject: &'a str,
html_body: &'a str,
}
impl EmailClient {
pub fn new() -> Self {
Self {
http_client: Client::new(),
sender: &POST_MARK_FROM_EMAIL,
}
}
pub async fn send_email(
&self,
recipient: SubscriberEmail,
subject: &str,
email_body: &str,
) -> Result<(), reqwest::Error> {
let url = format!("{}/email", POST_MARK_BASE_URL);
let request_body = SendEmailRequest {
from: self.sender,
to: recipient.as_ref(),
subject,
html_body: email_body,
};
self.http_client
.post(&url)
.header("X-Postmark-Server-Token", POST_MARK_API_TOKEN)
.json(&request_body)
.send()
.await?
.error_for_status()?;
Ok(())
}
}