1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Postman_Zend_Mail
* @subpackage Transport
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
*
* @see Postman_Zend_Mime
*/
// require_once 'Zend/Mime.php';
/**
*
* @see Postman_Zend_Mail_Protocol_Smtp
*/
// require_once 'Zend/Mail/Protocol/Smtp.php';
/**
*
* @see Postman_Zend_Mail_Transport_Abstract
*/
// require_once 'Zend/Mail/Transport/Abstract.php';
/**
* SMTP connection object
*
* Loads an instance of Postman_Zend_Mail_Protocol_Smtp and forwards smtp transactions
*
* @category Zend
* @package Postman_Zend_Mail
* @subpackage Transport
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
if (! class_exists ( 'PostmanGmailApiModuleZendMailTransport' )) {
class PostmanGmailApiModuleZendMailTransport extends Postman_Zend_Mail_Transport_Abstract {
const SERVICE_OPTION = 'service';
const MESSAGE_SENDER_EMAIL_OPTION = 'sender_email';
private $logger;
private $message;
private $transcript;
/**
* EOL character string used by transport
*
* @var string
* @access public
*/
public $EOL = "\n";
/**
* Remote smtp hostname or i.p.
*
* @var string
*/
protected $_host;
/**
* Port number
*
* @var integer|null
*/
protected $_port;
/**
* Local client hostname or i.p.
*
* @var string
*/
protected $_name = 'localhost';
/**
* Authentication type OPTIONAL
*
* @var string
*/
protected $_auth;
/**
* Config options for authentication
*
* @var array
*/
protected $_config;
/**
* Instance of Postman_Zend_Mail_Protocol_Smtp
*
* @var Postman_Zend_Mail_Protocol_Smtp
*/
protected $_connection;
/**
* Constructor.
*
* @param string $host
* OPTIONAL (Default: 127.0.0.1)
* @param array|null $config
* OPTIONAL (Default: null)
* @return void
*
* @todo Someone please make this compatible
* with the SendMail transport class.
*/
public function __construct($host = '127.0.0.1', Array $config = array()) {
if (isset ( $config ['name'] )) {
$this->_name = $config ['name'];
}
if (isset ( $config ['port'] )) {
$this->_port = $config ['port'];
}
if (isset ( $config ['auth'] )) {
$this->_auth = $config ['auth'];
}
$this->_host = $host;
$this->_config = $config;
$this->logger = new PostmanLogger ( get_class ( $this ) );
}
/**
* Class destructor to ensure all open connections are closed
*
* @return void
*/
public function __destruct() {
if ($this->_connection instanceof Postman_Zend_Mail_Protocol_Smtp) {
try {
$this->_connection->quit ();
} catch ( Postman_Zend_Mail_Protocol_Exception $e ) {
// ignore
}
$this->_connection->disconnect ();
}
}
/**
* Sets the connection protocol instance
*
* @param Postman_Zend_Mail_Protocol_Abstract $client
*
* @return void
*/
public function setConnection(Postman_Zend_Mail_Protocol_Abstract $connection) {
$this->_connection = $connection;
}
/**
* Gets the connection protocol instance
*
* @return Postman_Zend_Mail_Protocol|null
*/
public function getConnection() {
return $this->_connection;
}
/**
* Send an email via the Gmail API
*
* Uses URI https://www.googleapis.com
*
*
* @return void
* @todo Rename this to sendMail, it's a public method...
*/
public function _sendMail() {
// Prepare the message in message/rfc822
$message = $this->header . Postman_Zend_Mime::LINEEND . $this->body;
$this->message = $message;
// The message needs to be encoded in Base64URL
$encodedMessage = rtrim ( strtr ( base64_encode ( $message ), '+/', '-_' ), '=' );
$googleApiMessage = new Google_Service_Gmail_Message ();
$googleService = $this->_config [self::SERVICE_OPTION];
$googleClient = $googleService->getClient();
$file_size = strlen($message);
$result = array ();
try {
$googleClient->setDefer(true);
$result = $googleService->users_messages->send ( 'me', $googleApiMessage, array('uploadType' => 'resumable') );
$chunkSizeBytes = 1 * 1024 * 1024;
// create mediafile upload
$media = new Google_Http_MediaFileUpload(
$googleClient,
$result,
'message/rfc822',
$message,
true,
$chunkSizeBytes
);
$media->setFileSize($file_size);
$status = false;
while (! $status) {
$status = $media->nextChunk();
}
$result = false;
// Reset to the client to execute requests immediately in the future.
$googleClient->setDefer(false);
$googleMessageId = $status->getId();
if ($this->logger->isInfo ()) {
$this->logger->info ( sprintf ( 'Message %d accepted for delivery', PostmanState::getInstance ()->getSuccessfulDeliveries () + 1 ) );
}
$this->transcript = print_r ( $result, true );
$this->transcript .= PostmanModuleTransport::RAW_MESSAGE_FOLLOWS;
$this->transcript .= $message;
} catch ( Exception $e ) {
$this->transcript = $e->getMessage ();
$this->transcript .= PostmanModuleTransport::RAW_MESSAGE_FOLLOWS;
$this->transcript .= $message;
throw $e;
}
}
public function getMessage() {
return $this->message;
}
public function getTranscript() {
return $this->transcript;
}
/**
* Format and fix headers
*
* Some SMTP servers do not strip BCC headers. Most clients do it themselves as do we.
*
* @access protected
* @param array $headers
* @return void
* @throws Postman_Zend_Transport_Exception
*/
protected function _prepareHeaders($headers) {
if (! $this->_mail) {
/**
*
* @see Postman_Zend_Mail_Transport_Exception
*/
// require_once 'Zend/Mail/Transport/Exception.php';
throw new Postman_Zend_Mail_Transport_Exception ( '_prepareHeaders requires a registered Postman_Zend_Mail object' );
}
// google will unset the Bcc header for us.
// unset ( $headers ['Bcc'] );
// Prepare headers
parent::_prepareHeaders ( $headers );
}
}
}
|