2019-07-18 13:55:14 +02:00
|
|
|
// email_service.rs
|
|
|
|
use crate::errors::ServiceError;
|
2019-03-29 21:43:03 +01:00
|
|
|
use crate::models::Invitation;
|
2018-12-09 16:55:36 +01:00
|
|
|
use sparkpost::transmission::{
|
|
|
|
EmailAddress, Message, Options, Recipient, Transmission, TransmissionResponse,
|
|
|
|
};
|
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref API_KEY: String = std::env::var("SPARKPOST_API_KEY").expect("SPARKPOST_API_KEY must be set");
|
2018-12-09 16:55:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-18 13:55:14 +02:00
|
|
|
pub fn send_invitation(invitation: &Invitation) -> Result<(), ServiceError> {
|
|
|
|
let tm = Transmission::new_eu(API_KEY.as_str());
|
2022-02-18 03:44:02 +01:00
|
|
|
let sending_email =
|
|
|
|
std::env::var("SENDING_EMAIL_ADDRESS").expect("SENDING_EMAIL_ADDRESS must be set");
|
2018-12-09 16:55:36 +01:00
|
|
|
// new email message with sender name and email
|
|
|
|
let mut email = Message::new(EmailAddress::new(sending_email, "Let's Organise"));
|
|
|
|
|
|
|
|
let options = Options {
|
|
|
|
open_tracking: false,
|
|
|
|
click_tracking: false,
|
|
|
|
transactional: true,
|
|
|
|
sandbox: false,
|
|
|
|
inline_css: false,
|
|
|
|
start_time: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// recipient from the invitation email
|
|
|
|
let recipient: Recipient = invitation.email.as_str().into();
|
|
|
|
|
|
|
|
let email_body = format!(
|
|
|
|
"Please click on the link below to complete registration. <br/>
|
|
|
|
<a href=\"http://localhost:3000/register.html?id={}&email={}\">
|
|
|
|
http://localhost:3030/register</a> <br>
|
|
|
|
your Invitation expires on <strong>{}</strong>",
|
|
|
|
invitation.id,
|
|
|
|
invitation.email,
|
2022-02-02 03:19:39 +01:00
|
|
|
invitation.expires_at.format("%I:%M %p %A, %-d %B, %C%y")
|
2018-12-09 16:55:36 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// complete the email message with details
|
|
|
|
email
|
|
|
|
.add_recipient(recipient)
|
|
|
|
.options(options)
|
|
|
|
.subject("You have been invited to join Simple-Auth-Server Rust")
|
|
|
|
.html(email_body);
|
|
|
|
|
|
|
|
let result = tm.send(&email);
|
|
|
|
|
|
|
|
// Note that we only print out the error response from email api
|
|
|
|
match result {
|
2019-03-10 03:03:09 +01:00
|
|
|
Ok(res) => match res {
|
|
|
|
TransmissionResponse::ApiResponse(api_res) => {
|
|
|
|
println!("API Response: \n {:#?}", api_res);
|
2019-07-18 13:55:14 +02:00
|
|
|
Ok(())
|
2018-12-09 16:55:36 +01:00
|
|
|
}
|
2019-03-10 03:03:09 +01:00
|
|
|
TransmissionResponse::ApiError(errors) => {
|
|
|
|
println!("Response Errors: \n {:#?}", &errors);
|
2019-07-18 13:55:14 +02:00
|
|
|
Err(ServiceError::InternalServerError)
|
2019-03-10 03:03:09 +01:00
|
|
|
}
|
|
|
|
},
|
2018-12-09 16:55:36 +01:00
|
|
|
Err(error) => {
|
2019-07-18 13:55:14 +02:00
|
|
|
println!("Send Email Error: \n {:#?}", error);
|
|
|
|
Err(ServiceError::InternalServerError)
|
2018-12-09 16:55:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|