#!/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/sendmailv2.pl

use CGI::Carp qw(fatalsToBrowser);

# $email_add{'reply-to'} = 'events@aisport.africa' ;

# &mailsend_sendgrid('text','rory@kre8it.co.za','TEST - Tue 30-Apr-2024','testing 123','/var/www/html/pdf/event_details','Event_Details-5136.pdf','Telemedia Events','website@bookingtelemedia.co.za','','','',0) ;

&common_v2_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) ;
# &common_v2_send_smtp_mail('','rory@kre8it.co.za','rory@mathew.za.net','',"Calibration : $now_ccyy_mm_dd $now_hh_min_sec","Please see attached calibration...",'','','','','','',0,0) ;
# &common_v2_send_smtp_mail('','rory@kre8it.co.za','rory@mathew.za.net','',"Calibration : $now_ccyy_mm_dd $now_hh_min_sec","Please see attached calibration...",'','','','','','',0,0) ;
# &common_send_mail('rory@kre8it.co.za','rory@mathew.za.net','k@re8it.com','html','',"Please see attached calibration...","Calibration : $now_ccyy_mm_dd $now_hh_min_sec") ;
# &common_send_mail('rory@kre8it.co.za','rory@mathew.za.net','k@re8it.com','','',"Please see attached calibration...","Calibration : $now_ccyy_mm_dd $now_hh_min_sec") ;
# &common_send_mail('rory@kre8it.co.za','rory@mathew.za.net,k@re8it.com','','','',"Please see attached calibration...","Calibration : $now_ccyy_mm_dd $now_hh_min_sec") ;


exit ;

#------------------------------------------------------------------------------------------

sub common_v2_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_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_email_with_attachments($from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,$attachpath,$attachname,'') ;
			return ;
		}
		if ($operator_email) {
			&common_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_email_with_attachments {
	
	my ($from,$to,$cc,$bcc,$subj,$text_msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication) = @_ ;
	
	use MIME::Lite;
	use Net::SMTP_auth;

	# SMTP configuration
	my $smtp_host = $afrihost_smtp ;
	my $smtp_port = $afrihost_port ;
	my $smtp_user = $afrihost_from ;
	my $smtp_pass = $afrihost_psw ;
	my $from      = $afrihost_from ;
	
	# HTML body part
	my $mail_body = &common_get_mail_body($text_msg,$table_msg) ;
	
	my $fullattachpath = "$attachpath/$attachname";
	die "Attachment file not found: $fullattachpath" unless -e $fullattachpath;

	# ===  build the MulitPart MIME Message ===
	
	# Create the 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',
	);

	# Create the HTML part
	my $html_part = MIME::Lite::->new(
		'Type' => 'multipart/related',
	);
	
	$html_part->attach(
		'Type' => 'text/html',
		'Data' => $mail_body,
	) ;

	# === Attachments ===	

	my $attachtype  = ($attachapplication) ? "application/$attachapplication" : 'AUTO' ;
	my $attach_part = MIME::Lite::->new(
		'Type' => 'multipart/mixed',
	);
	$attach_part->attach (
		Type        => "$attachtype",
		Encoding 	=> 'base64',
		Path        => "$fullattachpath",
		Filename    => $attachname,
		Disposition => 'attachement'
	) ;
	
	$msg->attach($attach_part);
	$msg->attach($html_part);
	
	my $email = $msg->as_string();
	
	my $smtp_msg = Net::SMTP_auth->new($smtp_host, Port=>587) or die "Can't connect";
	$smtp_msg->auth('PLAIN', $smtp_user, $smtp_pass) or die "Can't authenticate:" . $smtp_msg->message();

	$smtp_msg->mail($from) or die "Error:" . $smtp_msg->message();
	
	$smtp_msg->to($_) for split(/\s*,\s*/, join(',', $to, $cc, $bcc));
	
	# $smtp_msg->recipient($to)   or die "Error:".$smtp_msg->message();
	$smtp_msg->data()           or die "Error:".$smtp_msg->message();
	$smtp_msg->datasend($email) or die "Error:".$smtp_msg->message();
	$smtp_msg->dataend()        or die "Error:".$smtp_msg->message();
	$smtp_msg->quit             or die "Error:".$smtp_msg->message();

} #----------------------------------------------------------------------------------------

