use MIME::Lite;
use Net::SMTP;
use Net::SMTP::TLS;
use MIME::Base64;
use Authen::SASL;

my $guser = 'polyiamond@gmail.com';
my $gpass = 'LarinA53';

my $nuser = 'john.oscarkilo';
my $npass = 'larina702';

# my $tofile = 'devoncc.csv';
# my $tofile = 'lancscc.csv';
my $tofile = 'mpsxxx.csv';
my $from_addr = '"John McKay" <john@liburne.com>';
my $from_addr_g = '"John McKay" <polyiamond@gmail.com>';
my $mail_host = 'mail01.4relay.net';
my $mail_host_g = 'smtp.gmail.com';
my $msgfile = 'msg.txt';

my $enablepdf = 1;
my $htmlpath = '/jak/mps/xx/';
my $letter  = 'letter.html';
my $letterpath = $htmlpath.$letter;
my $pdfpath = 'referendum.pdf';

### Adjust subject and body message
my $subject = 'The Referendum';

open HFILE, $msgfile or die "failed to open $msgfile ($!)\n";
$mess = '';
while ($line = <HFILE>) {
  chomp($line);
  $mess .= "$line\n";
}
close HFILE;

open HFILE, $letterpath or die "failed to open $letterpath ($!)\n";
$lett = '';
while ($line = <HFILE>) {
  chomp($line);
  $lett .= "$line\n";
}
close HFILE;

my $count   = 0;
my $gcount  = 0;
my $enabled = 1;

open HFILE, $tofile or die "failed to open $tofile ($!)\n";
@csvlines = <HFILE>;
close HFILE;

foreach $csvline (@csvlines) {
print "LINE=$csvline\n";
  chomp($csvline);
  if ($csvline eq '') { next; }
  $count++;

  my $auth = 0;
  my @part = split(/\|/, $csvline);

  my $name = $part[0];
  my $email = $part[3];
  my $party = $part[1];
  if ($party == $party) {
#  if (!($party =~ /labour/i)) {
#  $email = 'polyiamond@gmail.com';
    my $to_address = '"'.$name.'" <'.$email.'>';

    $from_address = $from_addr;
    $mailhost = $mail_host;
    $user = $nuser;
    $pass = $npass;
    if ($to_address != $to_address) {
#    if ($to_address =~ m/yahoo|btconnect|hotmail|btinternet|pipex|aol|blueyonder|oscarkilo/i) {
      $auth = 1;
      $from_address = $from_addr_g;
      $mailhost = $mail_host_g;
      $gcount++;
      $user = $guser;
      $pass = $gpass;
    }

    print "$count - ($gcount) - sending email from: $from_address to: $name - $to_address \n";

    if ($enablepdf == 1) {  &createPDF($name); }

    my $message_body = $mess;

    $message_body =~ s/\{name\}/$name/g;

    if ($enabled == 1) {

      ### Create the multipart container
      $msg = MIME::Lite->new (
        From => $from_address,
        To => $to_address,
        Subject => "a/o $name - $subject",
        Type =>'multipart/mixed'
      ) or die "Error creating multipart container: $!\n";

      ### Add the text message part
      $msg->attach (
        Type => 'TEXT',
        Data => $message_body
      ) or die "Error adding the text message part: $!\n";

      ### Add PDF file
      if ($enablepdf == 1) {
        $msg->attach (
           Type => 'application/pdf',
           Path => $pdfpath,
           Filename => $pdfpath,
           Disposition => 'attachment'
        ) or die "Error adding $pdfpath: $!\n";
      }

      ### Send the Message
      if ($auth == 0) {
        $smtp = Net::SMTP->new($mailhost, Port=>25);
        $smtp->auth($user, $pass) or die "Can't authenticate:".$smtp->message();
        $smtp->mail($from_address);
        $smtp->to($to_address);
        $smtp->data();
        $smtp->datasend($msg->as_string);
        $smtp->dataend();
        $smtp->quit();
      }
      else {
        print "using TLS via $mailhost\n";
        $smtp = new Net::SMTP::TLS($mailhost,	Hello=>$mailhost, Port=>587, User=>$user, Password=>$pass, Debug=>1);
        $smtp->mail($from_address);
        $smtp->to($to_address);
        $smtp->data();
        $smtp->datasend($msg->as_string);
        $smtp->dataend();
        $smtp->quit();
      }

    }

#  MIME::Lite->send('smtp', $mail_host, Timeout=>60);
#  $msg->send;

    sleep(3);
##    system("rm -f $pdfpath");
  }
}
close HFILE;

sub createPDF {
  my $fullname = shift(@_);
  my @lines = ();

  my $opletterpath = $letterpath;
  $opletterpath =~ s/$letter/op$letter/;

  $letto = $lett;
  $letto =~ s/NNNNN/$fullname/sg;

  open LFILE, '>'.$opletterpath or die "failed to open $opletterpath ($!)\n";
  print LFILE "$letto\n";
  close LFILE;

  $cmd = '/jak/00__scripts/wkhtmltopdf-amd64 http://stopit.org.uk/xx/op'.$letter.' '.$pdfpath;
  system($cmd);
}

