aisa/scripts/admin/oneoff/sendmailv3.pl
2025-11-26 11:31:54 +02:00

146 lines
4.8 KiB
Perl

#!/usr/bin/perl
BEGIN { use lib '/usr/home/cfg' ; require push_inc ; }
use MIME::Lite;
use Mail::Sendmail;
require cfg ;
print "Content-type: text/html\n\n";
&today ;
our $debug = 1 ;
#------------------------------------------------------------------------------------------
# https://itvadmin.co.za/cgi-bin/scripts/admin/oneoff/sendmailv3.pl
use CGI::Carp qw(fatalsToBrowser);
&common_v3_send_smtp_mail('','rory@kre8it.co.za','rory@mathew.za.net,admin@kre8it.co.za','k@re8it.com',"Calibration : $now_ccyy_mm_dd $now_hh_min_sec","Please see attached calibration...",'','html','',"$htmlpath/pdf/operator_flyer",'operator_email_attach.pdf','pdf',0,0) ;
exit ;
#------------------------------------------------------------------------------------------
sub common_v3_send_smtp_mail {
my ($from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication,$uploaded_doc,$operator_email) = @_ ;
# &common_debug("common_send_smtp_mail [$from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication,$uploaded_doc,$operator_email]") ;
return unless $msg ;
return unless $subj ;
# my $htype = (-f "$attachpath/$attachname" || $uploaded_doc || $operator_email) ? 'htmlattach' : ($html eq 'html') ? $html : 'text' ;
# my $res = &common_mailsend_sendgrid($htype,$to,$subj,$msg,$attachpath,$attachname,"$useropts{short} Events",'events@itvadmin.co.za',$cc,$bcc,$uploaded_doc,$operator_email);
# return if $res eq 'success' ; # carry on if mailsend_sendgrid failed
my $main_email = $email_add{'events'} ;
$to = $main_email unless $to ;
$from = $afrihost_from unless $from ;
my $mail_body = ($html) ? &common_get_mail_body($msg,$table_msg,$thead) : $msg ;
if ($html) {
if ($attachname && -f "$attachpath/$attachname") {
&common_debug("common_send_smtp_mail attach : $attachpath/$attachname [$attachapplication]") ;
&common_v3_email_with_attachments($from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication) ;
return ;
}
if ($uploaded_doc) {
my @parts = split(/\//,$uploaded_doc) ;
$attachname = $parts[-1] ;
pop @parts ;
$attachpath = join("/",@parts) ;
&common_v3_email_with_attachments($from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,$attachpath,$attachname,'') ;
return ;
}
if ($operator_email) {
&common_v3_email_with_attachments($from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,"$htmlpath/pdf/operator_flyer/operator_email_attach.pdf",'operator_email_attach.pdf','pdf') ;
return ;
}
# &common_send_mail($to,$cc,$bcc,$html,$thead,$msg,$subj,$table_msg) ;
} else {
# &common_send_mail($to,$cc,$bcc,$html,$thead,$msg,$subj,$table_msg) ;
}
} #------------------------------------------------------------------------------------------
sub common_v3_email_with_attachments {
my ($from, $to, $cc, $bcc, $subj, $text_msg, $table_msg, $html, $thead, $attachpath, $attachname, $attachapplication) = @_;
use MIME::Lite;
use Net::SMTP::SSL;
# === Gmail SMTP configuration ===
my $smtp_host = $gmail_smtp; # e.g. "smtp.gmail.com"
my $smtp_port = 465;
my $smtp_user = $gmail_from;
my $smtp_pass = $gmail_psw;
# HTML body
my $mail_body = common_get_mail_body($text_msg, $table_msg);
# Attachment
my $fullattachpath = "$attachpath/$attachname";
die "Attachment file not found: $fullattachpath\n" unless -e $fullattachpath;
# === Create top-level message ===
my $msg = MIME::Lite->new(
From => "$useropts{short} Events <$from>",
'Reply-To' => $email_reply_to,
To => $to,
Cc => $cc || '',
Bcc => $bcc || '',
Subject => $subj,
Type => 'multipart/mixed',
);
# Add HTML part
$msg->attach(
Type => 'text/html',
Data => $mail_body
);
# Add attachment
my $attachtype = ($attachapplication) ? "application/$attachapplication" : 'AUTO';
$msg->attach(
Type => $attachtype,
Path => $fullattachpath,
Filename => $attachname,
Disposition => 'attachment',
Encoding => 'base64',
);
# === Send via Gmail ===
my $smtp = Net::SMTP::SSL->new($smtp_host, Port => $smtp_port, Timeout => 20)
or die "Cannot connect to $smtp_host: $!";
$smtp->auth($smtp_user, $smtp_pass)
or die "SMTP auth failed: " . $smtp->message();
$smtp->mail($from)
or die "MAIL FROM failed: " . $smtp->message();
for my $recipient (split /\s*,\s*/, join(',', grep { $_ } ($to, $cc, $bcc))) {
$smtp->to($recipient)
or die "RCPT TO <$recipient> failed: " . $smtp->message();
}
$smtp->data();
$smtp->datasend($msg->as_string);
$smtp->dataend();
$smtp->quit;
} #----------------------------------------------------------------------------------------
use today ;
use common ;
1;