mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-09 09:52:10 +00:00
* Update cargo deps, upgrading lettre. #789 * Adding a comment * Adding some better expect messages. * Fixing lettre email.
This commit is contained in:
parent
9e5e0eb9b7
commit
7ef044231f
756
Cargo.lock
generated
756
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -35,14 +35,14 @@ actix-files = { version = "0.4", default-features = false }
|
|||
actix-web-actors = { version = "3.0", default-features = false }
|
||||
awc = { version = "2.0", default-features = false }
|
||||
log = "0.4"
|
||||
env_logger = "0.7"
|
||||
env_logger = "0.8"
|
||||
strum = "0.19"
|
||||
lazy_static = "1.3"
|
||||
rss = "1.9"
|
||||
url = { version = "2.1", features = ["serde"] }
|
||||
openssl = "0.10"
|
||||
http-signature-normalization-actix = { version = "0.4", default-features = false, features = ["sha-2"] }
|
||||
tokio = "0.2"
|
||||
tokio = "0.3"
|
||||
sha2 = "0.9"
|
||||
anyhow = "1.0"
|
||||
reqwest = { version = "0.10", features = ["json"] }
|
||||
|
|
|
@ -35,7 +35,7 @@ openssl = "0.10"
|
|||
http = "0.2"
|
||||
http-signature-normalization-actix = { version = "0.4", default-features = false, features = ["sha-2"] }
|
||||
base64 = "0.13"
|
||||
tokio = "0.2"
|
||||
tokio = "0.3"
|
||||
futures = "0.3"
|
||||
itertools = "0.9"
|
||||
uuid = { version = "0.8", features = ["serde", "v4"] }
|
||||
|
|
|
@ -36,7 +36,7 @@ http = "0.2"
|
|||
http-signature-normalization-actix = { version = "0.4", default-features = false, features = ["sha-2"] }
|
||||
http-signature-normalization-reqwest = { version = "0.1.3", default-features = false, features = ["sha-2"] }
|
||||
base64 = "0.13"
|
||||
tokio = "0.2"
|
||||
tokio = "0.3"
|
||||
futures = "0.3"
|
||||
itertools = "0.9"
|
||||
uuid = { version = "0.8", features = ["serde", "v4"] }
|
||||
|
|
|
@ -10,7 +10,7 @@ path = "src/lib.rs"
|
|||
|
||||
[dependencies]
|
||||
lemmy_utils = { path = "../lemmy_utils" }
|
||||
tokio = "0.2"
|
||||
tokio = { version = "0.3", features = ["sync"] }
|
||||
strum = "0.19"
|
||||
strum_macros = "0.19"
|
||||
futures = "0.3.5"
|
||||
|
|
|
@ -11,8 +11,7 @@ path = "src/lib.rs"
|
|||
regex = "1.3"
|
||||
config = { version = "0.10", default-features = false, features = ["hjson"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
lettre = "0.9"
|
||||
lettre_email = "0.9"
|
||||
lettre = "0.10.0-alpha.3"
|
||||
log = "0.4"
|
||||
itertools = "0.9"
|
||||
rand = "0.7"
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
use crate::settings::Settings;
|
||||
use lettre::{
|
||||
smtp::{
|
||||
authentication::{Credentials, Mechanism},
|
||||
message::{header, Mailbox, MultiPart, SinglePart},
|
||||
transport::smtp::{
|
||||
authentication::Credentials,
|
||||
client::{Tls, TlsParameters},
|
||||
extension::ClientId,
|
||||
ConnectionReuseParameters,
|
||||
},
|
||||
ClientSecurity,
|
||||
SmtpClient,
|
||||
Address,
|
||||
Message,
|
||||
SmtpTransport,
|
||||
Transport,
|
||||
};
|
||||
use lettre_email::Email;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub fn send_email(
|
||||
subject: &str,
|
||||
|
@ -18,35 +20,75 @@ pub fn send_email(
|
|||
html: &str,
|
||||
) -> Result<(), String> {
|
||||
let email_config = Settings::get().email.ok_or("no_email_setup")?;
|
||||
let domain = Settings::get().hostname;
|
||||
|
||||
let email = Email::builder()
|
||||
.to((to_email, to_username))
|
||||
.from(email_config.smtp_from_address.to_owned())
|
||||
.subject(subject)
|
||||
.html(html)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let mailer = if email_config.use_tls {
|
||||
SmtpClient::new_simple(&email_config.smtp_server).unwrap()
|
||||
} else {
|
||||
SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
|
||||
}
|
||||
.hello_name(ClientId::Domain(Settings::get().hostname))
|
||||
.smtp_utf8(true)
|
||||
.authentication_mechanism(Mechanism::Plain)
|
||||
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited);
|
||||
let mailer = if let (Some(login), Some(password)) =
|
||||
(&email_config.smtp_login, &email_config.smtp_password)
|
||||
{
|
||||
mailer.credentials(Credentials::new(login.to_owned(), password.to_owned()))
|
||||
} else {
|
||||
mailer
|
||||
let (smtp_server, smtp_port) = {
|
||||
let email_and_port = email_config.smtp_server.split(':').collect::<Vec<&str>>();
|
||||
(
|
||||
email_and_port[0],
|
||||
email_and_port[1]
|
||||
.parse::<u16>()
|
||||
.expect("email needs a port"),
|
||||
)
|
||||
};
|
||||
|
||||
let mut transport = mailer.transport();
|
||||
let result = transport.send(email.into());
|
||||
transport.close();
|
||||
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"),
|
||||
))
|
||||
.subject(subject)
|
||||
.multipart(
|
||||
MultiPart::mixed().multipart(
|
||||
MultiPart::alternative()
|
||||
.singlepart(
|
||||
SinglePart::eight_bit()
|
||||
.header(header::ContentType(
|
||||
"text/plain; charset=utf8"
|
||||
.parse()
|
||||
.expect("email could not parse header"),
|
||||
))
|
||||
.body(html),
|
||||
)
|
||||
.multipart(
|
||||
MultiPart::related().singlepart(
|
||||
SinglePart::eight_bit()
|
||||
.header(header::ContentType(
|
||||
"text/html; charset=utf8"
|
||||
.parse()
|
||||
.expect("email could not parse header"),
|
||||
))
|
||||
.body(html),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.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));
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
match result {
|
||||
Ok(_) => Ok(()),
|
||||
|
|
|
@ -22,7 +22,7 @@ actix = "0.10"
|
|||
anyhow = "1.0"
|
||||
diesel = "1.4"
|
||||
background-jobs = " 0.8"
|
||||
tokio = "0.2"
|
||||
tokio = "0.3"
|
||||
strum = "0.19"
|
||||
strum_macros = "0.19"
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
|
Loading…
Reference in a new issue