inserting a line into the body of an eMail before forwarding

L

Lisa

Does anyone know how to insert a line into the body of an eMail b4 your
forward it?

I wan to take the original recipient eMail address and add it as a
single line at the top of the body of the message, then send the
message to a different address.

this code all works, I just don't know how to insert the line into the
body.

use Mail::pOP3Client;
use Mail::MboxParser::Mail;
my $pop = new Mail::pOP3Client( USER => "foo.bar\@foobar.com",
PASSWORD => "FoOBaR",
HOST => "foobar.com",
DEBUG => 0 );
for my $i (1 .. $pop->Count) {
my %EmailHeader = get_header_hash($i);



#$EmailHeader{'Envelope-to'} <-now containes original addressee, and
this is what I want to insert into the TOP of the body prior to sending
it (below)



my $Msg =
Mail::MboxParser::Mail->new([$pop->Head($i)],[$pop->Body($i)]);
my $mail = $Msg->make_convertable;
$mail->replace_in_header('to', '(e-mail address removed)');
$mail->send('sendmail');
}

Thanks
 
G

Gunnar Hjalmarsson

Lisa said:
Does anyone know how to insert a line into the body of an eMail b4 your
forward it?

#$EmailHeader{'Envelope-to'} <-now containes original addressee, and
this is what I want to insert into the TOP of the body prior to sending
it (below)

my $Msg =
Mail::MboxParser::Mail->new([$pop->Head($i)],[$pop->Body($i)]);

Maybe you can simply do:

my $Msg = Mail::MboxParser::Mail->new( [ $pop->Head($i) ],
[ "Original addressee: $EmailHeader{'Envelope-to'}\n\n",
$pop->Body($i) ] );
 
L

Lisa

Nope.

Returns;

Can't modify non-lvalue subroutine call at pop_the_box.cgi line 163,
<GEN0> line 136.

Lisa
 
L

Lisa

I wish it was that simple.

$pop-Body($i), returns the whole body, including the multi part boundry
strings as well as the content type, char set, and content encoding
strings. so that pre-pending the $EmailHeader{'Envelope-to') simply
puts that data infront of (outside ) body entity and therefore NOT
printed by the emial client when the message is read.

:Lisa
 
L

Lisa

Unfortunately the only solution I can come up with invovles regex
(which I am not very good at) substitution, where you would take note
of the boundry string held in the

$EmailHeader{'Content-Type'}

say it was; multipart/alternative; boundary,
"----=_NextPart_000_00CA_01C4E683.25AB63F0"

<snippet>
And since the body would look something like this;

This is a multi-part message in MIME format.

------=_NextPart_000_00CA_01C4E683.25AB63F0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

test

------=_NextPart_000_00CA_01C4E683.25AB63F0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html;
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>test</FONT></DIV></BODY></HTML>

------=_NextPart_000_00CA_01C4E683.25AB63F0--
</snippet>

I figure you would regex the body and find the;
boundry string which is directly followed by the "Content-Type:
text/plain;"
then you would need to locate the \n\n and replace it with;
"\n\n$EmailHeader{'Envelope-to'}\n\n"

Problem is, Like I said regex is not my cup of tea even though I can
see it's use here.

Lisa
 
G

Gunnar Hjalmarsson

Lisa said:
Unfortunately the only solution I can come up with invovles regex

Assuming that you have the whole message body as a string in the
variable $body, how about this to start with:

if ( $msg->header->{content-type} =~ /boundary=/i ) {
my $part = '(?:;\\s*charset=\\S+)?\\s+Cont\S+Encoding: *\\S+\\n';
my $text = qr(Content-Type:\s*text/plain$part)i;
my $html = qr(Content-Type:\s*text/html$part)i;
my $addr = "Original addressee: $EmailHeader{'Envelope-to'}";
$body =~ s/($text)/$1\n$addr\n/;
$body =~ s/($html)/$1\n<p>$addr<\/p>\n/;
} else {
$body = "$addr\n\n$body";
}
 
L

Lisa

Gunnar said:
Assuming that you have the whole message body as a string in the
variable $body, how about this to start with:

if ( $msg->header->{content-type} =~ /boundary=/i ) {
my $part = '(?:;\\s*charset=\\S+)?\\s+Cont\S+Encoding: *\\S+\\n';
my $text = qr(Content-Type:\s*text/plain$part)i;
my $html = qr(Content-Type:\s*text/html$part)i;
my $addr = "Original addressee: $EmailHeader{'Envelope-to'}";
$body =~ s/($text)/$1\n$addr\n/;
$body =~ s/($html)/$1\n<p>$addr<\/p>\n/;
} else {
$body = "$addr\n\n$body";
}

Well this appears to be close to what I am trying to do (brain hurting)
with a few tweeks, however it is still not working and as I admitedly
do not quite understand what a few of the lines are supposed to be
doing my tweeking is not hitting the mark.

Here is what I have now;

