Search This Blog

Thursday, January 4, 2007

SMTP to SMS gateway

# (c) Daniyar, 2007
# mySMTP2SMS gateway prototype
# it uses GSMLIB to send SMS via GSM modem
# install it as the service
# instsrv mySMTP2SMS "C:\Program Files\gsmlib\srvany.exe"
# then add registry keys as explained in the srvany.wri
# usage:
# blat -serverSMTP 192.168.1.48 -port 8025 -f me@gmail.com -to
0796574016 -body "this is a test sms"


use Win32::EventLog::Carp;
use Net::SMTP::Server;
use Net::SMTP::Server::Client;
use Net::SMTP::Server::Relay;

print "mysmtp2sms:start...";

# create a server on IP servers external address
$server = new Net::SMTP::Server('192.168.1.48', 8025)
croak("mysmtp2sms: Unable to create server: $!\n"); # write to event
log

while($conn = $server->accept()) {

my $client = new Net::SMTP::Server::Client($conn)
croak("mysmtp2sms: Unable to handle client connection: $!\n");

# Process the client. This command will block until
# the connecting client completes the SMTP transaction.
$client->process next;

@to=$client->{TO};

$strTo=$to[0][0]; # get only 1 recepient, we do not do a lot of
check here, but you can certainly do some, suc as strip domain name,
etc.
$strTo=~ s/[<>]//g; # remove <> symbols globally
#print "$strTo\n";
#print $client->{MSG};
#print "\n";
@msg = split(/\n/, $client->{MSG}); # split message to lines in
order to cut the header
$l=scalar(@msg); # length
#print "l=$l\n";
$i=0;
$c= ord($msg[$i]);
while (($c!=13) && ($i<$l)) # look for the first empty line that
must be begining of the message body
{$c= ord($msg[++$i]);}
#print "i=$i\n";

$strMsg= join("",@msg[$i+1..$l]); # make a string from message body
without header
$strMsg=~ s/\n//g; # strip new line symbols

#print "$strMsg\n";

# call gsmlib

@arg= ("c:\\program
files\\gsmlib\\gsmsendsms.exe",'-d','COM3:','-b','115200',$strTo,
$strMsg);

system(@arg) == 0
or carp "mysmtp: system @args failed: $?"

}

print "mysmtp2sms:quit...";

1 comment:

Anonymous said...

Great work.