Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am helping a friend build a list emailer, for his subscribed users. We where trying to use Mail::Bulkmail by Jim Thomason, but we just could not get it to work, it appears to be having a problem, and he won't respond to any questions, so we decided just to make one.

Here is what I'm doing. He is getting the information in a set pattern, like this example:

$_mergeFields = "##fname##;##lname##;##email##;##phone##;"; $_list_to_send_to = "Richard;Smith;myemail\@testmail.com;8005551212;|T +oby;Johnson;email2\@testmail.com;8885551212|Chanty;Perkins;another\@t +est.com;8665551212;";
There are the same amount of variables in the one that shows the order.

Now the message might be something like this:
$_message = qq~ Hello ##fname## how are you today? Hopefully your day is going well. I + am the list server for some company, blah blah blah, and I have list +ed your phone number as ##phone##. Well, here is all the data I have +for you: Name: ##fname## ##lname## Phone: ##phone## email: ##email## So I just wanted to say thank you for signing up yesterday to get emai +ls from us, and if you don't want to receive any more just click the +link below and we won't send any more out. blah blah blah blah~;
Of course this email list is just for a business and when the emails go out, it is for the newsletter. So it is not for spam or anything, everyone is double opted in.

Anyhow, I am trying to find the easiest way to take the merge fields and make a hash so that I can then swap out the data something like this:
$_field = 0; %_merData = (); %_mergedData = (); while($_mergeFields) { $_field++; ($_tempVar,$_mergeFields) = split /\;/, $_mergeFields, 2; $_merData{$_field} = $_tempVar; } $_field = 0; while($_list_to_send_to) { ($_tempVar,$_list_to_send_to) = split /\|/, $_list_to_send_to, 2; my $_tempMailMessage = $_message; my $_tempSubject = $_subject; while($_tempVar) { $_field++; ($_tempVar2,$_tempVar) = split /\;/, $_tempVar, 2; $_mergedData{$_field} = $_tempVar2; } foreach $key (key(%_merData)) { $_tempMailMessage =~ s/$_merData{$key}/$_mergedData{$key}/ig; $_tempSubject =~ s/$_merData{$key}/$_mergedData{$key}/ig; if($_merData{$key} =~ /email##$/) { $__to = $_mergedData{$key}; } } go_send_the_message($__to,$_tempSubject,$_tempMailMessage); }
Something like that. That is not the way it should be done, but I don't know another way to do it. Do you? Can you point me in the right direction?
Thank you.

Richard

Replies are listed 'Best First'.
Re: merging two fields of data into one hash or array
by f00li5h (Chaplain) on Mar 24, 2007 at 04:22 UTC

    There are many fine templating engines out there, Template::Toolkit being the most featureful, and pleasant. There are also HTML::Template and Text::Template

    Since you're not doing anything too fancy, Text::Template will most likely be enough.

    you will have to split your strings into proper data structures before you pass them to the template engine, but that's not too hard.

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
Re: merging two fields of data into one hash or array
by bobf (Monsignor) on Mar 24, 2007 at 04:40 UTC

    As f00li5h pointed out, the best approach would be to use a templating module.

    To pick up where he left off and provide an example of how you can use split to separate your input data, I submit the following code:

    use strict; use warnings; 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;"; my $_message = qq~ Hello ##fname## how are you today? Hopefully your day is going well. I + am the list server for some company, blah blah blah, and I have list +ed your phone number as ##phone##. Well, here is all the data I have +for you: Name: ##fname## ##lname## Phone: ##phone## email: ##email## So I just wanted to say thank you for signing up yesterday to get emai +ls from us, and if you don't want to receive any more just click the +link below and we won't send any more out. blah blah ~; my @fields = split( ';', $_mergeFields ); my @recipients = split( '\|', $_list_to_send_to ); foreach my $recipient ( @recipients ) { my @recip_data = split( ';', $recipient ); my $recip_msg = $_message; foreach my $field ( @fields ) { my $value = shift @recip_data; $recip_msg =~ s/$field/$value/g; } print $recip_msg, "\n\n"; }

    Note: Don't use this in production - use a templating module.

      I'm more of a fan of stuffing the customer info into a hash

      the stuff in <readmore> tags is not as interesting

      # 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[
      ]; 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_rec +ord; send_spam $customer_record{email}, $message; }
      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

      Update

      1. I won't ever use <readmore> tags for code folding again
      2. It's nice that my answer is in some way related to the question, as well as TIMTOWTDI
Re: merging two fields of data into one hash or array
by dReKurCe (Scribe) on Mar 24, 2007 at 14:11 UTC
    Greets: Your procedural approach was trippy.

    I tried to preserve the flavour and wrote a
    dumper like explanation. See that offset?
    Well thats the trip, on the recipient stack.
    None the less,heres the gist:

    interoffice memo to learn modular arithmetic to---- >myemail@testmail.com interoffice memo to learn modular arithmetic to---- >email2@testmail.com interoffice memo to learn modular arithmetic to---- >anothe@test.comn interoffice memo to learn modular arithmetic to---- >yaea@sendmail.edu
    accomplished with:

    #! /usr/bin/perl $_mergeFields="##fname##;##lname##;##email##;##phone##"; $_list_to_send_to= "Richard;Smith;myemail\@testmail.com;8005551212| Toby;Johnson;email2\@testmail.com;8885551212| Chanty;Perkins;another\@test.comn;8665551212| Eli;Codernaught;yaea\@sendmail.edu;8225551212"; %_merData = (); %_mergedData = (); $_field = 0; while($_mergeFields){ $_field++; ($_tempVar,$_mergeFields)= split /\;/, $_mergeFields,2; $_merData{$_field} = $_tempVar; print "FieldsStack=$_field:\n". "pop \t$_tempVar\n". "==Fields stack:$_mergeFields\n"; } $_field = 0; $_recipient_list=0; while($_list_to_send_to) { $_recipient_list++; ($_tempVar,$_list_to_send_to) = split /\|/,$_list_to_send_to, +2; my $_tempMailMessage = $_message; my $_tempSubject = $_subject; print "\nRecipient Stack:\n$_list_to_send_to\n". "pop array:$_tempVar\n"; while($_tempVar) { $_field++; ($_tempVar2,$_tempVar) = split /\;/, $_tempVar +, 2; $_mergedData{$_field} = $_tempVar2; print "FieldStack=$_field:\n". "pop \t $_tempVar2\n". "==Fields stack: $_tempVar\n". "-----------------\n"; }} for $key (keys(%_merData)) { $_tempMailMessage =~ s/$_merData{$key}/$_mergedData{$key}/ig; $_tempSubject =~ s/$_merData{$key}/$_mergedData{$key}/ig; $copykey=$key+1; if($_merData{$key} =~ /email##$/) { $coef=$recipx*$copy +key; $ultrakey=$coef-1; $__to=$_mergedData{$ultrakey}; print "\n\ninteroffice memo to learn modular arithmetic to---- +>$__to\n"; }}} #go_send_the_message($__to,$_tempSubject,$_tempMailMessage);} for $recipx(1...$_recipient_list){
    Which you might want to run. Happy Coding