Need Perl script to parse email body to sms

I have setup an sms gateway. In my setup procmail will send sms's when there are specific words in the subject line. For the sms sending I use Gnokii.
A standard email  is too big to be send as a sms, with headers the mail is bigger then 640 char.
So I need a script which only parses the body to Gnokii and when needed truncates it to the max size accepted by Gnokii.

Can anyone help me with a setup of Perl script which reads mail from the standard input(procmail)
and sends the body to the standard output  
LVL 1
howartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:

use Email::Simple;
 
#Read entire email from standard input
local $/;
my $email_text = <STDIN>;
 
#Use Email::Simple module to parse email
my $email = Email::Simple->new($email_text);
 
#Get first 640 characters of body
my $body = $email->body();
$body =~ s/(.{0,640})/$1/;
 
#Print body to standard output
print $body;

Open in new window

0
howartAuthor Commented:
Ok thx.
Need to install Email simple module
will let you know if it works. :)
0
Adam314Commented:
Missed one thing in previous post... change line 12 to this:
$body =~ s/^(.{0,640}).*$/$1/s;

Open in new window

0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

howartAuthor Commented:
Does not work correclty

this is may email:


From administrator@poppers.nl  Wed Mar  5 16:42:02 2008
Return-Path: <administrator@poppers.nl>
X-Original-To: null@sms.poppers.nl
Delivered-To: null@sms.poppers.nl
Received: from mail2.poppers.nl (proxy-poppers.poppers.local [192.168.1.12])
        by sms.poppers.nl (Postfix) with ESMTP id 1628223CB8
        for <null@sms.poppers.nl>; Wed,  5 Mar 2008 16:42:02 +0100 (CET)
Received: from 192.168.1.1 ([192.168.1.1]) by mail2.poppers.nl with Microsoft SMTPSVC(5.0.2195.6713); Wed, 5 Mar 2008 16:42:01 +0100
X-Mailer: AKCP embedded mailer
To: <person@poppers.nl>
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1914
From: <ado@poppers.nl>
Date: 5 MAR 2008 16:42:3 +0100
Subject: Test email from sensorProbe
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="===_123==_"
Message-ID: <PROXY-POPPERSgx8VUk0000001e@mail2.poppers.nl>
X-OriginalArrivalTime: 05 Mar 2008 15:42:01.0406 (UTC) FILETIME=[7448DDE0:01C87ED7]
 
This is a multi-part message in MIME format.
 
--===_123==_
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
 
This is a test email.
 
 
sensorProbe2 v 2.0 at http://192.168.1.1
System Name: SensorProbe Lelystad 1
 
 
5/3/2008
 
Poppers Senco Nederland B.V. K.v.K. Flevoland 39055186
 
DISCLAIMER: Dit e-mailbericht inclusief alle bijlagen zijn vertrouwelijk =
en uitsluitend bestemd voor de geadresseerde(n). Is deze e-mail per =
vergissing bij u terechtgekomen, dan verzoeken wij u dit aan de afzender =
te laten weten door deze e-mail te beantwoorden en daarna te =
verwijderen, inclusief de bijlagen. Openbaarmaking, verspreiding of =
verstrekking aan derden zonder voorafgaande toestemming van Poppers =
Senco Nederland B.V. is niet toegestaan. De inhoud van deze e-mail is =
alleen ter informatie. Poppers Senco Nederland B.V. aanvaardt geen =
enkele aansprakelijkheid voor virussen.
DISCLAIMER: This e-mail message including any attachments are private =
and confidential and are only intended for the addressee(s). If you =
received this e-mail by mistake, please notify the sender by replying =
and immediately delete the e-mail. Disclosing or distribution is not =
permitted without the prior authorisation by Poppers Senco Nederland =
B.V. This email is for information purposes only. Poppers Senco =
Nederland B.V. does not accept any liability for viruses.
 
 
 
--===_123==_--

Open in new window

0
Adam314Commented:
That message doesn't have any text in the body, the text is in an attachment.  
0
howartAuthor Commented:
Most email readers see that as body-text.
Ok, do you know a way to only display the text attachment?
0
howartAuthor Commented:
ok use now Email::MIME

this is the code

when I run it I get this error:

Don't know how to decode quoted-printable  this is a test email.   at /usr/lib/perl5/site_perl/5.8.1/Email/MIME.pm line 84



#!/usr/bin/perl
 
# use Email::Simple;
use Email::MIME;
 
#Read entire email from standard input
local $/;
my $email_text = <STDIN>;
#
# Use Email::Simple module to parse email
#my $email = Email::Simple->new($email_text);
my $email = Email::MIME->new($email_text);
 
 
#
#  #Get first 120 characters of body
my $body = $email->body();
   $body =~ s/^(.{0,120}).*$/$1/s;
 
#   #Print body to standard output
print $body;

Open in new window

0
Adam314Commented:
The problem seems to be that there are spaces on lines that should be completly blank.  A blank line is what is used to seperate a header from body text, but there is a space on the line.  This is confusing the Email::MIME.  If the message shows up fine in an e-mail client, then the client must ignore those spaces.

Then, the body of the message is "This is a multi-part message in MIME format.".  What you probably want is the text of the first attachment.  Here is some updated code:

use Email::MIME;
 
# Read entire email from standard input, fix lines that should be blank
local $/;
my $email_text = <STDIN>;
$email_text =~ s/^\s+$//mg;
 
# Parse message
my $email = Email::MIME->new($email_text);
 
# Get first 120 characters of body
my $body = $email->body();
$body =~ s/^(.{0,120}).*$/$1/s;
print "********** Body:\n$body";
 
# Get the text from the first attachment
my $part1_body = ($email->subparts())[0]->body;
#NOTE: If you want only the first 120 characters,
#      Use the same regex on $part1_body that is
#      used on $body
print "********** Part 1:\n$part1_body";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
howartAuthor Commented:
Ok the above code works
with the regex statement you get:

#!/usr/bin/perl
 
use Email::MIME;
 
# Read entire email from standard input, fix lines that should be blank
local $/;
my $email_text = <STDIN>;
$email_text =~ s/^\s+$//mg;
#
#  # Parse message
my $email = Email::MIME->new($email_text);
#
#   # Get first 120 characters of body
my $body = $email->body();
$body =~ s/^(.{0,120}).*$/$1/s;
print "********** Body:\n$body";
#
# Get the text from the first attachment
my $part1_body = ($email->subparts())[0]->body;
#NOTE: If you want only the first 120 characters,
#      Use the same regex on $part1_body that is
#      used on $body
 
$part1_body =~ s/^(.{0,120}).*$/$1/s;
print "********** Part 1:\n$part1_body";
#

Open in new window

0
howartAuthor Commented:
Need some fine tuning. Forgott that total of char in sms message may not exceed 120 char.
Now body can contain 120 and txt attachment also 120 char.

But this one works fine for my alarm messages. omitted the body part.
0
howartAuthor Commented:
great assist, many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.