#!/usr/bin/perl -w

#
# Usage:
#
# echo dpavlin@pliva.hr Dobrica Pavlinusic | ./send-bulk.pl
# echo first.last@pliva.hr | ./send-bulk.pl
#

my $debug=0;

my @emails;
my @fullnames;

use strict;

while (<>) {
	chomp;

	s/\015//g;				# kill cr

	tr/šðžèæŠÐŽÈÆ/¹ð¾èæ©Ð®ÈÆ/;	# 1250 -> iso8859-2

	s/^"//;
	s/"$//;
	s/\t"/\t/g;
	s/"\t/\t/g;
	s/  */ /g;

	next if (/^#/ || /^$/ || /^\s+$/);

	my ($email,$fullname);

	if (m/^(\w+)\.([\w-]+)\@[.\w-]+$/) {
		$fullname=ucfirst($1)." ".ucfirst($2);
		$email=$_;
		print "Heuristic: $_ -> $fullname <$email>\n" if ($debug);
	} elsif (/\s/) {
		($email,$fullname)=split(/\s+/,$_,2);
	} else {
		$email = $_;
		$fullname = "";
	}

	die "usage:\necho email\@dom first last name | $0\n[input: $_]\n" if (!defined($fullname) || !defined($email) || $email !~ m,@,);

	push @emails,$email;
	push @fullnames,$fullname;

}

print "Collected ",($#emails+1)," e-mail addresses...\nSending...\n";

foreach my $email (@emails) {
	my $fullname = shift @fullnames;

	if (defined $debug && $debug) {
		open(MAIL,">> /tmp/mailfoo") || die "$!";
	} else {
		open(MAIL,"| /usr/lib/sendmail -t") || die "sendmail: $!";
	}

	my $in_mail="mail.txt";

	open(IN,"$in_mail") || die "in mail: $!";
	my $headers=1;
	while(<IN>) {
		# ship some of headers in outgoing mail
		next if ($headers && (/^From / || /^Status: / || /^X-Mutt\S*: /));
		$headers=0 if (/^$/);
		chomp;
		chomp;
		if ($fullname && $fullname ne "") {
			s/^To: ###email###.*$/To: $fullname <$email>/g;
		} else {
			s/^To: ###email###.*$/To: $email/g;
		}
		print MAIL "$_\n";
	}
	close(IN);
	close(MAIL);
#	print "." if ($debug);
	print "$fullname <$email>\n";
}

1;

__END__

=head1 e-mail forge

this is strictly prohibited by netiquette

=head2 Create e-mail message using mail client in file mail.txt

e.g. Use mutt to create e-mail message and postpone it. Usage of mutt or some other mail client is recommended if you want to include attachements in your e-mail message. Otherwise, you can also use any editor (vi...). Then move file with postponed message to mail.txt and edit From: header so that it apperas as requested.

It should have at least following headers:

 From: PCST happy support <pcst@pliva.hr>
 To: ###email###
 Subject: Happy, happy, joy, joy

 body of message

Token ###email### is placeholder which will be replaced by full name of person and an e-mail address.

The script will automaticlly remove any From e-mail date header (which separates message in postponed messages folder), Status: headers which marks message as allready opened and X-Mutt header.

=head2 Create list of e-mail addresses for distribution

List should be in following format:

 e-mail@domain.bla First Last name

e-mail address and First and last name must be whitespace (space, tab)
separated.

=head2 Start send-bulk.pl script

You should be in directory which has mail.txt (template mail), and list of e-mail addresses (list.txt in this example) and then do:

 send-bulk.pl < list.txt

=head2 Having problems?

Optional step is to check if line

 #my $debug=1
 
in send-bulk.pl is commeted (that's the # at beginning of the line), because if it isn't, it won't send any mails, just create temp file /tmp/mailfoo for debugging.

=cut
