2021-12-15 19:49:59 +00:00
|
|
|
use crate::{settings::structs::Settings, LemmyError};
|
2020-09-14 15:29:50 +00:00
|
|
|
use lettre::{
|
2020-10-30 17:19:00 +00:00
|
|
|
message::{header, Mailbox, MultiPart, SinglePart},
|
|
|
|
transport::smtp::{
|
|
|
|
authentication::Credentials,
|
|
|
|
client::{Tls, TlsParameters},
|
2020-09-14 15:29:50 +00:00
|
|
|
extension::ClientId,
|
|
|
|
},
|
2020-10-30 17:19:00 +00:00
|
|
|
Address,
|
|
|
|
Message,
|
|
|
|
SmtpTransport,
|
2020-09-14 15:29:50 +00:00
|
|
|
Transport,
|
|
|
|
};
|
2020-10-30 17:19:00 +00:00
|
|
|
use std::str::FromStr;
|
2021-10-12 11:38:55 +00:00
|
|
|
use uuid::Uuid;
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
pub fn send_email(
|
|
|
|
subject: &str,
|
|
|
|
to_email: &str,
|
|
|
|
to_username: &str,
|
|
|
|
html: &str,
|
2021-09-22 15:57:09 +00:00
|
|
|
settings: &Settings,
|
2021-12-15 19:49:59 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let email_config = settings
|
|
|
|
.email
|
|
|
|
.to_owned()
|
|
|
|
.ok_or_else(|| LemmyError::from_message("no_email_setup"))?;
|
2021-09-22 15:57:09 +00:00
|
|
|
let domain = settings.hostname.to_owned();
|
2020-09-14 15:29:50 +00:00
|
|
|
|
2020-10-30 17:19:00 +00:00
|
|
|
let (smtp_server, smtp_port) = {
|
|
|
|
let email_and_port = email_config.smtp_server.split(':').collect::<Vec<&str>>();
|
2021-12-15 19:49:59 +00:00
|
|
|
if email_and_port.len() == 1 {
|
|
|
|
return Err(LemmyError::from_message(
|
|
|
|
"email.smtp_server needs a port, IE smtp.xxx.com:465",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:19:00 +00:00
|
|
|
(
|
|
|
|
email_and_port[0],
|
|
|
|
email_and_port[1]
|
|
|
|
.parse::<u16>()
|
|
|
|
.expect("email needs a port"),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let email = Message::builder()
|
|
|
|
.from(
|
|
|
|
email_config
|
|
|
|
.smtp_from_address
|
|
|
|
.parse()
|
|
|
|
.expect("email from address isn't valid"),
|
|
|
|
)
|
|
|
|
.to(Mailbox::new(
|
|
|
|
Some(to_username.to_string()),
|
|
|
|
Address::from_str(to_email).expect("email to address isn't valid"),
|
|
|
|
))
|
2021-10-12 11:38:55 +00:00
|
|
|
.message_id(Some(format!("{}@{}", Uuid::new_v4(), settings.hostname)))
|
2020-09-14 15:29:50 +00:00
|
|
|
.subject(subject)
|
2020-10-30 17:19:00 +00:00
|
|
|
.multipart(
|
|
|
|
MultiPart::mixed().multipart(
|
|
|
|
MultiPart::alternative()
|
|
|
|
.singlepart(
|
2021-02-01 20:56:37 +00:00
|
|
|
SinglePart::builder()
|
2021-07-06 13:26:46 +00:00
|
|
|
.header(header::ContentType::TEXT_PLAIN)
|
2021-02-01 20:56:37 +00:00
|
|
|
.body(html.to_string()),
|
2020-10-30 17:19:00 +00:00
|
|
|
)
|
|
|
|
.multipart(
|
|
|
|
MultiPart::related().singlepart(
|
2021-02-01 20:56:37 +00:00
|
|
|
SinglePart::builder()
|
2021-07-06 13:26:46 +00:00
|
|
|
.header(header::ContentType::TEXT_HTML)
|
2021-02-01 20:56:37 +00:00
|
|
|
.body(html.to_string()),
|
2020-10-30 17:19:00 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.expect("email built incorrectly");
|
|
|
|
|
|
|
|
// don't worry about 'dangeous'. it's just that leaving it at the default configuration
|
|
|
|
// is bad.
|
|
|
|
let mut builder = SmtpTransport::builder_dangerous(smtp_server).port(smtp_port);
|
|
|
|
|
|
|
|
// Set the TLS
|
|
|
|
if email_config.use_tls {
|
|
|
|
let tls_config = TlsParameters::new(smtp_server.to_string()).expect("the TLS backend is happy");
|
|
|
|
builder = builder.tls(Tls::Wrapper(tls_config));
|
2020-09-14 15:29:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 17:19:00 +00:00
|
|
|
// Set the creds if they exist
|
|
|
|
if let (Some(username), Some(password)) = (email_config.smtp_login, email_config.smtp_password) {
|
|
|
|
builder = builder.credentials(Credentials::new(username, password));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mailer = builder.hello_name(ClientId::Domain(domain)).build();
|
|
|
|
|
|
|
|
let result = mailer.send(&email);
|
2020-09-14 15:29:50 +00:00
|
|
|
|
|
|
|
match result {
|
|
|
|
Ok(_) => Ok(()),
|
2021-12-15 19:49:59 +00:00
|
|
|
Err(e) => Err(LemmyError::from(e).with_message("email_send_failed")),
|
2020-09-14 15:29:50 +00:00
|
|
|
}
|
|
|
|
}
|