in reply to Re: search replace (interpolation)
in thread search replace (interpolation)

What I am trying to do is replace the envelope headers of a email address dynamically.

The psuedo is as follows:

Read email template with tokens in the envelope headers:
Message-ID: <1039751337.22128@idhost> Content-Transfer-Encoding: 8bit Content-Type: multipart/alternative; boundary="_----------=_1039751337 +22128" MIME-Version: 1.0 Date: Thu, 12 Dec 2002 19:48:57 PST From: __FULLFROM__ <__FROM__> To: __FNAME__ __LNAME__ <__EMAIL__> Subject: __SUBJECT__ Precedence: bulk

I now break up the email template into two parts. 1 is a hash containing all the header information the other is a variable with the body.

I then want to hit a db and dynamically replace the headers with the fields in the db and send out the email.

Replies are listed 'Best First'.
Re^3: search replace (interpolation)
by Aristotle (Chancellor) on Dec 16, 2002 at 20:27 UTC
    So put the information you retrieve from the database in a hash with the keys FULLFROM, FROM, FNAME, LNAME, EMAIL, and SUBJECT.
    my %header_line = read_header_or_something($mail_file); while(my %recipient_info = get_info_from_db()) { while(my ($header, $value) = each %header_line) { $value =~ s/__(.+?)__/$recipient_info{$1}/g; print "$header: $value\n"; } }

    And I hope you're not sending the mail to people who haven't opted in on their own.. :)

    Update: doh, silly me for forgetting to mention you would be better off with a templating system. Unlike UnderMine, I would propose the more generic and powerful Template Toolkit though.

    Makeshifts last the longest.

      This sounds like a perfect case for using a standard templating module. Using say HTML::Template you end up with very simple template and perl coding.
      Message-ID: <1039751337.22128@idhost> Content-Transfer-Encoding: 8bit Content-Type: multipart/alternative; boundary="_----------=_1039751337 +22128" MIME-Version: 1.0 Date: Thu, 12 Dec 2002 19:48:57 PST From: <!-- TMPL_VAR NAME=FULLFROM --> <<!-- TMPL_VAR NAME=FROM -->> To: <!-- TMPL_VAR NAME=FNAME --> <!-- TMPL_VAR NAME=LNAME --> <<!-- TM +PL_VAR NAME=EMAIL -->> Subject: <!-- TMPL_VAR NAME=SUBJECT --> Precedence: bulk
      then the perl is trivial
      while(my %recipient_info = get_info_from_db()) { my $template = HTML::Template->new(filename => 'email.tmpl'); $template->param(%recipient_info); print $template->output(); }
      Hope this helps
      UnderMine
      thanks all for the help.
Re: Re: Re: search replace (interpolation)
by tachyon (Chancellor) on Dec 16, 2002 at 20:31 UTC

    This would be a hell of a lot more robust, maintainable and less obfuscated.

    my $email_template = get_email_template_as_string(); my $sql = 'SELECT first_name, last_name, email FROM address_book'; my $sth = $dbh->prepare_cached{$sql} or die 'Error'; while ( my ($first_name, $last_name, $email) = $sth->fetchrow_array() +) { my $text = $email_template; $text =~ s/__FNAME__/$first_name/; $text =~ s/__LNAME__/$last_name/; # contiue with substitutions send_email($text); }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print