Dear All,

I'm having trouble with processing my template and the text coming out
only once. I think it's to do with the scalar ref output.

I have tried many things, but now stuck. It's $email_output only getting
processed once, i.e. no content in it the other 3 times in the loop "for
my $queue ( sort keys %queues ) {"

Thanks.

#!/usr/bin/perl

use strict;
use warnings;
use RT::Client::REST;
use DateTime;
use Text::SimpleTable;
use Data::Dumper;
use MIME::Lite;
use Template;

my $user   = 'xxx';
my $pass   = 'xxx';
my $dt     = DateTime->now;
my $ymd    = $dt->clone->subtract( months => 1 )->ymd;
my %queues = (
    1 => '[EMAIL PROTECTED]',
    2 => '[EMAIL PROTECTED]',
    3 => '[EMAIL PROTECTED]',
    4 => '[EMAIL PROTECTED]',
);

my $rt = RT::Client::REST->new(
    server  => 'http://xxx.xxx.com',
    timeout => 30,
);

$rt->login( username => $user, password => $pass )
  or die "Problem logging in: $!\n";

for my $queue ( sort keys %queues ) {
    my $email_output;
    my $support_info;
    my $tt = Template->new();
    my $tt_vars = { client => $queue, };

    my %searches = (
        "Created Requests:"    => "Created > '$ymd' AND Queue = '$queue'",
        "Resolved Requests:"   => "Resolved > '$ymd' AND Queue = '$queue'",
        "Unresolved Requests:" => "Status = 'open' AND Queue = '$queue'",
    );
    for my $search ( sort keys %searches ) {
        my $t = Text::SimpleTable->new(
            [ 11, 'ID' ],
            [ 60, 'Subject' ],
            [ 25, 'Requestor' ],
            [ 25, 'Created' ],
        );

        #print $search, "\n";
        my @ids = $rt->search(
            type  => 'ticket',
            query => $searches{$search},
        );

        for my $id ( sort @ids ) {
            my ($ticket) = $rt->show( type => 'ticket', id => $id );
            $t->row(
                $ticket->{id},         $ticket->{Subject},
                $ticket->{Requestors}, $ticket->{Created},
            );
        }
        $support_info .= "\n" . $search . "\n" . $t->draw;
    }

    # __END__ is the DATA file handle
    $tt->process( \*DATA, $tt_vars, \$email_output )
      || die $tt->error(), "\n";

    print Dumper($email_output), "\n";

    my $msg = MIME::Lite->new(
        From    => 'xxx',
        To      => 'xx',
        Subject => "Report",
        Type    => 'multipart/mixed',
    );

    # Add Text
    $msg->attach(
        Type => 'TEXT',
        Data => $email_output,
    );

    # Type for .txt
    $msg->attach(
        Type => 'TEXT',

        #Encoding => 'base64',
        Data     => $support_info,
        Filename => 'Report.txt',
    );

    # Type for PDF
    $msg->attach(
        Type => 'application/pdf',

        #Encoding => 'base64',
        Data     => $support_info,
        Filename => 'Report.pdf',
    );

    # Fire off our message
    $msg->send();

    # Reset search results
    $support_info = undef;
}

__END__
Dear [% client %],

As part of your Support Contract, we are sending you
a summary of your Support Requests for the past month
in Text and PDF formats.

Please print or save the PDF for your records.

Thank you.

--
Kind Regards,

The Support Team.

-- 
Walking the road to enlightenment... I found a penguin and a camel on the
way..... Fancy a [EMAIL PROTECTED] Just ask!!!
http://perlmonks.org/?node_id=386673

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to