緣起~
內部 Push Mail 的需求.
在現實機房內,可能有些設備,基本上支援簡單的 Mail Alert,如 NetApp / Fortigate ....
所以我們可以透過 Push Mail 的方式,將 Mail Alert 轉發至 手機的 SMS.
如下的參考資訊與方式:
http://www.bulksms.com/features/send-sms-messages-from-your-email.htm
http://developer.bulksms.com/eapi/code-samples/
http://developer.bulksms.com/eapi/code-samples/perl/send_sms/
send_sms.pl
#!/usr/bin/perl -w
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(timeout => 30);
# Please see the FAQ regarding HTTPS (port 443) and HTTP (port 80/5567)
my $res = $ua->request(POST '?EAPI URL?/submission/send_sms/2/2.0',
Header => 'Content-Type: application/x-www-form-urlencoded',
Content => [
username => 'myusername',
password => 'xxxxxxxxxx',
msisdn => '44123123123',
message => 'Test from Perl',
],
);
if ($res->is_error) {
die "HTTP request error, with error code ".$res->code.
", and body:\n\n".$res->error_as_HTML;
}
my ($result_code, $result_string, $batch_id) = split(/\|/, $res->content);
if ($result_code eq '0') {
print "Message sent: batch $batch_id";
}
else {
print "Error sending: $result_code: $result_string";
}
print "\n";
上面這個 Mail 2 SMS GateWay 的方式是由專門的公司才提供的服務.
如要 DIY Push Mail 的方法,可參考如下,以 Perl 為例:
http://cpansearch.perl.org/src/LENGEL/Net-SMS-2Way-0.08-FIXED/contrib/email2sms.pl
# Author: Lee Engel, <lee@kode.co.za>
# Copyright (C) 2009 by Lee S. Engel
# A very simple email-to-sms gateway.
# INSTALLATION INSTRUCTIONS:
# Create a user which will handle all the email-to-sms stuff. Example: adduser -m -d /home/sms sms
# Install Net::SMS::2Way and create a config file for it at /home/sms/sms.cfg
# Install the MailTool Perl module. (See http://search.cpan.org/~markov/MailTools-2.04/) Try this: perl -MCPAN -e " install( 'MailTool' ); "
# Create a .forward file in the sms user's home directory: echo '|/home/sms/email2sms.pl' > /home/sms/.forward
# Change your alias_maps config option in /etc/postfix/main.cf to look like this: alias_maps = hash:/etc/aliases pcre:/etc/aliases-regexp
# Create /etc/aliases-regexp with a line which looks likes this: /^\d+$/ sms
# Copy this script to /home/sms/email2sms.pl and make it executable by all.
經由上述說明得知可以透過 mail .forward 的機制達到這個功能,
由於 Net::SMS::2Way 內定是使用 BulkSMS API
http://bulksms.2way.co.za/docs/eapi/submission/send_sms/
並不太適用於個人實際運用,故想自創一個 Email to SMS Gateway ( Push Mail Server) 的想法,
抄改上述程式範例修改如下;
[root@aaa zabbix]# cat .forward
|/home/zabbix/sms/email2sms.pl
[root@aaa zabbix]# cat /home/zabbix/sms/email2sms.pl
#!/usr/bin/perl
use Mail::Header;
use Mail::Internet;
use Mail::Send;
$mail = Mail::Internet->new(\*STDIN);
$mail_headers = $mail->head();
$mail_body = $mail->body();
$mail->tidy_body( $mail_body );
$body_text = join( "\n", @$mail_body );
chomp( $body_text );
if( length( $body_text ) > 160 )
{
$body_text = substr( $body_text, 0, (160 - length($body_text)) );
}
$headers = $mail_headers->header_hashref();
$sender_address = $headers->{From}->[0];
$sender_address =~ s/^(\S+)\s+.*/$1/;
chomp( $sender_address);
@to_headers = qw( To X-Original-To Delivered-To );
foreach $to_header ( @to_headers )
{
open(write_log,">>/home/zabbix/mail2sms.txt");
print write_log "$sender_address $body_text\n";
close(write_log);
#在這裡就可以寫自己的企業簡訊 API介接的地方
last;
}
[root@aaa zabbix]#
這樣會發成當有人寄 Mail 給 zabbix 時,會觸發上面的 .forward
==> 接者在 Run 這個 shell
接者將 $body_text 轉導成 SMS 的 API 內文即可,這樣就完成
Push Mail 的簡單任務了.
沒有留言:
張貼留言