Using MIME::Parser instead of Email::Simple
Benjamin Renard

Benjamin Renard commited on 2014-02-12 17:36:31
Showing 1 changed files, with 16 additions and 27 deletions.

... ...
@@ -29,7 +29,7 @@
29 29
 #       SMSC is optionaly. If not provided, default value is used.
30 30
 #
31 31
 
32
-use Email::Simple;
32
+use MIME::Parser;
33 33
 use Encode qw/encode decode/;
34 34
 
35 35
 # Configuration
... ...
@@ -37,6 +37,9 @@ $smsc="+33609001390";
37 37
 $check_authorized_number=undef;
38 38
 @authorized_number=("0612345678","0033600000000");
39 39
 $gnokii_config="/etc/gnokiirc";
40
+$home_directory="/var/log/gnokii";
41
+
42
+my $message;
40 43
 
41 44
 # constantes tirees de <sysexits.h>
42 45
 use constant {
... ...
@@ -59,33 +62,16 @@ if($#ARGV + 1 != 1 and $#ARGV + 1 != 2){
59 62
 
60 63
 
61 64
 # recuperation du mail
65
+my $parser = MIME::Parser->new;
66
+$parser->decode_headers(1);
67
+$parser->output_under("/tmp");
68
+my $entity = $parser->parse(\*STDIN);
62 69
 
63
-my $text;
64
-
65
-while(<STDIN>){
66
-	$text .= $_;
67
-}
68
-
69
-
70
-if($text eq ''){
71
-	print "Mail is empty !";
72
-	exit EX_NOINPUT;
73
-}
74
-
75
-my $email = Email::Simple->new($text);
76
-
77
-
78
-# recuperation du sujet
79
-
80
-my $subject = $email->header("Subject");
81
-
82
-if($subject eq ''){
83
-	print "No text in Subject header !";
84
-	exit EX_NOINPUT;
85
-}
86
-
87
-$subject = decode('MIME-Header', $subject);
70
+my $subject = $entity->head->get("Subject");
71
+$subject = Encode::encode('utf-8', $subject);
72
+my $body = $entity->body_as_string;
88 73
 
74
+$message = "$subject\n$body\n";
89 75
 
90 76
 
91 77
 # recuperation du numero de mobile
... ...
@@ -113,7 +99,10 @@ sub in_array {
113 99
 
114 100
 # Envoie du SMS
115 101
 if (not $check_authorized_number or $mob ne '' && in_array(\@authorized_number,$mob)){
116
-	`echo "$subject" | gnokii --config $gnokii_config --sendsms $mob --smsc $smsc`;
102
+	chdir $home_directory;
103
+	open(GNOKII,  "| /usr/bin/gnokii --config $gnokii_config --sendsms $mob --smsc $smsc");
104
+	print GNOKII $message;
105
+	close(GNOKII);
117 106
 	if($? != 0){
118 107
 		print "Error sending SMS";
119 108
 		exit EX_SOFTWARE;
120 109