1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Swift Mailer mail() sending plugin |
5
|
|
|
* Please read the LICENSE file |
6
|
|
|
* @author Chris Corbyn <[email protected]> |
7
|
|
|
* @package Swift_Connection |
8
|
|
|
* @license GNU Lesser General Public License |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
require_once dirname(__FILE__) . "/../ClassLoader.php"; |
12
|
|
|
Swift_ClassLoader::load("Swift_Events_SendListener"); |
13
|
|
|
Swift_ClassLoader::load("Swift_Events_BeforeSendListener"); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Swift mail() send plugin |
17
|
|
|
* Sends the message using mail() when a SendEvent is fired. Using the NativeMail connection provides stub responses to allow this to happen cleanly. |
18
|
|
|
* @package Swift_Connection |
19
|
|
|
* @author Chris Corbyn <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Swift_Plugin_MailSend implements Swift_Events_SendListener, Swift_Events_BeforeSendListener |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The operating system of the server |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $OS = null; |
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The return path in use here |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $returnPath = null; |
33
|
|
|
/** |
34
|
|
|
* The line ending before we intrusively change it |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $oldLE = "\r\n"; |
38
|
|
|
/** |
39
|
|
|
* 5th parameter in mail(). |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $additionalParams; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* @param string 5th mail() function parameter as a sprintf() formatted string where %s is the sender. |
47
|
|
|
*/ |
48
|
|
|
public function __construct($params="-oi -f %s") |
49
|
|
|
{ |
50
|
|
|
$this->setAdditionalParams($params); |
51
|
|
|
$this->setOS(PHP_OS); |
52
|
|
|
} |
53
|
|
|
/** |
54
|
|
|
* Set the 5th mail() function parameter as a sprintf() formatted string where %s is the sender. |
55
|
|
|
* @param string |
56
|
|
|
*/ |
57
|
|
|
public function setAdditionalParams($params) |
58
|
|
|
{ |
59
|
|
|
$this->additionalParams = $params; |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* Get the 5th mail() function parameter as a sprintf() string. |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getAdditionalParams() |
66
|
|
|
{ |
67
|
|
|
return $this->additionalParams; |
68
|
|
|
} |
69
|
|
|
/** |
70
|
|
|
* Set the operating system string (changes behaviour with LE) |
71
|
|
|
* @param string The operating system |
72
|
|
|
* @param string $os |
73
|
|
|
*/ |
74
|
|
|
public function setOS($os) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->OS = $os; |
77
|
|
|
} |
78
|
|
|
/** |
79
|
|
|
* Get the operating system string |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getOS() |
83
|
|
|
{ |
84
|
|
|
return $this->OS; |
85
|
|
|
} |
86
|
|
|
/** |
87
|
|
|
* Check if this is windows or not |
88
|
|
|
* @return boolean |
89
|
|
|
*/ |
90
|
|
|
public function isWindows() |
91
|
|
|
{ |
92
|
|
|
return (substr($this->getOS(), 0, 3) == "WIN"); |
93
|
|
|
} |
94
|
|
|
/** |
95
|
|
|
* Swift's BeforeSendEvent listener. |
96
|
|
|
* Invoked just before Swift sends a message |
97
|
|
|
* @param Swift_Events_SendEvent The event information |
98
|
|
|
*/ |
99
|
|
|
public function beforeSendPerformed(Swift_Events_SendEvent $e) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$message = $e->getMessage(); |
102
|
|
|
$message->uncacheAll(); |
103
|
|
|
$this->oldLE = $message->getLE(); |
104
|
|
|
if (!$this->isWindows() && $this->oldLE != "\n") $message->setLE("\n"); |
105
|
|
|
} |
106
|
|
|
/** |
107
|
|
|
* Swift's SendEvent listener. |
108
|
|
|
* Invoked when Swift sends a message |
109
|
|
|
* @param Swift_Events_SendEvent The event information |
110
|
|
|
* @throws Swift_ConnectionException If mail() returns false |
111
|
|
|
*/ |
112
|
|
|
public function sendPerformed(Swift_Events_SendEvent $e) |
|
|
|
|
113
|
|
|
{ |
114
|
|
|
$message = $e->getMessage(); |
115
|
|
|
$recipients = $e->getRecipients(); |
116
|
|
|
|
117
|
|
|
$to = array(); |
|
|
|
|
118
|
|
|
foreach ($recipients->getTo() as $addr) |
119
|
|
|
{ |
120
|
|
|
if ($this->isWindows()) $to[] = substr($addr->build(true), 1, -1); |
121
|
|
|
else $to[] = $addr->build(); |
122
|
|
|
} |
123
|
|
|
$to = implode(", ", $to); |
124
|
|
|
|
125
|
|
|
$bcc_orig = $message->headers->has("Bcc") ? $message->headers->get("Bcc") : null; |
126
|
|
|
$subject_orig = $message->headers->has("Subject") ? $message->headers->get("Subject") : null; |
127
|
|
|
$to_orig = $message->headers->has("To") ? $message->headers->get("To") : null; |
128
|
|
|
|
129
|
|
|
$bcc = array(); |
130
|
|
|
foreach ($recipients->getBcc() as $addr) $bcc[] = $addr->build(); |
131
|
|
|
if (!empty($bcc)) $message->headers->set("Bcc", $bcc); |
132
|
|
|
$bcc = null; |
|
|
|
|
133
|
|
|
|
134
|
|
|
$body_data = $message->buildData(); |
135
|
|
|
$message_body = $body_data->readFull(); |
136
|
|
|
|
137
|
|
|
$subject_enc = $message->headers->has("Subject") ? $message->headers->getEncoded("Subject") : ""; |
138
|
|
|
|
139
|
|
|
$message->headers->set("To", null); |
140
|
|
|
$message->headers->set("Subject", null); |
141
|
|
|
|
142
|
|
|
$sender = $e->getSender(); |
143
|
|
|
$this->returnPath = $sender->build(); |
144
|
|
|
if ($message->headers->has("Return-Path")) $this->returnPath = $message->headers->get("Return-Path"); |
145
|
|
|
if (preg_match("~<([^>]+)>[^>]*\$~", $this->returnPath, $matches)) $this->returnPath = $matches[1]; |
146
|
|
|
|
147
|
|
|
$this->doMail($to, $subject_enc, $message_body, $message->headers, sprintf($this->getAdditionalParams(), $this->returnPath)); |
148
|
|
|
$message->setLE($this->oldLE); |
149
|
|
|
$message->headers->set("To", $to_orig); |
150
|
|
|
$message->headers->set("Subject", $subject_orig); |
151
|
|
|
$message->headers->set("Bcc", $bcc_orig); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $to |
156
|
|
|
* @param string $subject |
157
|
|
|
* @param string $message |
158
|
|
|
* @param Swift_Message_Headers $headers |
159
|
|
|
* @param string $params |
160
|
|
|
*/ |
161
|
|
|
public function doMail($to, $subject, $message, $headers, $params) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$original_from = @ini_get("sendmail_from"); |
164
|
|
|
@ini_set("sendmail_from", $this->returnPath); |
|
|
|
|
165
|
|
|
|
166
|
|
|
$headers = $headers->build(); |
167
|
|
|
|
168
|
|
|
if (!ini_get("safe_mode")) $success = mail($to, $subject, $message, $headers, $params); |
169
|
|
|
else $success = mail($to, $subject, $message, $headers); |
170
|
|
|
|
171
|
|
|
if (!$success) |
172
|
|
|
{ |
173
|
|
|
@ini_set("sendmail_from", $original_from); |
|
|
|
|
174
|
|
|
throw new Swift_ConnectionException("Sending failed using mail() as PHP's default mail() function returned boolean FALSE."); |
175
|
|
|
} |
176
|
|
|
@ini_set("sendmail_from", $original_from); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.