For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2004 > Determine Email Headers









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Determine Email Headers
Mike Blezien

2004-05-24, 7:30 pm

Hello,

we're currently using the Mail::Audit module to process a perl script that pipes
email sent to "fake" forward emails address, then send it's to a real
address,... all works fine except if it's a HTML formatted email. My question
is, is it possible with this module to determine if it's HTML or plain test
email... or would it be better to simple strip out all the HTML elements.

TIA
--
Mike<mickalo>Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
http://www.thunder-rain.com
Quality Web Hosting
http://www.justlightening.net
MSN Messager: mickalo@thunder-rain.com
Cell:1.985.320.1191 - Office:1.712.737.2548
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Wiggins D Anconia

2004-05-24, 7:30 pm

> Hello,
>
> we're currently using the Mail::Audit module to process a perl script

that pipes
> email sent to "fake" forward emails address, then send it's to a real
> address,... all works fine except if it's a HTML formatted email. My

question
> is, is it possible with this module to determine if it's HTML or plain

test
> email... or would it be better to simple strip out all the HTML elements.
>


It appears that Mail::Audit provides a wrapper to a message, if that
message is of a MIME format (which most if not all HTML messages will
be) then you can check the type of the message, and process its various
parts using the MIME::Entity methods.

Only you can tell us whether or not your users want their HTML e-mail. I
know I would be more than a little pissed if my HTML e-mail ended up in
the bit bucket, not because I am any big fan of HTML e-mail (I am not)
but (much to my dismay ;-)) I don't control everyone elses mail clients
or sending habits...

What do you mean by "all works fine unless it has HTML"... the module,
your program, or something down the line?

http://danconia.org
Mike Blezien

2004-05-24, 8:32 pm

What I was getting at, is a email is sent as a standard plain text message, the
body content of the message is sent with no problems, but if the message is sent
as HTML formatted email, then all the HTML elements, tags...etc are included in
the body of the message.... so I was wondering, when using the Mail::Audit
module what is the best way to determine which email header "content-type" to
pass, IE "Content-type: text/html\n" or "Content-Type: text/plain\n" to be
included in the email message that is then sent to the real email address.

If you happen to have any example coding, it would be much appreciate if you can
share it :)

TIA

Mike

Wiggins d Anconia wrote:
>
> that pipes
>
>
> question
>
>
> test
>
>
>
> It appears that Mail::Audit provides a wrapper to a message, if that
> message is of a MIME format (which most if not all HTML messages will
> be) then you can check the type of the message, and process its various
> parts using the MIME::Entity methods.
>
> Only you can tell us whether or not your users want their HTML e-mail. I
> know I would be more than a little pissed if my HTML e-mail ended up in
> the bit bucket, not because I am any big fan of HTML e-mail (I am not)
> but (much to my dismay ;-)) I don't control everyone elses mail clients
> or sending habits...
>
> What do you mean by "all works fine unless it has HTML"... the module,
> your program, or something down the line?
>
> http://danconia.org
>
>


Mike Blezien

2004-05-24, 8:32 pm

This is what comes in the body of the email message if it's not plain text
message and need to pass the email with the right headers so it's displayed
correctly when received:
Message Body content:

This is a multi-part message in MIME format.

------=_NextPart_000_0013_01C4419E.02807DF0
Content-Type: text/plain;
charset="US-ASCII"
Content-Transfer-Encoding: 7bit

*****Sent from webmaster@mydomain.com to test@yourdomain.com*****

------=_NextPart_000_0013_01C4419E.02807DF0
Content-Type: text/html;
charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD> <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html;
charset=3Dus-ascii"> <TITLE>Message</TITLE>

<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR></HEAD> <BODY>
<DIV><SPAN class=3D255234621-24052004><FONT face=3DArial>Sent from <A=20
href=3D"mailto:webmaster@studiosyris.com">webmaster@mydomain.com</A> to =
<A=20
href=3D"mailto:test@yourdomain.com">test@yourdomain.com</A></FONT><=
/SPAN></DIV></BODY></HTML>

------=_NextPart_000_0013_01C4419E.02807DF0--

End of the message

TIA,
Mike


Wiggins d Anconia wrote:
>
> that pipes
>
>
> question
>
>
> test
>
>
>
> It appears that Mail::Audit provides a wrapper to a message, if that
> message is of a MIME format (which most if not all HTML messages will
> be) then you can check the type of the message, and process its various
> parts using the MIME::Entity methods.
>
> Only you can tell us whether or not your users want their HTML e-mail. I
> know I would be more than a little pissed if my HTML e-mail ended up in
> the bit bucket, not because I am any big fan of HTML e-mail (I am not)
> but (much to my dismay ;-)) I don't control everyone elses mail clients
> or sending habits...
>
> What do you mean by "all works fine unless it has HTML"... the module,
> your program, or something down the line?
>
> http://danconia.org
>
>



