Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

mail2sms for Kannel

by Pizu (Initiate)
on Nov 10, 2017 at 09:39 UTC ( #1203105=perlquestion: print w/replies, xml ) Need Help??
Pizu has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have created a mail server using postfix and created the catchall address. - so far so good.

Am looking for something that reads the below..

1) From Address(s) - Needed to verify the sender to continue in the script next steps..

2) Original To Address(s) - Example: 11111111@domain.com;22222222@domain.com

Checks to be done on point 2, need to confirm that the To address contains 8 numbers @domain.com

If the above is matched..

I need remove the @domain.com and the number into a temp $mobilvariable and the subject into another $textvariable.

Then I can GET an HTTP request

something like:

my $url="http://x.x.x.x:14013/cgi-bin/sendsms?username=xxx&password=xxx&to=$mobilvariable&from=mail2sms&text=$textvariable";

my $request = new HTTP::Request('GET', $url);

Replies are listed 'Best First'.
Re: mail2sms for Kannel
by hippo (Abbot) on Nov 10, 2017 at 09:51 UTC
    From Address(s) - Needed to verify the sender to continue in the script next steps..

    You can use index to locate a fixed substring (such as "From: foo@bar.baz\n") in a longer one.

    need to confirm that the To address contains 8 numbers @domain.com

    A regular expression could do that. See the tutorial for getting started.

    I need remove the @domain.com and the number into a temp $mobilvariable and the subject into another $textvariable.

    More regular expressions this time with capture groups.

    Sounds like you have an algorithm planned and just now need to turn it into working code. Good luck.

    (BTW, you posted this in the wrong section (Perl Monks Discussion). I've moved it to Seekers of Perl Wisdom where it belongs.)

      Thanks for your quick response. Will check the links you provided.

Re: mail2sms for Kannel
by Discipulus (Monsignor) on Nov 10, 2017 at 09:54 UTC
    Hi Pizu and welcome to the monastery and to the wonderful world of Perl!

    Start every Perl progrm with: use strict; use warnings; on the top of it!

    to get address you can use split using ; as separator. Remember to chomp eventual newlines at the end of that input.

    To check valid mail address is probably better using a dedicated module of CPAN like Email::Valid Then if you need to check the presence of 8 numbers a regex seems a valid option: see perlrequick

    Again to remove part of a string use regexes as substitution as in s/found/substitution/ form.

    To issue an HTTP request you can use LWP::Simple or LWP::UserAgent or other CPAN modules

    Show us what you achive using the above suggestion applied to get a more detailed, useful, merited help



    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: mail2sms for Kannel
by haukex (Abbot) on Nov 10, 2017 at 09:54 UTC

      Hello all :)

      I managed to do some good progress.. :)

      1 #!/usr/bin/perl -w 2 use strict; 3 use warnings; 4 use Mail::Header; 5 use LWP::UserAgent; 6 use HTTP::Request; 7 use Email::Simple; 8 9 my @liste = (<>); 10 my $header_object = new Mail::Header \@liste, Modify => 0, MailFro +m => "COERCE" ; 11 12 my $to = $header_object->get('To'); 13 my $username = substr($header_object->get('To'), 0, index +($header_object->get('To'), '@')); 14 my $apparently_from = $header_object->get('Return-Path'); 15 my $from = $header_object->get('From'); 16 my $mail_from = $header_object->get('Mail-From'); 17 my $subject = $header_object->get('Subject'); 18 19 20 21 my $email_details = new Email::Simple; 22 23 my $body = $email_details->get('Body'); 24 25 /* Printing to file - testing */ 26 27 my $filename = '/var/scripts/log/mail2sms.txt'; 28 open(my $fh, '>', $filename) or die "Could not open file '$filenam +e' $!"; 29 30 print $fh ' To: ' . $to; 31 print $fh ' From: ' . $from; 32 print $fh ' maybe From: ' . $apparently_from; 33 print $fh ' Subject: ' . $subject; 34 print $fh ' Username: ' . $username; 35 print $fh ' mbox From: ' . (split(/\s+/,$mail_from))[0]; 36 print $fh ' Body: ' . $body; 37 print $fh $/; 38 39 40 41 my $ua = new LWP::UserAgent; 42 $ua->timeout(120); 43 #my $result = `ps -e | grep -v grep|grep smsbox |awk \'{print \$1} +\'`; 44 #my $result = true; 45 46 #if ( $result ne "" ) 47 { 48 my $url="http://x.x.x.x:14013/cgi-bin/sendsms?username=kanneluser& +password=kannelpassword&to=$username&from=mail2sms&text=$subject $bod +y"; 49 my $request = new HTTP::Request('GET', $url); 50 my $response = $ua->request($request); 51 my $content = $response->content(); 52 }

      My Issue is this now, Lines: 21,23 is not working, can someone help me?

      also for the body, is it possible to make it as a single string?

        My Issue is this now, Lines: 21,23 is not working, can someone help me?

        "Not working" - maybe you should have a closer look. Do those lines hold up signs with "ON STRIKE!" written on them?

        You really make it hard to help you. Why did you inject all of those numbers into your source code? Perl is not 1970s home computer BASIC and complains loudly about them:

        X:\>perl 1203732.pl Number found where operator expected at 1203732.pl line 2, near "2" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 9, near "9" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 12, near "12" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 19, near "19" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 20, near "20" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 21, near "21" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 23, near "23" (Missing semicolon on previous line?) Number found where operator expected at 1203732.pl line 25, near "25" (Missing semicolon on previous line?) Bareword found where operator expected at 1203732.pl line 25, near "* +Printing to" (Missing operator before to?) Having no space between pattern and following word is deprecated at 12 +03732.pl line 27. Warning: Use of "log" without parentheses is ambiguous at 1203732.pl l +ine 27. syntax error at 1203732.pl line 2, near "2" "use" not allowed in expression at 1203732.pl line 2, near "2 " "use" not allowed in expression at 1203732.pl line 3, near "3 " "use" not allowed in expression at 1203732.pl line 4, near "4 " "use" not allowed in expression at 1203732.pl line 5, near "5 " "use" not allowed in expression at 1203732.pl line 6, near "6 " "use" not allowed in expression at 1203732.pl line 7, near "7 " Substitution pattern not terminated at 1203732.pl line 35. X:\>

        And for the "not working" lines: What do you expect to happen, what happens instead?

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1203105]
Approved by beech
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-12-14 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (414 votes). Check out past polls.

    Notices?