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

Hello, I'm working on our company newsletter, for our employees, that is sent out via email. What I am trying to do is to replace a bracketed tag NAME, that is embedded within the template, with the name of the person in the database. What I am finding is that the first person's name in the db is being retained in the template when being sent instead of being replaced by the person the message is intended for. What step am I missing to resolve this issue?
open (FILE, "$datadir/sendme.txt") || &error_message("Can't file temp + file for sending mail - $datadir/sendme.txt."); $sendthis = <FILE>; close(FILE); if ($action eq 'moremail') { $osubject = $INFO{'osubject'}; $usename = $INFO{'usename'}; $obname = $INFO{'obname'}; $oaname = $INFO{'oaname'}; $list = $INFO{'list'}; $osubject =~ s/_/ /g; $osubject =~ s/%26/\&/g; $oaname =~ s/_/ /g; $oaname =~ s/%26/\&/g; $obname =~ s/_/ /g; $obname =~ s/%26/\&/g; } else { $startat = 0; } open (FILE, "$datadir/$list\.txt") || die "Can't open $datadir/lists\ +.txt\n"; @info = <FILE>; close(FILE); $num = @info; $endat = $startat + $increment; $lastone = 0; if ($num < $endat) { $endat = $num; $lastone = 1; } for ($a = $startat; $a < $endat; $a++) { ($name,$recipient,$nochop) = split(/,/,$info[$a]); if ($recipient =~ /.*\@.*\..*/) { open (MAIL, "|$mailprog") || die "Can't open $mailpro +g!\n"; print MAIL "To: $recipient\n"; print MAIL "From: $omultiname < $omultimail >\n"; print MAIL "Subject: $osubject\n"; print MAIL "Content-type: text/html;\n\n +"; if ($usename eq 'yes') { print MAIL "$obname $name$oaname<BR>\n\n"; $sendthis=~s/\[NAME\]/$name/gie; } print MAIL $sendthis, "\n"; close (MAIL); } }
Any help would be greatly appreciated! Lisaw

Replies are listed 'Best First'.
Re: Replacing a [TAG] in a Email Template
by Enlil (Parson) on Apr 18, 2003 at 19:00 UTC
    Your Question is pretty similar to this thread, I think you will find both answers to that thread pretty relevant to your question.

    -enlil

      Yeah - the problem seems to be identical. I would like just to add that Template Toolkit has a database plugin.
      I figured it out. I just had to reset the template to its original layout by re-opening the template after sending each email.
      print MAIL $sendthis, "\n"; close (MAIL); open (FILE, "$datadir/sendme.txt") || &error_message("Can't file temp +file for sending mail - $datadir/sendme.txt."); $sendthis = <FILE>; close(FILE); } }
      Have a Happy Easter!

        Instead of re-reading your template file each time (remember - disk I/O is expensive), read the template once and then manipulate a copy for each email you send. Take a look at the following (very rough code)...

        $sendthis = <FILE>; foreach my $mailrecord (@list_of_addresses_and_names_etc) { my $msg = $sendthis; # manipulate the $msg and send }
Re: Replacing a [TAG] in a Email Template
by lachoy (Parson) on Apr 18, 2003 at 21:33 UTC
    The Mail::Bulkmail module is built to handle this sort of thing.

    Chris
    M-x auto-bs-mode