# sub common_email_with_attachments {
	
	# my ($from,$to,$cc,$bcc,$subj,$text_msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication) = @_ ;
	
	# use MIME::Lite;
	# use Net::SMTPS;

	# # SMTP configuration
	# my $smtp_host = $afrihost_smtp ;
	# my $smtp_port = $afrihost_port ;
	# my $smtp_user = $afrihost_from ;
	# my $smtp_pass = $afrihost_psw ;
	# my $from      = $afrihost_from ;
	
	# # HTML body part
	# my $mail_body = &common_get_mail_body($text_msg,$table_msg) ;
	
	# my $fullattachpath = "$attachpath/$attachname";
	# die "Attachment file not found: $fullattachpath" unless -e $fullattachpath;

	# # #################################################################
	# # Lets build the MulitPart MIME Message
	# # #################################################################
	# # Create the 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',
	# );

	# # Create the HTML part
	# my $html_part = MIME::Lite::->new(
		# 'Type' => 'multipart/related',
	# );
	
	# $html_part->attach(
		# 'Type' => 'text/html',
		# 'Data' => $mail_body,
	# ) ;

	# # === Attachments ===	

	# my $attachtype  = ($attachapplication) ? "application/$attachapplication" : 'AUTO' ;
	# my $attach_part = MIME::Lite::->new(
		# 'Type' => 'multipart/mixed',
	# );
	# $attach_part->attach (
		# Type        => "$attachtype",
		# Encoding 	=> 'base64',
		# Path        => "$fullattachpath",
		# Filename    => $attachname,
		# Disposition => 'attachement'
	# ) or &common_send_mail('rory@kre8it.co.za','','','',"$useropts{short} ERROR : common.pm","common_send_smtp_mail cant attach $attachpath/$attachname : $!") ;
	
	# $msg->attach($attach_part);
	# $msg->attach($html_part);
	
	# my $email = $msg->as_string();
	
	# # # === Send via SMTPS (port 465) ===

	# my $smtp = Net::SMTPS->new(
		# $smtp_host,
		# Port    => 587,
		# doSSL   => 'starttls',
		# Timeout => 30,
		# Debug   => 1,
	# ) or die "Failed to connect to SMTP server";

	# $smtp->auth($smtp_user, $smtp_pass) or die "SMTP Auth failed: $!";

	# # Send the email
	# $smtp->mail($from) or die "MAIL FROM failed";
	# # $smtp->to($_) for split(/\s*,\s*/, join(',', $to, $cc, $bcc));
	
	# $smtp->to($to) or die "Error:".$smtp->message();
	# # $smtp->recipient($to) or die "Error:".$smtp->message();
	
	# # &common_debug("common_send_smtp_mail attach : $attachpath/$attachname [$attachapplication]") ;

	# # # Send the message
	# # $smtp->data();
	# # $smtp->datasend($msg->as_string);
	# # my $response = $smtp->dataend();
	# # $smtp->quit;
	
	# eval {
		# $smtp->data() or die "Error:" . $smtp->message();
		# $smtp->datasend($email) or die "Error:" . $smtp->message();
		# $smtp->dataend() or die "Error:" . $smtp->message();
		# $smtp->quit;
	# };
	# if ($@ || $!) {
		# print "Email sending failed: $@ $!\n";
	# }  else {
		# print "Email sent successfully.\n";
	# }
		
	
	# # my $smtp_msg = Net::SMTP_auth->new($smtp_host, Port=>587) or die "Can't connect";
	# # $smtp_msg->auth('PLAIN', $smtp_user, $smtp_pass) or die "Can't authenticate:" . $smtp_msg->message();

	# # $smtp_msg->mail($from) or die "Error:".$smtp_msg->message();
	
	# # # $smtp->to($_) for split(/\s*,\s*/, join(',', $to, $cc, $bcc));
	
	# # $smtp_msg->recipient($to)   or die "Error:".$smtp_msg->message();
	# # $smtp_msg->data()           or die "Error:".$smtp_msg->message();
	# # $smtp_msg->datasend($email) or die "Error:".$smtp_msg->message();
	# # $smtp_msg->dataend()        or die "Error:".$smtp_msg->message();
	# # $smtp_msg->quit             or die "Error:".$smtp_msg->message();

