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

Hi Monks!
I am trying to insert the value of my template file into the value of the email body, how could I do that, I need to send an email with my ".tmpl" file been the body of my email. This is an example, I will be using MIME::Lite to send the HTML email format if you are wondering how. But here is the code you can see it better, I hope, what I am trying to do:
#!/usr/bin/perl use strict; use CGI qw(-oldstyle_urls :standard); use CGI::Carp qw(fatalsToBrowser); use Date::Calc qw( Add_Delta_Days Decode_Date_US Today ); use HTML::Template; my $template = HTML::Template->new(filename => 'email.tmpl'); print "<h3>Email Test</h3>"; my $from = 'test@test.com'; my $to_name = 'Test Form'; my @mail_to = ( 'me@test.com' ); my $subject = "TEST TEMP\n\n"; # template parameters my $f_name = "Joe"; my $date = "02/08/2011"; my $acc = "123455667889"; my $company = "Test Company."; $template->param(f_name => $f_name); $template->param(date => $date); $template->param(acc => $acc); $template->param(company => $company); my $mail_body = $template; # not working, not the right way, still try +ing... sendmail( \@mail_to, $subject, $mail_body, $from, $to_name ); print "\nDONE\n"; __TESTDATA___ This is the email.tmpl file <table width=\"100%\" border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"0 +\" cellspacing=\"0\"> <tr> <td width=\"100%\"colspan=\"2\">&nbsp;</td> </tr> <tr> <td width=\"100%\" colspan=\"2\"><b>Test</b></td> </tr> <tr> <td width=\"30%\">&nbsp;</td><td width=\"70%\">&nbsp;</td> </tr> <tr> <td width=\"30%\">Name:</td><td width=\"70%\"><TMPL_VAR NAME=" +f_name">.</td> </tr> <tr> <td width=\"30%\">EDate:</td><td width=\"70%\"><TMPL_VAR NAME= +"date">.</td> </tr> <tr> <td width=\"30%\">Acc:</td><td width=\"70%\"><TMPL_VAR NAME="a +cc">.</td> </tr> <tr> <td width=\"30%\">Company:</td><td width=\"70%\"><TMPL_VAR NAM +E="company">.</td> </tr> </table>
Thanks for the Help!!!

Replies are listed 'Best First'.
Re: HTML Template Help!
by kennethk (Abbot) on Feb 09, 2011 at 21:50 UTC
    The potentially obvious issue is that you have my $mail_body = $template; in place of my $mail_body = $template->output; as documented in HTML::Template. I'm also not sure why you have CGI in there or where you are getting sendmail from (that's not MIME::Lite's interface), but perhaps this is more of what you need (does not run):

    #!/usr/bin/perl use strict; use CGI qw(-oldstyle_urls :standard); use CGI::Carp qw(fatalsToBrowser); use Date::Calc qw( Add_Delta_Days Decode_Date_US Today ); use HTML::Template; my $template = HTML::Template->new( filehandle => *DATA); print "<h3>Email Test</h3>"; my $from = 'test@test.com'; my $to_name = 'Test Form'; my @mail_to = ( 'me@test.com' ); my $subject = "TEST TEMP\n\n"; # template parameters my $f_name = "Joe"; my $date = "02/08/2011"; my $acc = "123455667889"; my $company = "Test Company."; $template->param(f_name => $f_name); $template->param(date => $date); $template->param(acc => $acc); $template->param(company => $company); my $mail_body = $template->output; # Corrected sendmail( \@mail_to, $subject, $mail_body, $from, $to_name ); print "\nDONE\n"; __DATA__ <table width=\"100%\" border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"0 +\" cellspacing=\"0\"> <tr> <td width=\"100%\"colspan=\"2\">&nbsp;</td> </tr> <tr> <td width=\"100%\" colspan=\"2\"><b>Test</b></td> </tr> <tr> <td width=\"30%\">&nbsp;</td><td width=\"70%\">&nbsp;</td> </tr> <tr> <td width=\"30%\">Name:</td><td width=\"70%\"><TMPL_VAR NAME=" +f_name">.</td> </tr> <tr> <td width=\"30%\">EDate:</td><td width=\"70%\"><TMPL_VAR NAME= +"date">.</td> </tr> <tr> <td width=\"30%\">Acc:</td><td width=\"70%\"><TMPL_VAR NAME="a +cc">.</td> </tr> <tr> <td width=\"30%\">Company:</td><td width=\"70%\"><TMPL_VAR NAM +E="company">.</td> </tr> </table>