2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-03-03 16:56:54 +00:00

76_MSGMail: move email delivery from MSGMail_set to MSGMail_send

to be usable from perl; use Blocking_Call to deliver email



git-svn-id: https://svn.fhem.de/fhem/trunk@10648 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
gandy92 2016-01-28 11:28:30 +00:00
parent 734495062f
commit 53551d573e
2 changed files with 89 additions and 47 deletions

View File

@ -1,5 +1,7 @@
# Add changes at the top of the list. Keep it in ASCII, and 80-char wide.
# Do not insert empty lines here, update check depends on it.
- change: 76_MSGMail: move email delivery from MSGMail_set to MSGMail_send
to be usable from perl; use Blocking_Call to deliver email
- change: 55_GDS.pm uses setKeyValue/getKeyValue for user credentials
- feature: new module 52_I2C_BME280.pm added (klausw)
- bugfix: 52_I2C_PCA9685: bugfix for interaction with FRM

View File

@ -4,6 +4,8 @@
#
# History:
#
# 2016-01-27: Add MSGMail_send() to be used from perl
# Use Blocking_Call to deliver email
# 2015-05-19: Add MSGMail_Attr()
# 2015-05-15: Add attribute mailtype as suggested by Roger (forum #37206)
# 2015-05-11: Improve error logging to assist problem solving
@ -19,6 +21,8 @@ use warnings;
use MIME::Lite;
use Net::SMTP; # libnet-3.06 has SSL included, so we need to check the version
require 'Blocking.pm';
my %sets = (
"add" => "MSGMail",
"clear" => "MSGMail",
@ -233,29 +237,88 @@ sub MSGMail_Set($@)
elsif ($a[0] eq "send")
{
# check all required data
my $from = AttrVal($name, "from", "");
my $to = AttrVal($name, "to", "");
my $subject = AttrVal($name, "subject", "");
my $authfile = AttrVal($name, "authfile", "");
my $smtphost = AttrVal($name, "smtphost", "");
my $smtpport = AttrVal($name, "smtpport", "465"); # 465 is the default port
my $cc = AttrVal($name, "cc", ""); # Carbon Copy
my $mtype = AttrVal($name, "mailtype", "plain");
my $mess = "";
for (my $i = 0 ; $i < ReadingsVal($name, "msgcount", 0) ; $i++)
{
$mess .= $data{$name}{$i};
}
return MSGMail_send($name, $mess, {});
} ###> END MSGMail
Log GetLogLevel($name, 2), "messenger set $name $v";
# set stats
# $hash->{CHANGED}[0] = $v;
$hash->{READINGS}{state}{TIME} = TimeNow();
$hash->{READINGS}{state}{VAL} = $v;
return undef;
}
my %MSGMail_params = (
'from' => {'attr'=>'from', 'default'=>'', 'required'=>1, 'set'=>1},
'to' => {'attr'=>'to', 'default'=>'', 'required'=>1, 'set'=>1},
'subject' => {'attr'=>'subject', 'default'=>'', 'required'=>1, 'set'=>1},
'cc' => {'attr'=>'cc', 'default'=>'', 'required'=>0, 'set'=>1},
'mtype' => {'attr'=>'mtype', 'default'=>'plain', 'required'=>1, 'set'=>1},
'authfile' => {'attr'=>'authfile', 'default'=>'', 'required'=>1, 'set'=>0},
'smtphost' => {'attr'=>'smtphost', 'default'=>'', 'required'=>1, 'set'=>0},
'smtpport' => {'attr'=>'smtpport', 'default'=>'465', 'required'=>1, 'set'=>0},
);
sub MSGMail_send($$;$)
{
my ($name, $msgtext, $extparamref) = @_;
my %params = ();
return "unknown device $name." unless exists ($defs{$name});
foreach my $key (keys %MSGMail_params)
{
$params{$key} = AttrVal($name, $MSGMail_params{$key}->{attr}, $MSGMail_params{$key}->{default});
$params{$key} = $extparamref->{$key} if (exists $extparamref->{$key} && $MSGMail_params{$key}->{set});
#Log3 $name, 0, "param $key is now '".$params{$key}."'";
}
my $mailtype = "text/plain";
$mailtype = "text/$mtype" if ($mtype =~ m/^(plain|html)$/);
$mailtype = "text/".$params{mtype} if ($params{mtype} =~ m/^(plain|html)$/);
return "No <from> address specified, use attr $name from <mail-address>"
if (!$from);
return "No <to> address specified, use attr $name to <mail-address>"
if (!$to);
return "No <subject> specified, use attr $name subject <text>"
if (!$subject);
return "No <authfile> specified, use attr $name authfile <filename>"
if (!$authfile);
return "No <smtphost> name specified, use attr $name sntphost <hostname>"
if (!$smtphost);
$params{mailtype} = $mailtype;
$params{name} = $name;
$params{msgtext} = $msgtext;
my @err = ();
foreach my $key (keys(%MSGMail_params))
{
push(@err, $key) if ($MSGMail_params{$key}->{required} && !$params{$key});
}
return "Missing at least one required parameter or attribue: ".join(', ',@err) if ($#err >= 0);
BlockingCall("MSGMail_sendInBackground", \%params);
return "";
}
sub MSGMail_error($$$)
{
my ($name, $msg, $error) = @_;
Log3 $name, 0, "$name: $msg: $error";
return $error;
}
sub MSGMail_sendInBackground($)
{
my ($paref) = @_;
my $authfile = $paref->{authfile};
my $name = $paref->{name};
my $cc = $paref->{cc};
my $from = $paref->{from};
my $mailtype = $paref->{mailtype};
my $smtphost = $paref->{smtphost};
my $subject = $paref->{subject};
my $to = $paref->{to};
my $msgtext = $paref->{msgtext};
open(FHEMAUTHFILE, "<" . $authfile)
|| return "Can not open authfile $authfile: $!";
@ -265,20 +328,12 @@ sub MSGMail_Set($@)
# Log 1, "MSG User = <" . @auth[0] . "> Passwort = <" . @auth[1] . ">";
# compose message
my $i;
my $mess = "";
for ($i = 0 ; $i < ReadingsVal($name, "msgcount", 0) ; $i++)
{
$mess .= $data{$name}{$i};
}
my $mailmsg = MIME::Lite->new(
From => $from,
To => $to,
Subject => $subject,
Type => "$mailtype; charset=UTF-8", #'multipart/mixed', # was 'text/plain'
Data => $mess
Data => $msgtext
);
# login to the SMTP Host using SSL and send the message
@ -309,22 +364,7 @@ sub MSGMail_Set($@)
Log3 $name, 1, "$name: successfully sent email w/ subject '$subject'";
} ###> END MSGMail
Log GetLogLevel($name, 2), "messenger set $name $v";
# set stats
# $hash->{CHANGED}[0] = $v;
$hash->{READINGS}{state}{TIME} = TimeNow();
$hash->{READINGS}{state}{VAL} = $v;
return undef;
}
sub MSGMail_error($$$)
{
my ($name, $msg, $error) = @_;
Log3 $name, 0, "$name: $msg: $error";
return $error;
}
##############################################