# } #----------------------------------------------------------------------------------------

# sub common_email_with_attachments {
	
	# my ($from,$to,$cc,$bcc,$subj,$text_msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication) = @_ ;
	
	# use MIME::Lite;
	# use Net::SMTPS;

	# # SMTP configuration
	# my $smtp_host = $afrihost_smtp ;
	# my $smtp_port = $afrihost_port ;
	# my $smtp_user = $afrihost_from ;
	# my $smtp_pass = $afrihost_psw ;
	# my $from      = $afrihost_from ;
	
	# # HTML body part
	# my $html_body = &common_get_mail_body($text_msg,$table_msg) ;
	
	# my $fullpath = "$attachpath/$attachname";
	# die "Attachment file not found: $fullpath" unless -e $fullpath;

	# # Create MIME::Lite message
	# my $msg = MIME::Lite->new(
		# From    => $from,
		# To      => $to,
		# Cc      => $cc,
		# Bcc     => $bcc,
		# Subject => $subj,
		# Type    => 'multipart/mixed',
	# );

	# $msg->attach(
		# Type => 'text/html',
		# Data => $html_body,
	# );

	# # === Attachments ===			
	
	# my $attachtype  = ($attachapplication) ? "application/$attachapplication" : 'application/octet-stream' ; # application/pdf or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
	# $msg->attach(
		# Type        => "$attachtype",
		# # Encoding 	=> 'base64',
		# Path        => "$fullpath",
		# Filename    => "$attachname",
		# Disposition => 'attachment',
	# );

	# # # JPG
	# # $msg->attach(
		# # Type     => 'image/jpeg',
		# # Path     => 'photo.jpg',
		# # Filename => 'photo.jpg',
		# # Disposition => 'attachment',
	# # );

	# # === Send via SMTPS (port 465) ===

	# my $smtp = Net::SMTPS->new(
		# $smtp_host,
		# Port    => $smtp_port,
		# doSSL   => 'ssl',
		# Timeout => 30,
		# Debug   => 1,
	# ) or die "Failed to connect to SMTP server";

	# $smtp->auth($smtp_user, $smtp_pass) or die "SMTP Auth failed: $!";

	# # Send the email
	# $smtp->mail($from) or die "MAIL FROM failed";
	# $smtp->to($_) for split(/\s*,\s*/, join(',', $to, $cc, $bcc));
	
	# # &common_debug("common_send_smtp_mail attach : $attachpath/$attachname [$attachapplication]") ;

	# # # Send the message
	# # $smtp->data();
	# # $smtp->datasend($msg->as_string);
	# # my $response = $smtp->dataend();
	# # $smtp->quit;
	
	# eval {
		# $smtp->data() or die "DATA command failed : $@ $!";
		# $smtp->datasend($msg->as_string) or die "DATASEND failed : $@ $!";
		# $smtp->dataend() or die "DATAEND failed : $@ $!";
		# $smtp->quit;
	# };
	# if ($@ || $!) {
		# print "Email sending failed: $@ $!\n";
	# } else {
		# print "Email sent successfully.\n";
	# }

# } #----------------------------------------------------------------------------------------

