Odyssey
errormail.i
1 <?php
2 /**
3  * @var ErrorMail
4  * It hardly has anything to do with errors...
5  * It is the generic class for sending emails.
6  * All in-house PHP at this point should be using it.
7  * That is Monitor, Admin, and Banking.
8  */
9 class ErrorMail {
10 
11  var $logmode;
12  var $logfile;
13  var $header;
14  var $mailcmd;
15  var $mailto;
16  var $mailfrom;
17  var $mailfromname;
18  var $replyto;
19  var $script;
20  var $subject;
21  var $msgbody;
22  var $line;
23  var $file;
24  var $callingfunction;
25  var $physfile;
26  var $error;
27  var $server_id;
28  var $sql;
29  var $errdate;
30  var $cu;
31  var $member;
32 
33  function __construct() {
34  /*
35  * changed name of function from ErrorMail to __construct
36  * in response to this PHP7 error
37  * Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP
38  */
39 
40  #-----------------------------------------
41  # EDITABLE CONFIGURATION VARIABLES
42 
43  $this->header = ""; # any headers, delimited with semi-colon
44  $this->mailto = "cuerror@homecu.net"; # recipient id
45  $this->hideto = false; # if true, to is bcc instead of to
46  $this->mailfrom = "noreply@homecu.net"; # default sender id ** NOTE: This should be JUST an email address with NO NAME somebody@email.com NOT "SomeBody" <somebody@email.com>
47  $this->mailfromname = ''; // ** SET the name to be empty
48  // * START the replyto off as empty. IF set by a script then I will send it in the header
49  $this->replyto = "";
50  $this->subject = "Digital Banking Message"; # default subject
51  $this->msgbody = ""; # default empty message
52  $this->htmlMsgbody = "";
53  $this->plainMsgbody = ""; // Allow the option of directly setting these parts.
54  $this->executeQuietly = false; // add `--quiet` option to a email sending python script
55  $this->bulk = false; // send bulk emails
56 
57  // When called from scheduler scripts such as sCalendarNotify.i and schedliveproc.php, these attributes are not defined.
58  $this->script = HCU_array_key_value("SCRIPT_NAME", $_SERVER); # What script?
59  $this->server = HCU_array_key_value("SERVER_NAME", $_SERVER); # What Server?
60  $this->query = HCU_array_key_value("REQUEST_URL", $_SERVER); # How was it called
61  $this->errdate = `date +"%a, %d %b %Y %T %Z"`;
62 
63  #------------------------------------------
64 
65  }
66 
67  /**
68  * Obtain a python file that will send the email.
69  */
70  private function getPyFileLocation() {
71  # Goal is to default it with a script with more generalized functionality
72  # added in sendSESEmailBulk.py script
73  if ($this->bulk) {
74  return dirname(__FILE__) . "/../scripts/sendSESEmailBulk.py";
75  } else {
76  return dirname(__FILE__) . "/../scripts/sendSESEmail.py";
77  }
78  }
79 
80  /**
81  * Prepare a a dictionary of {context: "json ob"}
82  * --context option for Python Email Sending Script
83  */
84  private function getAwsSesLoggingContextJsonString() {
85 
86  $context = array();
87 
88  // these attributes are expected to be set on the calling
89  // side before invoking SendMail and SendErr
90  $context["cucode"] = $this->cu;
91  $context["callingScript"] = $this->file;
92  $context["callingFunction"] = $this->callingfunction;
93  $context["serverName"] = $this->server;
94  $context["ServerScript"] = $this->script;
95 
96  return HCU_JsonEncode($context);
97  }
98 
99 
100  /**
101  * function SendMail()
102  * Send an email through AWS SES.
103  * Like the previous implemention, the email is sent in the background.
104  * That means that there is no checking to see if the email has failed.
105  * For AWS SES fails are:
106  * 1) Invalid syntax of either from or to emails (should be checked before),
107  * 2) The From email is not verified
108  * (at this point in time, the default of noreply@homecu.net is not verified.),
109  * For more information, @see the sendSESEmail.py script.
110  */
111  function SendMail() {
112  $pyFile = $this->getPyFileLocation();
113 
114  // Set up from
115  $from = $this->mailfromname != '' ? "{$this->mailfromname} <{$this->mailfrom}>" : $this->mailfrom;
116  $fromString = "--efrom " . escapeshellarg($from);
117 
118  // Set up reply to
119  $replyToString = $this->replyto != "" ? "--replyto " . escapeshellarg($this->replyto) : "";
120 
121  // Set up subject
122  $subjectString = "--subject " . escapeshellarg($this->subject);
123 
124  // Now HTML emails are set up using the header declaration (legacy).
125  // Also allow direct setting of the plaintext and htmltext parts of the email.
126  $plainTextBody = "";
127  $htmlTextBody = "";
128  if ($this->htmlMsgbody != "") {
129  $htmlTextBody = $this->htmlMsgbody;
130  }
131  if ($this->plainMsgbody != "") {
132  $plainTextBody = $this->plainMsgbody;
133  }
134  if ($this->msgbody != "") {
135  if (trim(strtolower($this->header)) == "content-type: text/html") { // For the most part, they have the same case except for a couple.
136  $htmlTextBody = $this->msgbody;
137  } else {
138  $plainTextBody = $this->msgbody;
139  }
140  }
141 
142  // Need to escape newlines. They cause the email to fail.
143  $plainTextString = "";
144  $htmlTextString = "";
145  if ($plainTextBody != "") {
146  // Requires some finagling: if newline is literal \n then bash line doesn't work but with \\n, it shows up in the email as "\n."
147  $plainTextBody = str_replace("\n", "\\n", str_replace("\r\n", "\\r\\n", $plainTextBody));
148  $plainTextString = "--plaintext \"`echo " . escapeshellarg($plainTextBody) . "`\"";
149  }
150  if ($htmlTextBody != "") {
151  // Do not replace newlines with breaks. Script is responsible for displaying correctly; also breaks image data (in the case of billing/workflow emails.)
152  $htmlTextString = "--htmltext " . escapeshellarg($htmlTextBody);
153  }
154 
155  if ($plainTextString == "" && $htmlTextString == "") {
156  $plainTextString = "--plaintext ''";
157  }
158 
159  // Set up to. Amazon has a limit of 50 recipients so parse at this point.
160  $toArray = is_array($this->mailto) ? $this->mailto : explode(";", str_replace(",", ";", strval($this->mailto)));
161  $toSwitch = $this->hideto ? "--bcc" : "--to";
162  $executeQuietly = $this->executeQuietly ? "--quiet" : "";
163 
164  // sanitize list of emails
165  $toArray = array_map("trim", $toArray);
166  $toArray = array_map("escapeshellarg", $toArray);
167 
168  $toString = "$toSwitch " . implode(" ", $toArray);
169 
170  $awsSesLoggingContext = "--context " . escapeshellarg($this->getAwsSesLoggingContextJsonString());
171 
172  $cmd = "(python3 $pyFile $toString $fromString $subjectString $plainTextString \
173  $htmlTextString $replyToString $executeQuietly $awsSesLoggingContext) > /dev/null &";
174 
175  exec($cmd);
176  }
177 
178  /**
179  * function SendErr()
180  * This sends an email pointing out an error.
181  * At this point in time, all references in Odyssey have not been updated.
182  * No, this is not the email in Mammoth which comes from the error handler.
183  */
184  function SendErr() {
185  $pyFile = $this->pyFile;
186 
187  $msg = "";
188  $msg .= "Error in $this->script on Server $this->server\\n";
189  $msg .= "Called as $this->query\\n\\n";
190  $msg .= $this->line == "" ? "" : "LINE: $this->line\\n";
191  $msg .= $this->file == "" ? "" : "FILE: $this->file\\n";
192  $msg .= $this->error == "" ? "" : "ERROR: $this->error\\n";
193  $msg .= $this->sql == "" ? "" : "SQL: $this->sql\\n";
194  $msg .= $this->cu == "" ? "" : "CU: $this->cu\\n";
195  $msg .= $this->member == "" ? "" : "MEMBER: $this->member\\n";
196 
197  // Set up message
198  // Requires some finagling: if newline is literal \n then bash line doesn't work but with \\n, it shows up in the email as "\n."
199  $plainTextString = "--plaintext \"`echo " . escapeshellarg($msg) . "`\"";
200 
201  // Set up subject
202  $subject = "Error in $this->script";
203  $subjectString = "--subject " . escapeshellarg($subject);
204 
205  // Set up from
206  $fromString = "--efrom " . escapeshellarg($this->mailfrom);
207 
208  // Set up to. Amazon has a limit of 50 recipients so parse at this point.
209  $toArray = is_array($this->mailto) ? $this->mailto : explode(";", str_replace(",", ";", strval($this->mailto)));
210  $toSwitch = $this->hideto ? "--bcc" : "--to";
211  $toList = array();
212 
213  $awsSesLoggingContext = "--context " . escapeshellarg($this->getAwsSesLoggingContextJsonString());
214 
215  foreach($toArray as $toEmail) {
216  if (trim($toEmail) != "") {
217  $toList[] = escapeshellarg($toEmail);
218 
219  if (count($toList) >= 50) {
220  $toString = "$toSwitch " . implode(" ", $toList);
221  $cmd = "(python3 $pyFile $toString $fromString $subjectString $plainTextString $awsSesLoggingContext) &";
222  exec($cmd);
223  $toList = array();
224  }
225  }
226  }
227 
228  $toString = "$toSwitch " . implode(" ", $toList);
229  $cmd = "(python3 $pyFile $toString $fromString $subjectString $plainTextString $awsSesLoggingContext) &";
230  exec($cmd);
231  }
232 
233 }
SendMail()
Definition: errormail.i:111
getAwsSesLoggingContextJsonString()
Definition: errormail.i:84
getPyFileLocation()
Definition: errormail.i:70