+

Using MIME::Parser instead of Email::Simple

Benjamin Renard [2014-02-12 17:34:21]
Using MIME::Parser instead of Email::Simple
Filename
mail2gnokii
diff --git a/mail2gnokii b/mail2gnokii
index 77001bb..d7220a5 100755
--- a/mail2gnokii
+++ b/mail2gnokii
@@ -29,7 +29,7 @@
 #       SMSC is optionaly. If not provided, default value is used.
 #

-use Email::Simple;
+use MIME::Parser;
 use Encode qw/encode decode/;

 # Configuration
@@ -37,6 +37,9 @@ $smsc="+33609001390";
 $check_authorized_number=undef;
 @authorized_number=("0612345678","0033600000000");
 $gnokii_config="/etc/gnokiirc";
+$home_directory="/var/log/gnokii";
+
+my $message;

 # constantes tirees de <sysexits.h>
 use constant {
@@ -59,33 +62,16 @@ if($#ARGV + 1 != 1 and $#ARGV + 1 != 2){


 # recuperation du mail
+my $parser = MIME::Parser->new;
+$parser->decode_headers(1);
+$parser->output_under("/tmp");
+my $entity = $parser->parse(\*STDIN);

-my $text;
-
-while(<STDIN>){
-	$text .= $_;
-}
-
-
-if($text eq ''){
-	print "Mail is empty !";
-	exit EX_NOINPUT;
-}
-
-my $email = Email::Simple->new($text);
-
-
-# recuperation du sujet
-
-my $subject = $email->header("Subject");
-
-if($subject eq ''){
-	print "No text in Subject header !";
-	exit EX_NOINPUT;
-}
-
-$subject = decode('MIME-Header', $subject);
+my $subject = $entity->head->get("Subject");
+$subject = Encode::encode('utf-8', $subject);
+my $body = $entity->body_as_string;

+$message = "$subject\n$body\n";


 # recuperation du numero de mobile
@@ -113,7 +99,10 @@ sub in_array {

 # Envoie du SMS
 if (not $check_authorized_number or $mob ne '' && in_array(\@authorized_number,$mob)){
-	`echo "$subject" | gnokii --config $gnokii_config --sendsms $mob --smsc $smsc`;
+	chdir $home_directory;
+	open(GNOKII,  "| /usr/bin/gnokii --config $gnokii_config --sendsms $mob --smsc $smsc");
+	print GNOKII $message;
+	close(GNOKII);
 	if($? != 0){
 		print "Error sending SMS";
 		exit EX_SOFTWARE;
ViewGit