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

use CGI::Carp qw(fatalsToBrowser);

# &common_send_localhost_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...") ;
&common_v4_send_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...") ;

# &common_v4_email_with_attachments('events@itvadmin.co.za','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...",$table_msg,1,$thead,"$htmlpath/pdf/operator_flyer",'operator_email_attach.pdf','pdf') ;

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

sub common_v4_send_mail {
	
	my ($to,$cc,$bcc,$html,$thead,$msg,$subj,$tmsg) = @_ ;

	return unless $msg ;
	return unless $subj ;
	
	# my $cc  = 'cc1@example.com, cc2@example.com'; # multiple cc's
	# $smtp->to(split(/\s*,\s*/, $cc));  # split and add each
	
	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 ;

	# # 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: $!";
	
	my $smtp = Net::SMTPS->new('localhost') or die "Can't connect";

	# Send the email
	$smtp->mail($from);
	$smtp->to($to);
	# $smtp->to($cc) if $cc ;
	$cc =~ s/\;/\,/g ;
	$smtp->to(split(/\s*,\s*/, $cc)) if $cc ; # split and add each
	$bcc =~ s/\;/\,/g ;
	$smtp->to(split(/\s*,\s*/, $bcc)) if $bcc;
	
	my $body = ($html) ? &common_get_mail_body($msg,$tmsg,$thead) : $msg  ;

	$smtp->data();
	$smtp->datasend("From: $useropts{short} Events <$from>\n");
	$smtp->datasend("To: $to\n");
	$smtp->datasend("Cc: $cc\n") if $cc ;  # visible
	# Do NOT include Bcc in headers
	$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;

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

sub common_v4_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' ; # application/pdf or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
	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();

	my $smtp_msg = Net::SMTP_auth->new('localhost') or die "Can't connect";
	
	$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();

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

exit ;

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

sub common_send_localhost_mail {
	
	my ($to,$cc,$bcc,$html,$thead,$msg,$subj) = @_ ;

	return unless $msg ;
	return unless $subj ;

	use Mail::Sendmail;

	$to = $main_email unless $to ;

	my %mail = () ;

	%mail = (
			 smtp    => 'localhost',
			 From    => "$useropts{short} Events <events\@itvadmin.co.za>",
			 To      => $to,
			 Cc      => $cc,
			 Bcc     => $bcc,
			 Subject => $subj
			 );

	# $mail{smtp} = "smtp.interactivetvafrica.com";
	# $mail{Debug} = 6;
	# $mail{port} = 587;
	# $mail{port} = 465;
	# $mail{Auth} = {user => $main_email, pass => $send_mail_psw, method => "LOGIN", required => 1};
			
	$mail{body} = <<END_OF_BODY;

	$msg

END_OF_BODY

	$mail{body} = &common_get_mail_body($msg,'') if $html ;

	sendmail(%mail) ;
	
} #------------------------------------------------------------------------------------------

use today ;
use common ;

1;