my $Msg =
Mail::MboxParser::Mail->new([$pop->Head($i)],[$pop->Body($i)]);
my $HeaderFields = $Msg->header;
print "Header content-type = ",
$HeaderFields->{'content-type'},br(),br();
if ( $HeaderFields->{'content-type'} =~ /boundary=/i ) {
print br(),br(),"--=={ Found Boundry ]==--",br(),br();
########## Not exactly sure what these are supposed to be doing,
########## but I think this might be where it is not working
my $part = '(?:;\\s*charset=\\S+)?\\s+Cont\S+Encoding: *\\S+\\n';
print "part = ",$part,br(),br();
my $text = qr(Content-Type:\s*text/plain$part)i;
print "text = ",$text,br(),br();
my $html = qr(Content-Type:\s*text/html$part)i;
print "html = ",$html,br(),br();
#############################################################
my $addr = "Original addressee: $EmailHeader{'Envelope-to'}";
print "addr = ",$addr,br(),br();
$Body =~ s/($text)/$1\n$addr\n/;
$Body =~ s/($html)/$1\n<p>$addr<\/p>\n/;
} else {
$Body = "$addr\n\n$Body";
}
print "tinkered Body = ",$Body,br(),br();


This produces the following output;

Header content-type = multipart/alternative;
boundary="----=_NextPart_000_010A_01C4E6AE.267ADA60"



--=={ Found Boundry ]==--

part = (?:;\s*charset=\S+)?\s+Cont\S+Encoding: *\S+\n

text =
(?i-xsm:Content-Type:\s*text/plain(?:;\s*charset=\S+)?\s+Cont\S+Encoding:
*\S+\n)

html =
(?i-xsm:Content-Type:\s*text/html(?:;\s*charset=\S+)?\s+Cont\S+Encoding:
*\S+\n)

addr = Original addressee: (e-mail address removed)

tinkered Body = This is a multi-part message in MIME format.
------=_NextPart_000_010A_01C4E6AE.267ADA60 Content-Type: text/plain;
charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Another Test ------=_NextPart_000_010A_01C4E6AE.267ADA60 Content-Type:
text/html; charset="iso-8859-1" Content-Transfer-Encoding:
quoted-printable
Another Test
------=_NextPart_000_010A_01C4E6AE.267ADA60--
 
G

Gunnar Hjalmarsson

Lisa said:
Well this appears to be close to what I am trying to do (brain hurting)
with a few tweeks, however it is still not working and as I admitedly
do not quite understand what a few of the lines are supposed to be
doing my tweeking is not hitting the mark.

Well, let's make it "work" first.

Two things:
my $Msg =
Mail::MboxParser::Mail->new([$pop->Head($i)],[$pop->Body($i)]);

You reasonably need to pass the $Body variable instead of
$pop->Body($i), and move the statement to after $Body has been modified.
my $part = '(?:;\\s*charset=\\S+)?\\s+Cont\S+Encoding: *\\S+\\n';

I've tweaked that row:

my $part = '(?:;\s*charset=\S+)?\s+Cont\S+Encoding:\s\S+\n';

Now, when I run the code below, it does insert "Original addressee: ..."
as expected.

use CGI ':standard';
my $Body = do { local $/; <DATA> };

my $part = '(?:;\s*charset=\S+)?\s+Cont\S+Encoding:\s\S+\n';
my $text = qr(Content-Type:\s*text/plain$part)i;
my $html = qr(Content-Type:\s*text/html$part)i;
my $addr = "Original addressee: foo.bar\@foobar.com";
$Body =~ s/($text)/$1\n$addr\n/;
$Body =~ s/($html)/$1\n<p>$addr<\/p>\n/;
print "tinkered Body = ",$Body,br(),br();

__DATA__
This is a multi-part message in MIME format.
------=_NextPart_000_010A_01C4E6AE.267ADA60 Content-Type: text/plain;
charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Another Test ------=_NextPart_000_010A_01C4E6AE.267ADA60 Content-Type:
text/html; charset="iso-8859-1" Content-Transfer-Encoding:
quoted-printable
Another Test
------=_NextPart_000_010A_01C4E6AE.267ADA60--
 
L

Lisa

Gunnar said:
I've tweaked that row:

my $part = '(?:;\s*charset=\S+)?\s+Cont\S+Encoding:\s\S+\n';

Now, when I run the code below, it does insert "Original addressee: ...."
as expected.
That was the fix right there!! thank you very much.

Lisa
 
G

Gunnar Hjalmarsson

Lisa said:
That was the fix right there!!

Hmm.. I missed a '*' character that should better be there:

my $part = '(?:;\s*charset=\S+)?\s+Cont\S+Encoding:\s*\S+\n';
---------------------------------------------------------^

I'm sure it can be further improved, but that's something to start with.
thank you very much.

You're welcome. If you still wonder about details in the code, feel free
to ask. But first, please read about the s/// and qr// operators in
"perldoc perlop" and review "perldoc perlre".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top