# sub common_v2_send_smtp_mail {
	
	# my ($from,$to,$cc,$bcc,$subj,$msg,$table_msg,$html,$thead,$attachpath,$attachname,$attachapplication,$uploaded_doc,$operator_email) = @_ ;
	
	# use Net::SMTPS;

	# # SMTP configuration
	# my $smtp_host = $afrihost_smtp ;
	# my $smtp_port = $afrihost_port ;
	# my $smtp_user = $afrihost_from ;
	# my $smtp_pass = $afrihost_psw ;

	# $from         = $afrihost_from unless $from ;

	# # Create SMTPS object
	# my $smtp = Net::SMTPS->new(
		# $smtp_host,
		# Port     => $smtp_port,
		# doSSL    => 'ssl',         # Ensure SSL from start (port 465)
		# Timeout  => 30,
		# Debug    => 1,             # Optional: for debug output
	# );

	# # Authenticate
	# $smtp->auth($smtp_user, $smtp_pass) or die "SMTP Auth failed: $!";

	# # Send the email
	# $smtp->mail($from);
	# $smtp->to($to);
	
	# my $body = ($html) ? &common_get_mail_body($msg,$table_msg) : $msg  ;

	# $smtp->data();
	# $smtp->datasend("From: $from\n");
	# $smtp->datasend("To: $to\n");
	# $smtp->datasend("Subject: $subj\n");
	# $smtp->datasend("MIME-Version: 1.0\n") if $html ;
	# $smtp->datasend("Content-Type: text/html; charset=UTF-8\n") if $html ;
	# $smtp->datasend("\n");  # empty line between headers and body
	# $smtp->datasend("$body\n");
	# $smtp->dataend();

	# $smtp->quit;

	# # print "Email sent successfully.\n";
	
# } #----------------------------------------------------------------------------------------

# sub common_get_mail_body {
	
	# my ($email_msg,$table_msg) = @_ ;
	
	# my $mail_body = <<END_OF_BODY;
		# <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
		# <html>
		# <head>
		# <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
		# <title></title>

		# <style type="text/css">
		# body { background-color:#ffffff; color:#222222; }
		# tbody tr td { font-size: 11px; }
		# .table th{background-color:#fff !important}.btn>.caret,.dropup>.btn>.caret{border-top-color:#000 !important}.label{border:1px solid #000}.table{border-collapse:collapse !important}.table-bordered th,.table-bordered td{border:1px solid #ddd !important}
		# .table-striped>tbody>tr:nth-child(odd)>td,.table-striped>tbody>tr:nth-child(odd)>th{background-color:#EEEEEE; color:#222222; }
		# .table-striped>tbody>tr:nth-child(even)>td,.table-striped>tbody>tr:nth-child(even)>th{ background-color:#A4A2A2; color:#ffffff; }
		# .table-striped>tbody>tr:nth-child(odd)>td a:not(.btn) { text-decoration: none; color: #FF6600; font-weight: bold; }
		# .table-striped>tbody>tr:nth-child(even)>td a:not(.btn) { text-decoration: none; color: #EC1C24; font-weight: bold; }
		# .table>thead>tr>th, .table>tbody>tr>th, .table>tfoot>tr>th, .table>thead>tr>td, .table>tbody>tr>td, .table>tfoot>tr>td { height: 8px; vertical-align: middle; }
		# </style>

		# </head>
		# <body style="background-color: #FFFFFF; font-family: Arial, Helvetica, sans-serif; font-size: 13px">

		# <div align="center">

		# <a href="https://interactivetvafrica.com/"><img alt="$useropts{short}" src="https://$ENV{SERVER_NAME}/img/email_logo.jpg" border="0"></a>
		# <br><br>
		# $email_msg
		# <br><br>
		# <table id="safet-table" class="table table-striped table-bordered bootstrap-datatable datatable responsive">    
			# $thead
			# <tbody>
				# $table_msg
			# </tbody>
		# </table>

		# <br>
		# <p>$display_notif_msg
		# Best regards,
		# <br>
		# <br>
		# The $useropts{short} Team.
		# </p>
		# </div>
		# </body>
		# </html>
# END_OF_BODY

	# return ($mail_body) ;

# } #----------------------------------------------------------------------------------------

use today ;
use common ;
# use mailsend ;

1;