package Email;

use warnings;
use strict;

our $VERSION = '2.0.1';

for my $data (qw(mailprog testing sender recipients subject textMessage htmlMessage logfile)) {
  no strict "refs";
  *$data = sub {
    my $self = shift;
    $self->{uc $data} = shift if @_;
    return $self->{uc $data};
  }
}

sub new {
  my $invocant = shift;

  $invocant = ref $invocant || $invocant;

  my $self = {
              SENDER => undef,
              RECIPIENTS => [],
              SUBJECT => undef,
              TEXTMESSAGE => undef,
              HTMLMESSAGE => undef,
	      TESTING => 0,
              MAILPROG => '/usr/sbin/sendmail',
	      LOGFILE => undef,
             };

  bless($self, $invocant);

  return $self->_init(@_);
}
sub DESTROY {}

sub _init {
  my $self = shift;
  my %params = @_;

  if ($params{'Sender'}) { $self->sender($params{'Sender'}); };
  if ($params{'Subject'}) { $self->subject($params{'Subject'}); };
  if ($params{'TextMessage'}) { $self->textMessage($params{'TextMessage'}); };
  if ($params{'HtmlMessage'}) { $self->htmlMessage($params{'HtmlMessage'}); };
  if ($params{'MailProg'}) { $self->mailprog($params{'MailProg'}); };
  if ($params{'LogFile'}) { $self->logfile($params{'LogFile'}); };

  return $self;
}

################################################################################
sub addRecipient {
  my ($self, $recipient) = @_;

  push @{$self->recipients}, $recipient;
}

################################################################################
sub clearRecipients {
  my ($self) = @_;

  $self->recipients( [] );
}

################################################################################
sub getRecipient {
  my ($self, $email) = @_;
  return $self->recipients->{$email};
}

################################################################################
sub sendText {
  my ($self, $recipient) = @_;

  ### Do some error checking before sending out
  return unless $self->_checkText;

  if (defined $recipient) {
    $self->_sendText($recipient);
  } else {
    $self->_sendText($_) for @{$self->recipients};
  }

  return 1;
}

################################################################################
sub sendTextHtml {
  my ($self, $recipient) = @_;

  ### Do some error checking before sending out
  return unless $self->_checkTextHtml;

  if (defined $recipient) {
    $self->_sendTextHtml($recipient);
  } else {
    $self->_sendTextHtml($_) for @{$self->recipients};
  }

  return 1;
}

################################################################################
sub _checkText {
  my ($self) = @_;

  warn "Need a sender to send mail\n" unless $self->sender;
  warn "Need at least one recipient to send mail\n" unless $self->recipients;
  warn "Need a subject to send mail\n" unless $self->subject;
  warn "Need a text message to send mail\n" unless $self->textMessage;

  return 1 if $self->sender && $self->recipients &&
    $self->subject && $self->textMessage;
}

################################################################################
sub _checkTextHtml {
  my ($self) = @_;

  warn "Need an html message to send mail in html\n" unless $self->textMessage;

  return 1 if $self->_checkText && $self->htmlMessage;
}

################################################################################
sub _sendText {
  my ($self, $recipient) = @_;
  my $tm = $self->textMessage;

  #### Fix substitution tags
  $tm =~ s/<name>/$recipient->{name}/g;

  ### Testing???
  if ($self->testing) {
    print STDERR "Sending mail...\n";
    print STDERR "\tSender = ", $self->sender, "\n";
    print STDERR "\tRecipient = $recipient->{name} ($recipient->{email})\n";
    print STDERR "\tSubject = ", $self->subject, "\n";
    #print STDERR "\tText Message = $tm\n";
    return;
  }

  open(MAIL, ("|" . $self->mailprog . " -t"));
  print MAIL "To: $recipient->{email}\n";
  print MAIL "From: ", $self->sender, "\n";
  print MAIL "Subject: ", $self->subject, "\n\n";
  print MAIL "$tm\n";
  close (MAIL);

  ### Handle logging if we need to
  if ($self->logfile) {
    open (FILE, ">>" . $self->logfile) or die "Cannot open logfile: ", $self->logfile, ", $!\n";
    print FILE "Name: $recipient->{name}; Email: $recipient->{email}\n";
    close FILE;
  }
}

################################################################################
sub _sendTextHtml {
  my ($self, $recipient) = @_;
  my $hm = $self->htmlMessage;
  my $tm = $self->textMessage;

  #### Fix substitution tags
  $hm =~ s/<name>/$recipient->{name}/g;
  $hm =~ s/<email>/$recipient->{email}/g;
  $tm =~ s/<name>/$recipient->{name}/g;

  ### Testing???
  if ($self->testing) {
    print STDERR "Sending mail...\n";
    print STDERR "\tSender = ", $self->sender, "\n";
    print STDERR "\tRecipient = $recipient->{name} ($recipient->{email})\n";
    print STDERR "\tSubject = ", $self->subject, "\n";
    print STDERR "\tText Message = $tm\n";
    print STDERR "\tHTML Message = $hm\n";
    return;
  }

  open(MAIL, ("|" . $self->mailprog . " -t"));
  print MAIL "To: $recipient->{email}\n";
  print MAIL "From: ", $self->sender, "\n";
  print MAIL "Subject: ", $self->subject, "\n";
  print MAIL "MIME-Version: 1.0\n";
  print MAIL "Content-Type: multipart/alternative;\n";
  print MAIL "	boundary=\"----=_NextPart_000_02F0_01C1BD8D.568C7AD0\"\n\n";
  print MAIL "This is a multi-part message in MIME format.\n\n";
  print MAIL "------=_NextPart_000_02F0_01C1BD8D.568C7AD0\n";
  print MAIL "Content-Type: text/plain;\n";
  print MAIL "	charset=\"us-ascii\"\n";
  print MAIL "Content-Transfer-Encoding: 7bit\n\n";
  print MAIL $tm;
  print MAIL "\n\n------=_NextPart_000_02F0_01C1BD8D.568C7AD0\n";
  print MAIL "Content-Type: text/html;\n";
  print MAIL "	charset=\"us-ascii\"\n";
  print MAIL "Content-Transfer-Encoding: 8bit\n\n";
  print MAIL $hm;
  print MAIL "\n\n------=_NextPart_000_02F0_01C1BD8D.568C7AD0--\n";
  close (MAIL);

  ### Handle logging if we need to
  if ($self->logfile) {
    open (FILE, ">>" . $self->logfile) or die "Cannot open logfile: ", $self->logfile, ", $!\n";
    print FILE "Name: $recipient->{name}; Email: $recipient->{email}\n";
    close FILE;
  }
}

1;
