|
1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Email helper class. |
|
4
|
|
|
* |
|
5
|
|
|
* $Id: email.php 3769 2008-12-15 00:48:56Z zombor $ |
|
6
|
|
|
* |
|
7
|
|
|
* @package Core |
|
8
|
|
|
* @author Kohana Team |
|
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
|
10
|
|
|
* @license http://kohanaphp.com/license.html |
|
11
|
|
|
*/ |
|
12
|
|
|
class email_Core |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
// SwiftMailer instance |
|
16
|
|
|
protected static $mail; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Creates a SwiftMailer instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string DSN connection string |
|
22
|
|
|
* @return Swift Swift object |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function connect($config = null) |
|
25
|
|
|
{ |
|
26
|
|
|
if (! class_exists('Swift', false)) { |
|
27
|
|
|
// Load SwiftMailer |
|
28
|
|
|
require Kohana::find_file('vendor', 'swift/Swift'); |
|
29
|
|
|
|
|
30
|
|
|
// Register the Swift ClassLoader as an autoload |
|
31
|
|
|
spl_autoload_register(array('Swift_ClassLoader', 'load')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Load default configuration |
|
35
|
|
|
($config === null) and $config = Kohana::config('email'); |
|
36
|
|
|
|
|
37
|
|
|
switch ($config['driver']) { |
|
38
|
|
|
case 'smtp': |
|
39
|
|
|
// Set port |
|
40
|
|
|
$port = empty($config['options']['port']) ? null : (int) $config['options']['port']; |
|
41
|
|
|
|
|
42
|
|
|
if (empty($config['options']['encryption'])) { |
|
43
|
|
|
// No encryption |
|
44
|
|
|
$encryption = Swift_Connection_SMTP::ENC_OFF; |
|
45
|
|
|
} else { |
|
46
|
|
|
// Set encryption |
|
47
|
|
|
switch (strtolower($config['options']['encryption'])) { |
|
48
|
|
|
case 'tls': $encryption = Swift_Connection_SMTP::ENC_TLS; break; |
|
49
|
|
|
case 'ssl': $encryption = Swift_Connection_SMTP::ENC_SSL; break; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Create a SMTP connection |
|
54
|
|
|
$connection = new Swift_Connection_SMTP($config['options']['hostname'], $port, $encryption); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
// Do authentication, if part of the DSN |
|
57
|
|
|
empty($config['options']['username']) or $connection->setUsername($config['options']['username']); |
|
58
|
|
|
empty($config['options']['password']) or $connection->setPassword($config['options']['password']); |
|
59
|
|
|
|
|
60
|
|
|
if (! empty($config['options']['auth'])) { |
|
61
|
|
|
// Get the class name and params |
|
62
|
|
|
list($class, $params) = arr::callback_string($config['options']['auth']); |
|
63
|
|
|
|
|
64
|
|
|
if ($class === 'PopB4Smtp') { |
|
65
|
|
|
// Load the PopB4Smtp class manually, due to its odd filename |
|
66
|
|
|
require Kohana::find_file('vendor', 'swift/Swift/Authenticator/$PopB4Smtp$'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Prepare the class name for auto-loading |
|
70
|
|
|
$class = 'Swift_Authenticator_'.$class; |
|
71
|
|
|
|
|
72
|
|
|
// Attach the authenticator |
|
73
|
|
|
$connection->attachAuthenticator(($params === null) ? new $class : new $class($params[0])); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Set the timeout to 5 seconds |
|
77
|
|
|
$connection->setTimeout(empty($config['options']['timeout']) ? 5 : (int) $config['options']['timeout']); |
|
78
|
|
|
break; |
|
79
|
|
|
case 'sendmail': |
|
80
|
|
|
// Create a sendmail connection |
|
81
|
|
|
$connection = new Swift_Connection_Sendmail( |
|
82
|
|
|
empty($config['options']) ? Swift_Connection_Sendmail::AUTO_DETECT : $config['options'] |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
// Set the timeout to 5 seconds |
|
86
|
|
|
$connection->setTimeout(5); |
|
87
|
|
|
break; |
|
88
|
|
|
default: |
|
89
|
|
|
// Use the native connection |
|
90
|
|
|
$connection = new Swift_Connection_NativeMail($config['options']); |
|
91
|
|
|
break; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Create the SwiftMailer instance |
|
95
|
|
|
return email::$mail = new Swift($connection); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Send an email message. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string|array recipient email (and name), or an array of To, Cc, Bcc names |
|
102
|
|
|
* @param string|array sender email (and name) |
|
103
|
|
|
* @param string message subject |
|
104
|
|
|
* @param string message body |
|
105
|
|
|
* @param boolean send email as HTML |
|
106
|
|
|
* @return integer number of emails sent |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function send($to, $from, $subject, $message, $html = false) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
// Connect to SwiftMailer |
|
111
|
|
|
(email::$mail === null) and email::connect(); |
|
112
|
|
|
|
|
113
|
|
|
// Determine the message type |
|
114
|
|
|
$html = ($html === true) ? 'text/html' : 'text/plain'; |
|
115
|
|
|
|
|
116
|
|
|
// Create the message |
|
117
|
|
|
$message = new Swift_Message($subject, $message, $html, '8bit', 'utf-8'); |
|
118
|
|
|
|
|
119
|
|
|
if (is_string($to)) { |
|
120
|
|
|
// Single recipient |
|
121
|
|
|
$recipients = new Swift_Address($to); |
|
122
|
|
|
} elseif (is_array($to)) { |
|
123
|
|
|
if (isset($to[0]) and isset($to[1])) { |
|
124
|
|
|
// Create To: address set |
|
125
|
|
|
$to = array('to' => $to); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// Create a list of recipients |
|
129
|
|
|
$recipients = new Swift_RecipientList; |
|
130
|
|
|
|
|
131
|
|
|
foreach ($to as $method => $set) { |
|
132
|
|
|
if (! in_array($method, array('to', 'cc', 'bcc'))) { |
|
133
|
|
|
// Use To: by default |
|
134
|
|
|
$method = 'to'; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Create method name |
|
138
|
|
|
$method = 'add'.ucfirst($method); |
|
139
|
|
|
|
|
140
|
|
|
if (is_array($set)) { |
|
141
|
|
|
// Add a recipient with name |
|
142
|
|
|
$recipients->$method($set[0], $set[1]); |
|
143
|
|
|
} else { |
|
144
|
|
|
// Add a recipient without name |
|
145
|
|
|
$recipients->$method($set); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
if (is_string($from)) { |
|
151
|
|
|
// From without a name |
|
152
|
|
|
$from = new Swift_Address($from); |
|
153
|
|
|
} elseif (is_array($from)) { |
|
154
|
|
|
// From with a name |
|
155
|
|
|
$from = new Swift_Address($from[0], $from[1]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return email::$mail->send($message, $recipients, $from); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
} // End email |
|
161
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: