diff --git a/VIRTUAL_VACATION/INSTALL.md b/VIRTUAL_VACATION/INSTALL.md index 853dd990..086fbac5 100644 --- a/VIRTUAL_VACATION/INSTALL.md +++ b/VIRTUAL_VACATION/INSTALL.md @@ -32,6 +32,7 @@ MIME::EncWords Log::Log4perl Log::Dispatch GetOpt::Std +Net::DNS; ``` You may install these via CPAN, or through your package tool. @@ -43,7 +44,7 @@ CPAN: 'perl -MCPAN -e shell', then 'install Module::Whatever' ```bash - apt-get install libemail-sender-perl libemail-simple-perl libemail-valid-perl libtry-tiny-perl libdbd-pg-perl libemail-mime-perl liblog-log4perl-perl liblog-dispatch-perl libgetopt-argvfile-perl libmime-charset-perl libmime-encwords-perl libmime-encwords-perl + apt-get install libemail-sender-perl libemail-simple-perl libemail-valid-perl libtry-tiny-perl libdbd-pg-perl libemail-mime-perl liblog-log4perl-perl liblog-dispatch-perl libgetopt-argvfile-perl libmime-charset-perl libmime-encwords-perl libmime-encwords-perl libnet-dns-perl ``` and one of : diff --git a/VIRTUAL_VACATION/vacation.pl b/VIRTUAL_VACATION/vacation.pl index 297628d6..fd072182 100644 --- a/VIRTUAL_VACATION/vacation.pl +++ b/VIRTUAL_VACATION/vacation.pl @@ -30,7 +30,7 @@ use Email::Simple::Creator; use Try::Tiny; use Log::Log4perl qw(get_logger :levels); use File::Basename; - +use Net::DNS; # ========== begin configuration ========== # IMPORTANT: If you put passwords into this script, then remember @@ -53,24 +53,17 @@ our $vacation_domain = 'autoreply.example.org'; our $recipient_delimiter = '+'; -# smtp server used to send vacation e-mails -our $smtp_server = 'localhost'; # port to connect to; defaults to 25 for non-SSL, 465 for 'ssl', 587 for 'starttls' our $smtp_server_port = 25; # this is the helo we [the vacation script] use on connection; you may need to change this to your hostname or something, -# depending upon what smtp helo restrictions you have in place within Postfix. +# depending upon what smtp helo restrictions you have in place within Postfix. our $smtp_client = 'localhost'; # send mail encrypted or plaintext # if 'starttls', use STARTTLS; if 'ssl' (or 1), connect securely; otherwise, no security our $smtp_ssl = 'starttls'; -# passed to Net::SMTP constructor for 'ssl' connections or to starttls for 'starttls' connections; should contain extra options for IO::Socket::SSL -our $ssl_options = { - SSL_verifycn_name => $smtp_server -}; - # maximum time in secs to wait for server; default is 120 our $smtp_timeout = '120'; @@ -86,7 +79,6 @@ our $smtp_authpwd = ''; # From: Some Friendly Name our $friendly_from = ''; - # Set to 1 to enable logging to syslog. our $syslog = 0; @@ -457,10 +449,26 @@ sub send_vacation_email { my $from = $email; my $to = $orig_from; + # part of the username in the email && part of the domain in the email + my ($email_username_part, $email_domain_part) = split(/@/, $email); + + my $resolver = Net::DNS::Resolver->new; + my @mx = mx($resolver, $email_domain_part); + my $smtp_server; + if (@mx) { + $smtp_server = @mx[0]->exchange; + $logger->debug("Found MX record <$smtp_server> for user <$email>!"); + } else { + $logger->error("Unable to find MX record for user <$email>, error message: ".$resolver->errorstring); + exit(0); + } + my $smtp_params = { host => $smtp_server, port => $smtp_server_port, - ssl_options => $ssl_options, + ssl_options => { + SSL_verifycn_name => $smtp_server + }, ssl => $smtp_ssl, timeout => $smtp_timeout, localaddr => $smtp_client,