script perl email2sms

Practical Extraction and Report Language. Perl este un limbaj de programare interpretativ, conceput pentru procesarea de text.

script perl email2sms

Mesaj necititde soulraven » Joi 26 Aug 2010 12:47 pm

salut, ajutati-ma va rog sa portez un script perl de acces la pop3 intr-un script de acces la pop3 SSL.
scriptul l-am gasit pe lista de discutii kannel, numai ca este pentru pop3 fara SSL, iar eu vreau sa folosesc gmail si aici am nevoie de SSL.
am tot incercat, dar sunt varza la perl.
asta este scriptul original
#!/usr/local/bin/perl -w
use Net::POP3;
use strict;
use LWP::UserAgent;

# Connect to pop3 server
my $pop3 = Net::POP3->new("mailserver.domain.com") || die "Error : Couldn't log+on to server";

# Login to pop3 server
my $Num_Message = $pop3->login("pop3_user", "pop3_passwd");

my $Messages = $pop3->list();
my ($MsgDel,$MsgNo);
my ($MsgId, $MsgDate, $MsgFrom, $MsgTo, $MsgCc, $MsgSub);
my ($MsgAttach, $MsgSize, $MsgHeader, $MsgHeadFlg, $MsgBody);

foreach $MsgNo (keys %$Messages)
{
my $MsgContent = $pop3->get($MsgNo);
my $count = 0;
$MsgHeadFlg = 0;
$MsgBody = "";

$MsgSize = $pop3->list($MsgNo);

# Process message data
while()
{

# Exit if last line of mail
if ($count >= scalar(@$MsgContent))
{
last;
}

# Check if end of mail header
if (@$MsgContent[$count] =~ /^\n/)
{
$MsgHeadFlg = 1;
}

# Proceed if message header not processed
if (not $MsgHeadFlg)
{

# Split the line
my @LineContent = split /: /, @$MsgContent[$count];

# Check Header Info
SWITCH:
{
# Get message date
$LineContent[0] =~ /Date/i && do
{
$MsgDate = $LineContent[1];
last SWITCH;
};

# Get message id
$LineContent[0] =~ /Message-ID/i && do
{
$MsgId = $LineContent[1];
last SWITCH;
};

# Get message from
$LineContent[0] =~ /From/i && do
{
$MsgFrom = $LineContent[1];
last SWITCH;
};

# Get message to
$LineContent[0] =~ /To/i && do
{
$MsgTo = $LineContent[1];
last SWITCH;
};

# Get message cc
$LineContent[0] =~ /Cc/i && do
{
$MsgCc = $LineContent[1];
last SWITCH;
};


# Get message subject
$LineContent[0] =~ /Subject/i && do
{
$MsgSub = $LineContent[1];
last SWITCH;
};
}
}
else
{
# Process message body
$MsgBody .= @$MsgContent[$count];
}
$count++;
}
my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $result = `ps -e | grep -v grep|grep smsbox |awk \'{print \$1}\'`;

if ( $result ne "" )
{
my $url="http://kennel.domain.com:13013/cgi-bin/sendsms?username=kennel_user&password=kenel_passwd&to=$MsgSub&text=$MsgBody";
my $request = new HTTP::Request('GET', $url);
my $response = $ua->request($request);
my $content = $response->content();
$MsgDel = $pop3->delete($MsgNo)
}
}

# Disconnect from pop3 server
$pop3->quit();


pana acum am modificat asa scriptul

#!/bin/perl -w

use Mail::POP3Client;
use IO::Socket::SSL;
use CGI qw(:standard);
use LWP::UserAgent;
$cgi = new CGI;


$username = 'email@domeniu.ro';
$password = 'parola';

$mailhost = 'pop.gmail.com'; #pop.gmail.com
$port = '995'; #995

print $cgi->header();

$pop = Mail::POP3Client->new(USER => $username,
PASSWORD => $password,
HOST => $mailhost,
PORT => $port,
USESSL => 'true',
DEBUG => 0,) or die("ERROR: Unable to connect to mail server.\n");

#print $pop->Count() . " messages found!\n";

my $Messages = $pop3->list();
my ($MsgDel,$MsgNo);
my ($MsgId, $MsgDate, $MsgFrom, $MsgTo, $MsgCc, $MsgSub);
my ($MsgAttach, $MsgSize, $MsgHeader, $MsgHeadFlg, $MsgBody);

