mirror of
				https://github.com/nitnelave/lldap.git
				synced 2023-04-12 14:25:13 +00:00 
			
		
		
		
	server: Send an email with the test command
This commit is contained in:
		
							parent
							
								
									bbcd0288cf
								
							
						
					
					
						commit
						67f3e0167a
					
				
							
								
								
									
										31
									
								
								server/src/infra/mail.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								server/src/infra/mail.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
use crate::infra::configuration::MailOptions;
 | 
			
		||||
use anyhow::Result;
 | 
			
		||||
use lettre::{
 | 
			
		||||
    message::Mailbox, transport::smtp::authentication::Credentials, Message, SmtpTransport,
 | 
			
		||||
    Transport,
 | 
			
		||||
};
 | 
			
		||||
use log::debug;
 | 
			
		||||
 | 
			
		||||
pub fn send_test_email(to: Mailbox, options: &MailOptions) -> Result<()> {
 | 
			
		||||
    let from = options
 | 
			
		||||
        .from
 | 
			
		||||
        .clone()
 | 
			
		||||
        .unwrap_or_else(|| "LLDAP <nobody@lldap>".parse().unwrap());
 | 
			
		||||
    let reply_to = options.reply_to.clone().unwrap_or_else(|| from.clone());
 | 
			
		||||
    debug!(
 | 
			
		||||
        "Sending email to '{}' as '{}' via '{}'@'{}':'{}'",
 | 
			
		||||
        &to, &from, &options.user, &options.server, options.port
 | 
			
		||||
    );
 | 
			
		||||
    let email = Message::builder()
 | 
			
		||||
        .from(from)
 | 
			
		||||
        .reply_to(reply_to)
 | 
			
		||||
        .to(to)
 | 
			
		||||
        .subject("LLDAP test email")
 | 
			
		||||
        .body("The test is successful! You can send emails from LLDAP".to_string())?;
 | 
			
		||||
    let creds = Credentials::new(options.user.clone(), options.password.clone());
 | 
			
		||||
    let mailer = SmtpTransport::relay(&options.server)?
 | 
			
		||||
        .credentials(creds)
 | 
			
		||||
        .build();
 | 
			
		||||
    mailer.send(&email)?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
@ -7,6 +7,7 @@ pub mod jwt_sql_tables;
 | 
			
		||||
pub mod ldap_handler;
 | 
			
		||||
pub mod ldap_server;
 | 
			
		||||
pub mod logging;
 | 
			
		||||
pub mod mail;
 | 
			
		||||
pub mod sql_backend_handler;
 | 
			
		||||
pub mod tcp_backend_handler;
 | 
			
		||||
pub mod tcp_server;
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ use crate::{
 | 
			
		||||
        sql_opaque_handler::register_password,
 | 
			
		||||
        sql_tables::PoolOptions,
 | 
			
		||||
    },
 | 
			
		||||
    infra::{cli::*, configuration::Configuration, db_cleaner::Scheduler},
 | 
			
		||||
    infra::{cli::*, configuration::Configuration, db_cleaner::Scheduler, mail},
 | 
			
		||||
};
 | 
			
		||||
use actix::Actor;
 | 
			
		||||
use anyhow::{anyhow, Context, Result};
 | 
			
		||||
@ -99,11 +99,18 @@ fn run_server_command(opts: RunOpts) -> Result<()> {
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn send_test_email_command(opts: TestEmailOpts) -> Result<()> {
 | 
			
		||||
    let to = opts.to.parse()?;
 | 
			
		||||
    let config = infra::configuration::init(opts)?;
 | 
			
		||||
    infra::logging::init(&config)?;
 | 
			
		||||
    mail::send_test_email(to, &config.smtp_options)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<()> {
 | 
			
		||||
    let cli_opts = infra::cli::init();
 | 
			
		||||
    match cli_opts.command {
 | 
			
		||||
        Command::ExportGraphQLSchema(opts) => infra::graphql::api::export_schema(opts),
 | 
			
		||||
        Command::Run(opts) => run_server_command(opts),
 | 
			
		||||
        Command::SendTestEmail(_opts) => Ok(()),
 | 
			
		||||
        Command::SendTestEmail(opts) => send_test_email_command(opts),
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user