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: svn://svn.code.sf.net/p/fhem/code/trunk@10648 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
gandy92
2016-01-28 11:28:30 +00:00
parent c30e8c516e
commit 86eb1b20df
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. # 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. # 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 - change: 55_GDS.pm uses setKeyValue/getKeyValue for user credentials
- feature: new module 52_I2C_BME280.pm added (klausw) - feature: new module 52_I2C_BME280.pm added (klausw)
- bugfix: 52_I2C_PCA9685: bugfix for interaction with FRM - bugfix: 52_I2C_PCA9685: bugfix for interaction with FRM

View File

@@ -4,6 +4,8 @@
# #
# History: # 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-19: Add MSGMail_Attr()
# 2015-05-15: Add attribute mailtype as suggested by Roger (forum #37206) # 2015-05-15: Add attribute mailtype as suggested by Roger (forum #37206)
# 2015-05-11: Improve error logging to assist problem solving # 2015-05-11: Improve error logging to assist problem solving
@@ -19,6 +21,8 @@ use warnings;
use MIME::Lite; use MIME::Lite;
use Net::SMTP; # libnet-3.06 has SSL included, so we need to check the version use Net::SMTP; # libnet-3.06 has SSL included, so we need to check the version
require 'Blocking.pm';
my %sets = ( my %sets = (
"add" => "MSGMail", "add" => "MSGMail",
"clear" => "MSGMail", "clear" => "MSGMail",
@@ -233,29 +237,88 @@ sub MSGMail_Set($@)
elsif ($a[0] eq "send") elsif ($a[0] eq "send")
{ {
# check all required data my $mess = "";
my $from = AttrVal($name, "from", ""); for (my $i = 0 ; $i < ReadingsVal($name, "msgcount", 0) ; $i++)
my $to = AttrVal($name, "to", ""); {
my $subject = AttrVal($name, "subject", ""); $mess .= $data{$name}{$i};
my $authfile = AttrVal($name, "authfile", ""); }
my $smtphost = AttrVal($name, "smtphost", "");
my $smtpport = AttrVal($name, "smtpport", "465"); # 465 is the default port return MSGMail_send($name, $mess, {});
my $cc = AttrVal($name, "cc", ""); # Carbon Copy } ###> END MSGMail
my $mtype = AttrVal($name, "mailtype", "plain");
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"; 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>" $params{mailtype} = $mailtype;
if (!$from); $params{name} = $name;
return "No <to> address specified, use attr $name to <mail-address>" $params{msgtext} = $msgtext;
if (!$to);
return "No <subject> specified, use attr $name subject <text>" my @err = ();
if (!$subject); foreach my $key (keys(%MSGMail_params))
return "No <authfile> specified, use attr $name authfile <filename>" {
if (!$authfile); push(@err, $key) if ($MSGMail_params{$key}->{required} && !$params{$key});
return "No <smtphost> name specified, use attr $name sntphost <hostname>" }
if (!$smtphost); 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) open(FHEMAUTHFILE, "<" . $authfile)
|| return "Can not open authfile $authfile: $!"; || return "Can not open authfile $authfile: $!";
@@ -265,20 +328,12 @@ sub MSGMail_Set($@)
# Log 1, "MSG User = <" . @auth[0] . "> Passwort = <" . @auth[1] . ">"; # 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( my $mailmsg = MIME::Lite->new(
From => $from, From => $from,
To => $to, To => $to,
Subject => $subject, Subject => $subject,
Type => "$mailtype; charset=UTF-8", #'multipart/mixed', # was 'text/plain' 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 # 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'"; 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;
} }
############################################## ##############################################