-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetCommonsMail.php
More file actions
459 lines (403 loc) · 12.4 KB
/
NetCommonsMail.php
File metadata and controls
459 lines (403 loc) · 12.4 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* NetCommonsメール Utility
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Ryuji Masukawa
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('CakeEmail', 'Network/Email');
App::uses('SiteSetting', 'SiteManager.Model');
App::uses('WorkflowComponent', 'Workflow.Controller/Component');
App::uses('ComponentCollection', 'Controller');
App::uses('NetCommonsMailAssignTag', 'Mails.Utility');
App::uses('UserAttributeChoice', 'UserAttributes.Model');
/**
* NetCommonsメール Utility
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Mails\Utility
* @property SiteSetting $SiteSetting
* @property MailSetting $MailSetting
* @property RoomsLanguage $RoomsLanguage
* @property RolesRoomsUser $RolesRoomsUser
* @property User $User
* @property Workflow $Workflow
*/
class NetCommonsMail extends CakeEmail {
/**
* @var string 件名
*/
public $subject = null;
/**
* @var string|array 本文
*/
public $body = null;
/**
* @var array SiteSetting model data
*/
public $siteSetting = null;
/**
* @var NetCommonsMailAssignTag 埋め込みタグ Utility
*/
public $mailAssignTag = null;
/**
* @var array|null Userのemailフィールドリスト
*/
private $__userEmailFields;
/**
* Constructor
*
* @param array|string $config Array of configs, or string to load configs from email.php
* @see CakeEmail::__construct()
*/
public function __construct($config = null) {
parent::__construct($config);
$this->MailSetting = ClassRegistry::init('Mails.MailSetting');
$this->RoomsLanguage = ClassRegistry::init('Rooms.RoomsLanguage');
$this->RolesRoomsUser = ClassRegistry::init('Rooms.RolesRoomsUser');
$this->User = ClassRegistry::init('Users.User');
$this->mailAssignTag = new NetCommonsMailAssignTag();
}
/**
* 初期設定 プラグイン用
*
* @param int $languageId 言語ID
* @param string $pluginName プラグイン名
* @return void
* @see CakeEmail::$charset default=utf-8
* @see CakeEmail::$headerCharset default=utf-8
*/
public function initPlugin($languageId, $pluginName = null) {
// SiteSettingからメール設定を取得する
SiteSettingUtil::setup(array(
'Mail.from',
'Mail.from_name',
'Mail.messageType',
'Mail.transport',
'Mail.smtp.host',
'Mail.smtp.port',
'Mail.smtp.user',
'Mail.smtp.pass',
));
$this->__initConfig();
//$this->__setTags($languageId, $pluginName);
$this->mailAssignTag->initTags($languageId, $pluginName);
}
/**
* 初期設定 Shell用
*
* @param array $mailQueue メールキューデータ
* @return void
*/
public function initShell($mailQueue) {
$this->__initConfig();
$this->__setMailSettingQueue($mailQueue);
}
/**
* 初期設定 メールのコンフィグ
*
* @return void
*/
private function __initConfig() {
$config = array();
$transport = SiteSettingUtil::read('Mail.transport');
// SMTP, SMTPAuth
if ($transport == SiteSetting::MAIL_TRANSPORT_SMTP) {
$smtpHost = SiteSettingUtil::read('Mail.smtp.host');
$smtpPort = SiteSettingUtil::read('Mail.smtp.port');
$smtpUser = SiteSettingUtil::read('Mail.smtp.user');
$smtpPass = SiteSettingUtil::read('Mail.smtp.pass');
$smtpTls = SiteSettingUtil::read('Mail.smtp.tls');
$config['transport'] = 'Smtp';
$config['host'] = $smtpHost;
$config['port'] = $smtpPort;
// 値が無ければ:SMTP
// 値があれば :SMTPAuth。なのでユーザ、パス設定
if (!empty($smtpUser) && !empty($smtpPass)) {
$config['username'] = $smtpUser;
$config['password'] = $smtpPass;
}
// STARTTLSを使用する
if ($smtpTls) {
$config['tls'] = $smtpTls;
}
} elseif ($transport == SiteSetting::MAIL_TRANSPORT_PHPMAIL) {
// phpmail
$config['transport'] = 'Mail';
} elseif ($transport == 'Debug') {
// テスト用
$config['transport'] = 'Debug';
}
parent::config($config);
// html or text
$messageType = SiteSettingUtil::read('Mail.messageType');
parent::emailFormat($messageType);
}
/**
* From セット
*
* @param int $languageId 言語ID
* @return void
*/
public function setFrom($languageId) {
$from = SiteSettingUtil::read('Mail.from');
$fromName = SiteSettingUtil::read('Mail.from_name', null, $languageId);
$this->__setFrom($from, $fromName);
}
/**
* From 各種セット
*
* @param string $from 送信者メールアドレス
* @param string $fromName 送信者名
* @return void
*/
private function __setFrom($from, $fromName = null) {
parent::from($from, $fromName);
// 通称envelope-fromセット(正式名reverse-path RFC 5321)
parent::sender($from, $fromName);
// Return-Path(RFC 5322)セット - config['transport' => 'Mail']用
$config = $this->config();
$config['additionalParameters'] = '-f' . $from;
$this->config($config);
}
/**
* メール送信する件名、本文をセット
*
* @param array $mailQueue メールキューデータ
* @return void
*/
private function __setMailSettingQueue($mailQueue) {
if (empty($mailQueue)) {
return;
}
$subject = Hash::get($mailQueue, 'MailQueue.mail_subject');
$body = Hash::get($mailQueue, 'MailQueue.mail_body');
$replyTo = Hash::get($mailQueue, 'MailQueue.reply_to');
// 生文
$this->setSubject($subject);
$this->setBody($body);
// 返信先アドレス
$this->setReplyTo($replyTo);
}
/**
* 件名をセットする
*
* @param string $subject 件名
* @return void
*/
public function setSubject($subject) {
$this->subject = trim($subject);
}
/**
* 本文をセットする
*
* @param string $body 本文
* @return void
*/
public function setBody($body) {
$this->body = trim($body);
}
/**
* 返信先アドレス セット
*
* @param string $replyTo 返信先アドレス
* @return void
*/
public function setReplyTo($replyTo) {
if (! empty($replyTo)) {
parent::replyTo($replyTo);
}
}
/**
* 埋め込みタグの追加
*
* @param string $tag タグ
* @param string $value 変換する値
* @return array タグ
*/
public function assignTag($tag, $value = null) {
$this->mailAssignTag->assignTag($tag, $value);
}
/**
* 埋め込みタグを配列で追加
*
* @param array $tags タグ配列
* @return void
*/
public function assignTags($tags) {
$this->mailAssignTag->assignTags($tags);
}
/**
* 改行対応
*
* @return void
*/
public function brReplace() {
if (parent::emailFormat() == 'text') {
// text形式は配列にすると改行される
$this->body = explode("\n", $this->body);
} else {
App::uses('NetCommonsMailHtmlCleansing', 'Mails.Utility');
$this->body = (new NetCommonsMailHtmlCleansing())->cleanse($this->body);
}
}
/**
* キューからメール送信
*
* @param array $mailQueueUser メール配信先データ
* @param int $mailQueueLanguageId キューの言語ID
* @return bool|string|array false:エラー|送信メール文|送信メール文配列
*/
public function sendQueueMail($mailQueueUser, $mailQueueLanguageId) {
if ($this->body == '') {
$logMessage = 'Mail body is empty.';
LogError($logMessage . ' [' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// 改行対応
$this->brReplace();
// --- 3パターン対応
$roomId = Hash::get($mailQueueUser, 'room_id');
$userId = Hash::get($mailQueueUser, 'user_id');
$toAddress = Hash::get($mailQueueUser, 'to_address');
if ($roomId === null && $userId === null && $toAddress === null) {
$logMessage = 'Mail delivery destination is empty.';
LogError($logMessage . ' [' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
$messages = null;
if (isset($roomId)) {
// --- ルーム単位でメール配信
$blockKey = $mailQueueUser['block_key'];
$sendRoomPermission = $mailQueueUser['send_room_permission'];
$WorkflowComponent = new WorkflowComponent(new ComponentCollection());
$permissions = $WorkflowComponent->getBlockRolePermissions(array($sendRoomPermission),
$roomId, $blockKey);
$permRoleKeys = Hash::extract(
$permissions['BlockRolePermissions'][$sendRoomPermission], '{s}[value=1]');
$roleKeys = Hash::extract($permRoleKeys, '{n}.role_key');
$conditions = array(
'Room.id' => $roomId,
'RolesRoom.role_key' => $roleKeys,
);
$rolesRoomsUsers = $this->RolesRoomsUser->getRolesRoomsUsers($conditions);
$rolesRoomsUserIds = Hash::extract($rolesRoomsUsers, '{n}.RolesRoomsUser.user_id');
// 送らないユーザIDをルーム配信ユーザIDから排除
$notSendRoomUserIds = Hash::get($mailQueueUser, 'not_send_room_user_ids');
$notSendRoomUserIds = explode('|', $notSendRoomUserIds);
$userIds = array_diff($rolesRoomsUserIds, $notSendRoomUserIds);
$users = $this->User->find('all', array(
'recursive' => -1,
'conditions' => array('id' => $userIds, 'status' => UserAttributeChoice::STATUS_CODE_ACTIVE),
'callbacks' => false,
));
$messages = $this->__sendUserEmails($users, $mailQueueLanguageId);
} elseif (isset($userId)) {
// --- user単位でメール配信
$user = $this->User->find('first', array(
'recursive' => -1,
'conditions' => array('id' => $userId, 'status' => UserAttributeChoice::STATUS_CODE_ACTIVE),
'callbacks' => false,
));
$messages = $this->__sendUserEmails(array($user), $mailQueueLanguageId);
} elseif (isset($toAddress)) {
// --- メールアドレス単位でメール配信
$this->setFrom($mailQueueLanguageId);
parent::to($toAddress);
parent::subject($this->subject);
$messages = parent::send($this->body);
}
return $messages;
}
/**
* ユーザメールで送信
*
* @param array $users ユーザ 配列
* @param int $mailQueueLanguageId キューの言語ID
* @return array|null 送信メール文配列
*/
private function __sendUserEmails($users, $mailQueueLanguageId) {
$messages = null;
foreach ($users as $user) {
$receivableEmails = $this->__getReceivableEmailsFromUser($user);
foreach ($receivableEmails as $email) {
$messages[] = $this->__send($mailQueueLanguageId, $email, $messages);
}
}
return $messages;
}
/**
* メールを直送信
*
* @return bool 成功 or 失敗
*/
public function sendMailDirect() {
// 埋め込みタグ変換:定型文の埋め込みタグを変換して、メール生文にする
$this->mailAssignTag->assignTagReplace();
$this->body = $this->mailAssignTag->fixedPhraseBody;
$this->subject = $this->mailAssignTag->fixedPhraseSubject;
if ($this->body == '') {
$logMessage = 'Mail body is empty.';
LogError($logMessage . ' [' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
return false;
}
// メール本文の共通ヘッダー文、署名追加
$this->body = $this->mailAssignTag->addHeaderAndSignature($this->body);
// 改行対応
$this->brReplace();
parent::subject($this->subject);
$messages = parent::send($this->body);
return $messages;
}
/**
* __send
*
* @param int|string $mailQueueLanguageId 言語ID
* @param string $email メールアドレス
* @return array
*/
private function __send($mailQueueLanguageId, $email) {
$this->setFrom($mailQueueLanguageId);
parent::to($email);
parent::subject($this->subject);
return parent::send($this->body);
}
/**
* __getReceivableEmailsFromUser
*
* @param array $user User data
* @return string[] メールを受け取るメールアドレスリスト
*/
private function __getReceivableEmailsFromUser($user) : array {
if ($this->__userEmailFields === null) {
$this->__userEmailFields = $this->User->getEmailFields();
}
$userEmails = [];
foreach ($this->__userEmailFields as $emailField) {
$userEmails[] = [
'email' => Hash::get($user, 'User.' . $emailField),
'is_email_reception' => Hash::get($user, 'User.is_' . $emailField . '_reception'),
];
}
// メールを受け取る設定にしているメールリスト
$receivableEmails = [];
foreach ($userEmails as $userEmail) {
// 個人のメール受け取らない
if (!$userEmail['is_email_reception']) {
continue;
}
if (empty($userEmail['email'])) {
//$userId = Hash::get($user, 'User.id');
//$logMessage = "Email is empty. userId=$userId";
//CakeLog::debug($logMessage . ' [' . __METHOD__ . '] ' . __FILE__ . ' (line ' . __LINE__ . ')');
continue;
}
$receivableEmails[] = $userEmail['email'];
}
return $receivableEmails;
}
}