--
Mike<mickalo>Blezien
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Thunder Rain Internet Publishing
Providing Internet Solutions that work!
http://www.thunder-rain.com
Quality Web Hosting
http://www.justlightening.net
MSN Messager: mickalo@thunder-rain.com
Cell:1.985.320.1191 - Office:1.712.737.2548
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Wiggins D'Anconia

2004-05-24, 9:30 pm

Please bottom post...

Mike Blezien wrote:
> This is what comes in the body of the email message if it's not plain
> text message and need to pass the email with the right headers so it's
> displayed correctly when received:


Right, but the message itself is MIME and multipart. So I don't
understand from your original question why you would be dropping either
part? Shouldn't both parts continue on to their destination? I am still
not understanding why you would need to know what the content type is of
either part, since it is still just a single message. If the
destination is MIME compliant it will read one, if not then it will read
the preamble (and display it all), seems like you would still want them
both? If you are trying to have it decide which then you would need to
know apriori what the client can handle, and in the case of non-MIME you
would drop everything not text, but that depends on knowing the client.

Which still gets back to the original point that only you can tell us if
you want to drop the HTML or not.

I don't have specific sample code, can look into it but still trying to
understand specifically what you are looking for. Maybe stepping back
and looking at the problem as a whole, again, would help?

http://danconia.org

> Message Body content:
>
> This is a multi-part message in MIME format.
>
> ------=_NextPart_000_0013_01C4419E.02807DF0
> Content-Type: text/plain;
> charset="US-ASCII"
> Content-Transfer-Encoding: 7bit
>
> *****Sent from webmaster@mydomain.com to test@yourdomain.com*****
>
> ------=_NextPart_000_0013_01C4419E.02807DF0
> Content-Type: text/html;
> charset="US-ASCII"
> Content-Transfer-Encoding: quoted-printable
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD> <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html;
> charset=3Dus-ascii"> <TITLE>Message</TITLE>
>
> <META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR></HEAD> <BODY>
> <DIV><SPAN class=3D255234621-24052004><FONT face=3DArial>Sent from <A=20
> href=3D"mailto:webmaster@studiosyris.com">webmaster@mydomain.com</A> to =
> <A=20
> href=3D"mailto:test@yourdomain.com">test@yourdomain.com</A></FONT><=
> /SPAN></DIV></BODY></HTML>
>
> ------=_NextPart_000_0013_01C4419E.02807DF0--
>
> End of the message
>
> TIA,
> Mike
>
>
> Wiggins d Anconia wrote:
>
>
>

Mike Blezien

2004-05-24, 10:30 pm

Read below

Wiggins d'Anconia wrote:
> Please bottom post...
>
> Mike Blezien wrote:
>
>
>
> Right, but the message itself is MIME and multipart. So I don't
> understand from your original question why you would be dropping either
> part? Shouldn't both parts continue on to their destination? I am still
> not understanding why you would need to know what the content type is of
> either part, since it is still just a single message. If the
> destination is MIME compliant it will read one, if not then it will read
> the preamble (and display it all), seems like you would still want them
> both? If you are trying to have it decide which then you would need to
> know apriori what the client can handle, and in the case of non-MIME you
> would drop everything not text, but that depends on knowing the client.
>
> Which still gets back to the original point that only you can tell us if
> you want to drop the HTML or not.
>
> I don't have specific sample code, can look into it but still trying to
> understand specifically what you are looking for. Maybe stepping back
> and looking at the problem as a whole, again, would help?
>
> http://danconia.org



Basically what we have is a piped forwarding script that when a "fake" email
address, IE "username@mydomain.com" is recieved it's piped to script:

#!/usr/bin/perl
########################################
##########################
use Mail::Audit;
use DBI;
use strict;
alarm(30);

########################################
#########################
# some other sub routines and MySQL database coding to extract
# the real email address and name.
# .... coding... continues....
########################################
#########################

my $mail = Mail::Audit->new();
my $from = $mail->from();
my $fwdto = $mail->to();
my $subject = $mail->subject();
my $body = $mail->body();
my @mailbody = defined($body) ? @{$body} : "No Message Received";

open(MAIL,"|$mailprog") or die $!;
print MAIL "To: \"$name\" <$sendto>\n";
print MAIL "From: $from\n";
print MAIL "Reply-to: $from\n";
print MAIL "Subject: $subject\n\n";
print MAIL qq~@mailbody~;

close(MAIL) or die $!;

exit();
########################################
##############################

Now the $fwdto is matched agaisnt the data in the database, and we extract the
real address: $sendto and $name... The @mailbody array has the entire body of
the email message. If it's a plain text message, the body of the email is fine
and is displayed in the client's email reader without all those HTML characters,
like below (Message Body content).. but if it's HTML formatted email message,
then the body of the email contains all the garabage below. So what I am trying
to do is either attempt to somehow extract the actual text content of the
message or how to send it as the HTML formatted message properly so it's
displayed correctly when received. If I where able to extract just the actual
message content of the message content below, we would have something like this:

"Sent from webmaster@studiosyris.com - test@yourdomain.com"

I'm sure there is a simplier way to do and I hope I'm making my self clear here.
:)

TIA,
Mike
[color=darkred]

