CMS  Version 3.9
mail.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 class Mail
40 {
41  var $server;
42  var $port;
43  var $from;
44  var $to;
45  var $fullFrom;
46 
47  var $subject;
48  var $body;
49  var $headers;
50 
51  function Mail($from, $to, $subject, $body = "", $headers = "", $fullFrom = "")
52  {
53  if (Settings::getValue("email", "override_PHP_settings"))
54  {
55  $this->server = Settings::getValue("email", "mail_server");
56  $this->port = Settings::getValue("email", "smtp_port");
57  }
58  else
59  {
60  $this->server = ini_get("SMTP");
61  $this->port = ini_get("smtp_port");
62  }
63 
64  $this->from = $from;
65  $this->to = $to;
66  $this->subject = encode7bit($subject);
67  $this->body = $body;
68  $this->headers = $headers;
69  $this->fullFrom = encode7bit($fullFrom);
70  }
71 
72  function send()
73  {
74  $debug = Settings::getValue("email", "use_debugging_mode");
75  $debugPath = Settings::getValue("email", "debugging_mode_output_path");
76 
77  if ($debug)
78  {
79  $fp = fopen($debugPath . DIRECTORY_SEPARATOR . $this->to, 'a');
80  if($fp === FALSE)
81  {
82  trace("Mail: unable to open debug log file in $debugPath name {$this->to}", 3);
83  return false;
84  }
85 
86  $rtn = fwrite($fp, "From: $this->from\n".
87  "To: $this->to\n".
88  $this->headers.
89  "Subject: $this->subject\n".
90  "\n".
91  "$this->body\n\n");
92 
93  if($rtn === FALSE)
94  {
95  trace("Mail: unable to write to debug log file in $debugPath name {$this->to}", 3);
96  return false;
97  }
98 
99  fclose($fp);
100  return true;
101  }
102 
103  $smtp = new SMTP;
104  //$smtp->do_debug = 1; # sets the amount of debug information we get
105 
106  # connect to the smtp server
107  if(!$smtp->connect($this->server, $this->smtp_port))
108  {
109  die ("Error: " . $smtp->error["error"] . "\n");
110  }
111 
112  # say hello so we all know who we are
113  $smtp->Hello();
114 
115  $username = Settings::getValue("email", "username");
116  $password = Settings::getValue("email", "password");
117 
118  if ($username && $password)
119  {
120  if (!$smtp->Authenticate($username, $password))
121  {
122  $err = $smtp->error["error"];
123  trace("SMTP Authentication failed: $err", 1);
124  $smtp->quit();
125  throw new FakoliException($err);
126  }
127  }
128 
129  # start the mail transaction with with the return path address
130  $smtp->Mail($this->from);
131 
132  if ($smtp->error) trace("FROM: ".print_r($smtp->error, true), 2);
133 
134  # state each recipient
135  # NOTE: this does _not_ add to: or cc: headers to the email
136  $smtp->Recipient($this->to);
137 
138 
139  if ($smtp->error) trace("RCPT: ".print_r($smtp->error, true), 2);
140 
141  if (!$this->fullFrom) $this->fullFrom = $this->from;
142 
143  # send the message including headers (headers must be followed by an
144  # empty newline
145  $data = "From: $this->fullFrom\n".
146  "To: $this->to\n".
147  $this->headers.
148  "Subject: $this->subject\n".
149  "\n".
150  "$this->body\n";
151 
152  $smtp->Data($data);
153 
154  if ($smtp->error) trace(print_r($smtp->error, true), 2);
155 
156  # the mail either been sent or failed either way we are done
157  $smtp->quit();
158 
159  return true;
160  }
161 }?>
$err
Definition: error_log.inc:9
$username
FakoliException is the base exception class for all Fakoli errors.
Definition: core.inc:53
Definition: mail.inc:40
$to
Definition: mail.inc:44
$port
Definition: mail.inc:42
$fullFrom
Definition: mail.inc:45
$server
Definition: mail.inc:41
$from
Definition: mail.inc:43
send()
Definition: mail.inc:72
$body
Definition: mail.inc:48
$headers
Definition: mail.inc:49
$subject
Definition: mail.inc:47
Mail($from, $to, $subject, $body="", $headers="", $fullFrom="")
Definition: mail.inc:51
Definition: smtp.inc:54
static getValue($component, $name)
Retrieve the value of the specified Setting.
Definition: settings.inc:104
$forumRequest to
$documentMove from
$email subject