foreach $MsgNo (keys %$Messages)
{
my $MsgContent = $pop3->get($MsgNo);
my $count = 0;
$MsgHeadFlg = 0;
$MsgBody = "";

$MsgSize = $pop3->list($MsgNo);

# Process message data
while()
{

# Exit if last line of mail
if ($count >= scalar(@$MsgContent))
{
last;
}

# Check if end of mail header
if (@$MsgContent[$count] =~ /^\n/)
{
$MsgHeadFlg = 1;
}

# Proceed if message header not processed
if (not $MsgHeadFlg)
{

# Split the line
my @LineContent = split /: /, @$MsgContent[$count];

# Check Header Info
SWITCH:
{
# Get message date
$LineContent[0] =~ /Date/i && do
{
$MsgDate = $LineContent[1];
last SWITCH;
};

# Get message id
$LineContent[0] =~ /Message-ID/i && do
{
$MsgId = $LineContent[1];
last SWITCH;
};

# Get message from
$LineContent[0] =~ /From/i && do
{
$MsgFrom = $LineContent[1];
last SWITCH;
};

# Get message to
$LineContent[0] =~ /To/i && do
{
$MsgTo = $LineContent[1];
last SWITCH;
};

# Get message cc
$LineContent[0] =~ /Cc/i && do
{
$MsgCc = $LineContent[1];
last SWITCH;
};


# Get message subject
$LineContent[0] =~ /Subject/i && do
{
$MsgSub = $LineContent[1];
last SWITCH;
};
}
}
else
{
# Process message body
$MsgBody .= @$MsgContent[$count];
}
$count++;
}
my $ua = new LWP::UserAgent;
$ua->timeout(120);
my $result = `ps -e | grep -v grep|grep smsbox |awk \'{print \$1}\'`;

if ( $result ne "" )
{
my $url="http://kennel.domain.com:13013/cgi-bin/sendsms?username=kennel_user&password=kenel_passwd&to=$MsgSub&text=$MsgBody";
my $request = new HTTP::Request('GET', $url);
my $response = $ua->request($request);
my $content = $response->content();
$MsgDel = $pop3->delete($MsgNo)
}
}

# Disconnect from pop3 server
$pop->Close();

exit;


dar primesc eroarea asta

root@acron:/usr/local/email2sms# perl email2sms.pl
Name "main::pop3" used only once: possible typo at email2sms.pl line 128.
Content-Type: text/html; charset=ISO-8859-1

Can't locate object method "list" via package "Mail::POP3Client" at email2sms.pl line 27.


ma puteti ajuta sa portez scriptul?
soulraven
 
Mesaje: 48
Membru din: Joi 22 Sep 2005 3:00 am
Localitate: Campulung Muscel

Re: script perl email2sms

Mesaj necititde soulraven » Joi 26 Aug 2010 9:41 pm

ok, am facut putin progrese cu scriptul, acum reusesc sa citesc e-mailurile din cont, numai ca nu reusesc sa citesc toate mesajele si sa elimin ce nu am nevoie si sa ramana doar subiectul si mesajul.

noul cod arata cam asa

#!/bin/perl

use Mail::POP3Client;
use IO::Socket::SSL;
use CGI qw(:standard);
$cgi = new CGI;
use LWP::UserAgent;


$username = 'email@domeniu.ro';
$password = 'parola';

$mailhost = 'pop.gmail.com'; #examplepop.gmail.com
$port = '995'; #995

print $cgi->header();

$pop = Mail::POP3Client->new(USER => $username,
PASSWORD => $password,
HOST => $mailhost,
PORT => $port,
USESSL => 'true',
DEBUG => 0,) or die("ERROR: Unable to connect to mail server.\n");


if (($pop->Count()) < 1) {
print "No messages...\n";
exit;
}

my $Messages = $pop->Count();
my ($MsgDel,$MsgNo);
my ($MsgId, $MsgDate, $MsgFrom, $MsgTo, $MsgSub, $Delivred_To, $Content);
my ($MsgAttach, $MsgSize, $MsgHeader, $MsgHeadFlg, $MsgBody, $MIME_Version, $Received_Time, $Origin_IP);

