#! /usr/bin/perl
use strict;
sub send_spam { print @_; }
my $_mergeFields = "##fname##;##lname##;##email##;##phone##;";
my $_list_to_send_to = "Richard;Smith;myemail\@testmail.com;8005551212;|Toby;Johnson;email2\@testmail.com;8885551212|Chanty;Perkins;another\@test.com;8665551212;";
####
# trim the # marks
$_mergeFields =~ s/#//g;
# list of field names
my @field_names = split ';', $_mergeFields;
# list of lines from the data
my @records = split /[|]/, $_list_to_send_to;
my $template = q[
####
dear ##fname##
great prices in \|iaqra and molex watches available
speial offer for you now
exclusive offer sent to ##email##
####
];
foreach( @records ) {
my $message = $template;
# hash to fill with customer information
my %customer_record;
# use a hash slice to populate it,
# we now have keys fname lname email and phone
@customer_record{ @field_names } = split ';', $_ ;
# neat, a one line templating engine.
$message =~ s/##$_##/$customer_record{$_}/g for keys %customer_record;
send_spam $customer_record{email}, $message;
}