- 論壇徽章:
- 0
|
本帖最后由 riverlee2008 于 2010-05-19 20:16 編輯
這個應該很實際,幫助你實時查看是否有新郵件.
前提:
1) 擁有一個gmail賬號
2) 擁有一個飛信賬號
注:感謝gmail的rss feed(https://mail.google.com/mail/feed/atom)和飛信API(http://sms.api.bz/)
個人要求(@_@): 好東西要分享,望版主給封個精華貼,再加點小分,在下萬分感謝~~~
用法: perl alertGmail.pl -u gmail@gmail.com -p gmaillpasswd -f 13912345678 -fp fetionpasswd -t 13912345678 -d 300
程序如下:
alertGmail.tar.gz
(1.71 KB, 下載次數(shù): 50)
2010-05-19 20:12 上傳
點擊文件名下載附件
alertGmail by fetion
- #!/usr/bin/perl
- use strict;
- use warnings;
- use LWP::UserAgent;
- use Getopt::Long;
- my($user,$passwd,$from,$fetionpasswd,$to,$about,$help,$sleeptime);
- GetOptions ("u=s" => \$user, # gmail account
- "p=s" => \$passwd, # gmail account password
- "f=s" => \$from, # fetion account telphone numver
- "fp=s"=> \$fetionpasswd,# fetion account apssword
- "t=s" => \$to, # telphone number to send the gamil notification,can be fetion account itself
- "about"=> \$about, # print out about page
- "d=i" => \$sleeptime, # how often to check the mail(default 300s)
- "h|help"=>\$help
- ); # flag
- if($help){&usage();}
- if($about){&about();}
- if(!defined($user) || !defined($passwd) || !defined($from)|| !defined($fetionpasswd)||!defined($to)){
- help();
- }
- my $gmail_address='https://mail.google.com/mail/feed/atom';
- my %hash; #in order to whether this email have been alerted
- if(!defined($sleeptime) || $sleeptime<=0){
- $sleeptime=300; #5 minis
- }
- ########################################
- #check wether the gamil account and password works
- unless(checkGmailAccount($user,$passwd,$gmail_address)){
- print STDERR "Sorry,gmail account and password does not match.";
- exit(1);
- }
- ###############################
- #main part
- while(1){
- my $content=`curl -su $user:$passwd $gmail_address`;
-
- my %mail=parseMail($content);
- foreach my $key (keys %mail){
- if(exists($hash{$key})){
- #do nothing, it has been send already
- }else{
- my $msg=join "\n",($mail{$key}->{'name'},$mail{$key}->{'email'},$mail{$key}->{'modified'},$mail{$key}->{'title'},$mail{$key}->{'summary'});
- $msg=substr($msg,0,180); #as fetion api can only send no more than 180 words
- sedSMS($from,$to,$fetionpasswd,$msg);
- sleep(4); #then do with another mail
- $hash{$key}=1;
- }
- }
- sleep($sleeptime);
- }
- sub checkGmailAccount{
- my($user,$passwd,$gmail_address) = @_;
- my $content=`curl -su $user:$passwd $gmail_address`;
- if($content=~/<TITLE>Unauthorized<\/TITLE>/){
- return 0;
- }else{
- return 1;
- }
- }
- sub parseMail{
- my($s)=@_;
- my %mail;
- while($s=~/\<entry\>([\s\S]*?)\<\/entry\>/g){
- my $mail=$1;
- my($title,$summary,$modified,$name,$email);
- if($mail=~/\<title\>([\s\S]*?)\<\/title\>/){
- $title=$1;
- }
- if($mail=~/<summary>([\s\S]*?)<\/summary>/){
- $summary=$1;
- }
- if($mail=~/<modified>([\s\S]*?)<\/modified>/){
- $modified=$1;
- }
- if($mail=~/<name>([\s\S]*?)<\/name>/){
- $name=$1;
- }
- if($mail=~/<email>([\s\S]*?)<\/email>/){
- $email=$1;
- }
- my $key=join "\t",($email,$modified);
- $mail{$key}->{'title'}=$title;
- $mail{$key}->{'summary'}=$summary;
- $mail{$key}->{'modified'}=$modified;
- $mail{$key}->{'name'}=$name;
- $mail{$key}->{'email'}=$email;
- }
- return %mail;
- }
- sub sedSMS{
- my($from,$to,$passwd,$msg) = @_;
- my $ua = LWP::UserAgent->new;
- my %form=("username"=>$from,"password"=>$passwd,"sendto"=>$to,"message"=>$msg);
- my $headderref = { 'Accept-Charset' => "utf-8" };
- my $apiurl='http://sms.api.bz/fetion.php';
- my $res=$ua->post($apiurl,\%form,$headderref);
- print $res->content,"\n";
- }
- sub usage(){
- print <<HERE;
- perl alertGmail.pl -u gmail account (etc.gmail\@gmail.com)
- -p gmail account password(etc.111111)
- -f fetion telphone number(etc. 13912345678)
- -fp fetion account password(etc.123456)
- -t to which telphone you want to send the SMS(etc.13912345678)
- -d how often to check the gmail(default 300s)
- -a print about page
- -h print out this page
- HERE
- exit(0);
- }
- sub about{
- print <<HERE;
- ######
- ##### #######
- ######## about myself ###################
- Master in Bioinformatics,Basic contact is below:
- ################################################
- #Author :Jiang Li
- #Email :riverlee2008\@gmail.com
- #MSN :riverlee2008\@live.cn
- #Address:Harbin Medical University
- #TEl :+86-13936514493
- #################################################
- HERE
- exit(0);
- }
復制代碼 |
評分
-
查看全部評分
|