Wiggins D'Anconia

2004-05-24, 10:30 pm

Mike Blezien wrote:
> Read below


[snip]

>
>
> Basically what we have is a piped forwarding script that when a "fake"
> email address, IE "username@mydomain.com" is recieved it's piped to script:
>
> #!/usr/bin/perl
> ########################################
##########################
> use Mail::Audit;
> use DBI;
> use strict;
> alarm(30);
>
> ########################################
#########################
> # some other sub routines and MySQL database coding to extract
> # the real email address and name.
> # .... coding... continues....
> ########################################
#########################
>
> my $mail = Mail::Audit->new();
> my $from = $mail->from();
> my $fwdto = $mail->to();
> my $subject = $mail->subject();
> my $body = $mail->body();
> my @mailbody = defined($body) ? @{$body} : "No Message Received";
>
> open(MAIL,"|$mailprog") or die $!;
> print MAIL "To: \"$name\" <$sendto>\n";
> print MAIL "From: $from\n";
> print MAIL "Reply-to: $from\n";
> print MAIL "Subject: $subject\n\n";
> print MAIL qq~@mailbody~;
>
> close(MAIL) or die $!;
>
> exit();
> ########################################
##############################
>
> Now the $fwdto is matched agaisnt the data in the database, and we
> extract the real address: $sendto and $name... The @mailbody array has
> the entire body of the email message. If it's a plain text message, the
> body of the email is fine and is displayed in the client's email reader
> without all those HTML characters, like below (Message Body content)..
> but if it's HTML formatted email message, then the body of the email
> contains all the garabage below. So what I am trying to do is either
> attempt to somehow extract the actual text content of the message or how
> to send it as the HTML formatted message properly so it's displayed
> correctly when received. If I where able to extract just the actual
> message content of the message content below, we would have something
> like this:
>


Ok I believe the problem is that you are stripping out portions of the
header that you shouldn't be, specifically the portion that says that it
is a multi-part message. So when your second reader gets the message it
no longer states that the message is multipart. What through me is your
use of Mail::Audit. You don't seem to be using any of its true
functionality, only that you can read a message through it, which to my
knowledge makes its use overkill since you could use one of the mime
parsers directly.

Is there a particular reason why you not using the 'resend' method
provided with Mail::Audit? This will allow the complete message to be
intact and allow you to set the address to forward to. If you still
insist on piping to $mailprog there is the 'pipe' method, again will
allow the message to remain complete, but if $mailprog is just sendmail
or the sort you are better off using a module's method to do this rather
than piping directly to sendmail.


> "Sent from webmaster@studiosyris.com - test@yourdomain.com"
>
> I'm sure there is a simplier way to do and I hope I'm making my self
> clear here.
> :)


Definitely more clear. See if my comments help, if not fire off some
more questions :-). This is why I (and others) are so adamant about not
handling mail directly, amazing that such a complex beast can be made to
seem so simple *most* of the time...

http://danconia.org

>
>
>
>

Mike Blezien

2004-05-24, 10:30 pm

Wiggins d'Anconia wrote:
> Mike Blezien wrote:
>
>
>
> [snip]
>
>
> Ok I believe the problem is that you are stripping out portions of the
> header that you shouldn't be, specifically the portion that says that it
> is a multi-part message. So when your second reader gets the message it
> no longer states that the message is multipart. What through me is your
> use of Mail::Audit. You don't seem to be using any of its true
> functionality, only that you can read a message through it, which to my
> knowledge makes its use overkill since you could use one of the mime
> parsers directly.
>
> Is there a particular reason why you not using the 'resend' method
> provided with Mail::Audit? This will allow the complete message to be
> intact and allow you to set the address to forward to. If you still
> insist on piping to $mailprog there is the 'pipe' method, again will
> allow the message to remain complete, but if $mailprog is just sendmail
> or the sort you are better off using a module's method to do this rather
> than piping directly to sendmail.
>
>
>
>
> Definitely more clear. See if my comments help, if not fire off some
> more questions :-). This is why I (and others) are so adamant about not
> handling mail directly, amazing that such a complex beast can be made to
> seem so simple *most* of the time...
>
> http://danconia.org



well this my problem, with this particular project I can only pipe it to the
$mailprog "sendmail" in this case. I'm really not sure if there is a better way
to do this an still retain and send the message correctly no matter what
content-type the message is sent, plain text or HTML formatted message body...

How would I utilize the 'resend' method or 'pipe' method,... and would that make
a difference ??

TIA,
Mike

Mike Blezien

2004-05-24, 11:33 pm


Wiggins d'Anconia wrote:

> Definitely more clear. See if my comments help, if not fire off some
> more questions :-). This is why I (and others) are so adamant about not
> handling mail directly, amazing that such a complex beast can be made to
> seem so simple *most* of the time...
>
> http://danconia.org


Ok, problem solved! :) The 'resend' method did the trick!! thanks for pointing
that out, I guess I missed that when reading the doc on the module... I knew it
had to be really something simple. ;)

again, appreciate all your help.

Mike



>
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com