Stripping HTML

I’ve got a scrip & template that saves comments or correspondence to a
customer’s e-mail archive. I’d like to save the messages as
text/plain instead of text/html. I’m working in a 3.8.5 installation
that’s been patched to 3.8.7.

I want to strip the HTML from messages posted via the web. There must
be an elegant RT way to do it, since that’s how RT sends its e-mail
for those transactions.

Here’s the relevant snippet of my template:

{$headers = $Transaction->Attachments->Next->Headers();
$headers .= "\nFrom: " . $Transaction->Attachments->Next->CreatorObj->EmailAddress unless $headers =~ m/^From: /m;
$headers .= "\nTo: " . $Ticket->QueueObj->CorrespondAddress unless $headers =~ m/^To: /m;
$headers}

{$Transaction->Attachments->Next->Content()}

Should I be trying to call AddAttachments here, or something? Any
pointers much appreciated. Thanks.

Eleanor J. (Piglet) Evans, eje@panix.com
Customer Support, (212) 741-4400

I’ve got a scrip & template that saves comments or correspondence to a
customer’s e-mail archive. I’d like to save the messages as
text/plain instead of text/html. I’m working in a 3.8.5 installation
that’s been patched to 3.8.7.

I want to strip the HTML from messages posted via the web. There must
be an elegant RT way to do it, since that’s how RT sends its e-mail
for those transactions.

So I was about to write again and ask for more help, now that I’ve
gotten much further but am still stuck. But the process of composing
the mail clarified my error, and now it works! So, thanks, guys. I
really appreciate the brainstorming. :-).

In case anyone is tackling a similar problem, I’ll share my solution
for the archives, and also ask a question – should I redo this as an
RT::Action subclass? As you can see, I’ve stuffed a lot of code in
these templates, and I seem to recall having read somewhere that is
not a recommended practice.

There are 2 scrips & associated templates, one for archiving messages
as they come in to a ticket where the customer id custom field has
already been set, and a second one for archiving past messages on a
ticket when the customer id field is updated.

Description: On Correspond or Comment Archive
Condition: User Defined
Action: Notify Other Recipients
Template: Global template: Archive
Stage: TransactionCreate

Condition:

return 0 unless ($self->TransactionObj->Type eq ‘Correspond’) or ($self->TransactionObj->Type eq ‘Comment’) or ($self->TransactionObj->Type eq ‘Create’);
return 1 if $self->TicketObj->CustomFieldValues(‘CustIDs’)->Count;
return 0;

Global template: Archive

RT-Attach-Message: yes
X-Archive-Custids: {my $custids;
my $CFV = $Ticket->CustomFieldValues(‘CustIDs’);
while (my $CID = $CFV->Next) {
$custids .= $CID->Content . ’ ';
}
$custids}

Please archive me in CustIDs: {my $custids;
my $CFV = $Ticket->CustomFieldValues(‘CustIDs’);
while (my $CID = $CFV->Next) {
$custids .= $CID->Content . ’ ';
}
$custids}

{my $content = $Transaction->ContentObj;
my $headers = $content->Headers();
$headers .= "\nFrom: " . $Transaction->CreatorObj->EmailAddress unless $headers =~ m/^From: /m;
$headers .= "\nTo: " . ($Transaction->Type eq ‘Comment’ ? $Ticket->QueueObj->CommentAddress : $Ticket->QueueObj->CorrespondAddress) unless $headers =~ m/^To: /m;
$headers .= "\nDate: " . $Transaction->Created unless $headers =~ m/^Date: /m;
$headers .= "\nSubject: " . $Transaction->Subject || $Ticket->Subject unless $headers =~ m/^Subject: /m;
$headers}

{$Transaction->Content()}

Description: On CustID Change Archive
Condition: User Defined
Action: Notify Other Recipients
Template: Global template: Back Archive
Stage: TransactionCreate

Condition:

return 0 unless (($self->TransactionObj->Type eq “CustomField” && $self->TransactionObj->Field == 2) and
($self->TransactionObj->OldValue ne $self->TransactionObj->NewValue));
return 1 if $self->TransactionObj->NewValue;
return 0;

Global template: Back Archive

RT-Attach-Message: yes
X-Archive-Custids: {$Transaction->NewValue}

Please archive me in CustID: {$Transaction->NewValue}

{ my $resolved_message;
my $transactions = $Ticket->Transactions;
$transactions->Limit( FIELD => ‘Type’, VALUE => ‘Correspond’ );
$transactions->Limit( FIELD => ‘Type’, VALUE => ‘Comment’ );
$transactions->Limit( FIELD => ‘Type’, VALUE => ‘Create’ );
while (my $transaction = $transactions->Next) {
my $attachment = $transaction->ContentObj;
my $mime = $attachment->ContentAsMIME->dup;
$mime->head->mime_attr(“content-type” => “text/plain”);
RT::I18N::SetMIMEEntityToEncoding( $mime,
RT->Config->Get(‘EmailOutputEncoding’),
‘mime_words_ok’, );

$headers = $mime->header_as_string();
chomp($headers);
$headers .= "\nFrom: " . $attachment->CreatorObj->EmailAddress unless $headers =~ m/^From: /m;
$headers .= "\nTo: " . ($transaction->Type eq ‘Comment’ ? $Ticket->QueueObj->CommentAddress : $Ticket->QueueObj->CorrespondAddress) unless $headers =~ m/^To: /m;
$headers .= "\nDate: " . $transaction->Created unless $headers =~ m/^Date: /m;
$headers .= "\nSubject: " . $transaction->Subject || $Ticket->Subject unless $headers =~ m/^Subject: /m;
$resolved_message .= $headers;
$resolved_message .= “\n\n”;
$resolved_message .= $transaction->Content();
$resolved_message .= “\n\n\n”;
}
$resolved_message;
}

There’s a procmail recipe at the receiving end that splits the
forwarded messages, discarding the first part:

:0:

  • ^X-Archive-Custids: /[0-9]+
    | formail +1 -ds >>/path/to/archives/${MATCH}

Eleanor J. (Piglet) Evans, eje@panix.com
Customer Support, (212) 741-4400