VBTK/Actions/Email.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« VBTK/Wrapper/Vxprint.pm ↑ Summary VBTK/Server.pm »
1 #############################################################################
2 #
3 # NOTE: This file under revision control using RCS
4 # Any changes made without RCS will be lost
5 #
6 # $Source: /usr/local/cvsroot/vbtk/VBTK/Actions/Email.pm,v $
7 # $Revision: 1.8 $
8 # $Date: 2002/03/04 20:53:07 $
9 # $Author: bhenry $
10 # $Locker: $
11 # $State: Exp $
12 #
13 # Purpose: An extension of the VBTK::Actions library which defaults
14 # to common settings used in creating an email action.
15 #
16 # Depends on: VBTK::Common, VBTK::Actions
17 #
18 # Copyright (C) 1996 - 2002 Brent Henry
19 #
20 # This program is free software; you can redistribute it and/or
21 # modify it under the terms of version 2 of the GNU General Public
22 # License as published by the Free Software Foundation available at:
23 # http://http://www.gnu.org/copyleft/gpl.html
24 #
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 # GNU General Public License for more details.
29 #
30 #############################################################################
31 #
32 #
33 # REVISION HISTORY:
34 #
35 # $Log: Email.pm,v $
36 # Revision 1.8 2002/03/04 20:53:07 bhenry
37 # *** empty log message ***
38 #
39 # Revision 1.7 2002/03/04 16:49:09 bhenry
40 # Changed requirement back to perl 5.6.0
41 #
42 # Revision 1.6 2002/03/02 00:53:55 bhenry
43 # Documentation updates
44 #
45 # Revision 1.5 2002/02/20 20:41:35 bhenry
46 # *** empty log message ***
47 #
48 # Revision 1.4 2002/02/20 19:25:18 bhenry
49 # *** empty log message ***
50 #
51 # Revision 1.3 2002/02/19 19:01:28 bhenry
52 # Rewrote Actions to make use of inheritance
53 #
54 # Revision 1.2 2002/01/21 17:07:44 bhenry
55 # Disabled 'uninitialized' warnings
56 #
57 # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry
58 # VBTK Project
59  
60 package VBTK::Actions::Email;
61  
62 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 # I like using undef as a value so I'm turning off the uninitialized warnings
66 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 ▶use VBTK::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use VBTK::Actions;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 0 ▶use Mail::Sendmail;
Subroutine BEGIN: called CC=1 SCAR=2.6
71  
72 # Inherit methods from Actions class
73 our @ISA = qw(VBTK::Actions);
74  
75 our $VERBOSE = $ENV{VERBOSE};
76  
77 #-------------------------------------------------------------------------------
78 # Function: new
79 # Description: Object constructor. Allocates memory for all class members
80 # Input Parms:
81 # Output Parms: Pointer to class
82 #-------------------------------------------------------------------------------
83 sub new
84 {
85 my ($type,$self);
86
87 # If we're passed a hash, then it's probably from an inheriting class
88 if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH')))
89 {
90 $self = shift;
91 }
92 # Otherwise, allocate a new hash, bless it and handle any passed parms
93 else
94 {
95 $type = shift;
96 $self = {};
97 bless $self, $type;
98  
99 # Set any passed parms
100 $self->set(@_);
101  
102 # Setup a hash of default parameters
103 my $defaultParms = {
104 Name => $::REQUIRED,
105 To => undef,
106 Cc => undef,
107 Bcc => undef,
108 From => undef,
109 Subject => "VBServer_$::HOST",
110 MessagePrefix => undef,
111 Smtp => undef,
112 Timeout => 20,
113 SendUrl => 1,
114 LimitToEvery => '2 min',
115 SubActionList => undef,
116 LogActionFlag => undef,
117 };
118  
119 # Run a validation on the passed parms, using the default parms
120 $self->validateParms($defaultParms) || &fatal("Exiting");
121 }
122  
123 # Make sure the user put in some recipient
124 &fatal("Must specify To, Cc, or Bcc in call to new VBTK::Actions::Email")
125 unless($self->{To} or $self->{Cc} or $self->{Bcc});
126  
127 # Call the parent object
128 $self->SUPER::new() || &fatal("Exiting");
129  
130 # Setup the mail hash
131 $self->{mailHash} = {
132 To => $self->{To},
133 Cc => $self->{Cc},
134 Bcc => $self->{Bcc},
135 From => $self->{From},
136 Subject => $self->{Subject},
137 Smtp => $self->{Smtp},
138 };
139  
140 ($self);
141 }
142  
143 #-------------------------------------------------------------------------------
144 # Function: run
145 # Description: Run the email action. Send email to the specified recipients
146 # Input Parms:
147 # Output Parms: Pointer to class
148 #-------------------------------------------------------------------------------
149 sub run
150 {
151 my $self = shift;
152 my $message = shift;
153 my $mailHash = $self->{mailHash};
154 my $Timeout = $self->{Timeout};
155 my $To = $self->{To};
156 my $Smtp = $self->{Smtp};
157 my $MessagePrefix = $self->{MessagePrefix};
158  
159 # Append the
160 $mailHash->{Message} = $MessagePrefix . $message;
161
162 # Run all network operations within an alarmed eval, so that nomatter
163 # where it hangs, if it doesn't finish in $timeout seconds, then it will
164 # just fail gracefully.
165 eval {
166 local $SIG{ALRM} = sub { die "Timed out while connecting\n"; };
167 alarm $Timeout;
168  
169 &log("Sending email to '$To'") if ($VERBOSE > 1);
170  
171 &sendmail(%{$mailHash}) || die "$Mail::Sendmail::error\n";
172  
173 alarm 0;
174 };
175  
176 alarm 0;
177  
178 # Check for errors
179 if($@ ne '')
180 {
181 my $msg = "Error sending mail to '$Smtp' - $@";
182 &error($msg);
183 return 0;
184 }
185
186 1;
187 }
188  
189  
190 1;
191 __END__
« VBTK/Wrapper/Vxprint.pm ↑ Summary VBTK/Server.pm »