foreach $MsgNo ( $Messages ) {
my $MsgContent = $pop->HeadAndBody($MsgNo);
my $count = 0;
$MsgHeadFlg = 0;
$MsgBody = "";

(my $Mesaj_NR, my $MsgSize) = split('\s+',$pop->List($MsgNo));

#print $MsgSize . "Marime mesaj \n";
#print $Mesaj_NR . "Nr. Mesaj \n";
#print $MsgContent . "\n";

# Process message data
while()
{

# Exit if last line of mail
if ($count >= scalar(@$MsgContent))
{
#print $MsgContent . "count";
last;
}

# Check if end of mail header
if (@$MsgContent[$count] =~ /^\n/)
{
$MsgHeadFlg = 1;
}

# Proceed if message header not processed
if (not $MsgHeadFlg)
{

# Split the line
my @LineContent = split /: /, @$MsgContent[$count];

# Check Header Info
SWITCH:
{

#Get MIME Version
$LineContent[0] =~ /MIME-Version/i && do
{
$MIME_Version = $LineContent[1];
last SWITCH;
};
#Get Time Of recived
$LineContent[0] =~ /Received/i && do
{
$Received_Time = $LineContent[1];
last SWITCH;
};
#Get IP of sender
$LineContent[0] =~ /X-Originating-IP/i && do
{
$Origin_IP = $LineContent[1];
last SWITCH;
};
#Get Date
$LineContent[0] =~ /Date/i && do
{
$MsgDate = $LineContent[1];
last SWITCH;
};
#Get Delivred To
$LineContent[0] =~ /Delivered-To/i && do
{
$Delivred_To = $LineContent[1];
last SWITCH;
};
# Get message id
$LineContent[0] =~ /Message-ID/i && do
{
$MsgId = $LineContent[1];
last SWITCH;
};
# Get message subject
$LineContent[0] =~ /Subject/i && do
{
$MsgSub = $LineContent[1];
last SWITCH;
};

# Get message from
$LineContent[0] =~ /From/i && do
{
$MsgFrom = $LineContent[1];
last SWITCH;
};

# Get message to
$LineContent[0] =~ /To/i && do
{
$MsgTo = $LineContent[1];
last SWITCH;
};
# Get Content type
$LineContent[0] =~ /Content-Type/i && do
{
$Content = $LineContent[1];
last SWITCH;
};
}
}
else
{
# Process message body
$MsgBody .= @$MsgContent[$count];
}
$count++;
}
my $ua = new LWP::UserAgent;
$ua->timeout(120);
#my $result = `ps -e | grep -v grep|grep smsbox |awk \'{print \$1}\'`;
my $result = true;

if ( $result ne "" )
{
print $MsgSub . "subiect mesaj \n";
print $MsgBody . "continut mesaj \n";
my $url="http://kennel.domain.com:13013/cgi-bin/sendsms?username=kennel_user&password=kenel_passwd&to=$MsgSub&text=$MsgBody";
my $request = new HTTP::Request('GET', $url);
my $response = $ua->request($request);
my $content = $response->content();
$MsgDel = $pop->Delete($MsgNo)
}
}

# Disconnect from pop3 server
$pop->close();

exit;


cum procedez ca sa citesti toate mesajele noi si sa le execut mai jos pentru kannel ?
soulraven
 
Mesaje: 48
Membru din: Joi 22 Sep 2005 3:00 am
Localitate: Campulung Muscel

Re: script perl email2sms

Mesaj necititde Agkelos » Dum 13 Feb 2011 4:11 pm

Ar fi mai simplu sa incerci cu chilkat. In bucla while() verifici ck_from() sau ce verifici de obicei ca sa-ti dai seama ca mesajul trebuie trimis prin SMS, si cu un if() trimiti prin SMS doar ce te intereseaza.

Nu e gratuit, dar nu e nici scump si daca vrei sa faci ceva profi presupun ca merita :)
Avatar utilizator
Agkelos
 
Mesaje: 59
Membru din: Vin 30 Ian 2009 3:00 am
Localitate: Cluj


Înapoi la Perl

Cine este conectat

Utilizatorii ce navighează pe acest forum: Niciun utilizator înregistrat şi 2 vizitatori

cron