Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
MailNotificator.php
1 <?php
2 /**
3  * Gentics Portal.Node PHP
4  * Author & Copyright (c) by Gentics Software GmbH
5  * sales@gentics.com
6  * http://www.gentics.com
7  * Licenses can be found in the LICENSE.txt file in the root-folder of this installation
8  * You must not use this software without a valid license agreement.
9  *
10  * Class for notification users via email
11  *
12  */
14 {
15  /**
16  * @var string cache time for user model
17  */
18  public $cacheTime = 3600;
19 
20  /**
21  * @var array emails to which notification sent by default
22  */
23  public $defaultEmails = array();
24 
25  /**
26  * @var array PhpMailer instance properties
27  */
28  public $phpMailer = array();
29 
30  /**
31  * Create instance of mail object
32  *
33  * @return PHPMailer
34  */
35  public function createMail()
36  {
37  $mail = new PHPMailer(true);
38  foreach ($this->phpMailer as $prop => $value) {
39  $mail->$prop = $value;
40  }
41  return $mail;
42  }
43 
44  /**
45  * Check if email recipient is allowed to get mails
46  *
47  * @param EmailRecipient $recipient recipient
48  *
49  * @return bool
50  */
51  protected function validate($recipient)
52  {
53  if ($recipient instanceof EmailRecipient) {
54  if (!$recipient->skipValidation && $userRecipient = User::model()->cache(Yii::app()->notificationManager->cacheTime)->with('profile')->findByAttributes(array('email' => $recipient->to))) {
55  if ($userRecipient->profile->send_me_notifications == false) {
56  Yii::trace("Email skipped {$recipient->to}", 'MailNotificator');
57  return false;
58  }
59  }
60  Yii::trace("Email approved {$recipient->to}", 'MailNotificator');
61  return true;
62  }
63  return false;
64  }
65 
66  /**
67  * Send notification to all recipients who is instance of EmailRecipient
68  *
69  * @param Notification $notification occurred notification
70  *
71  * @return void
72  */
73  public function notify(Notification $notification)
74  {
75  foreach ($notification->recipients as $recipient) {
76  if ($this->validate($recipient)) {
77  $mail = $this->createMail();
78  if ($recipient->from) {
79  $mail->SetFrom($recipient->from);
80  }
81  $mail->AddAddress($recipient->to);
82  $mail->Subject = $notification->subject;
83  $mail->MsgHTML($notification->message);
84  try {
85  $mail->Send();
86  Yii::trace("Mail sent success. Subject:{$notification->subject}. To:{$recipient->to}, From: {$recipient->from}", 'MailNotificator');
87  } catch (Exception $e) {
88  Yii::log($e->getMessage(), CLogger::LEVEL_ERROR);
89  Yii::trace("Mail sent FAIL. Subject:{$notification->subject}. To:{$recipient->to}, From: {$recipient->from}", 'MailNotificator');
90  }
91  }
92  }
93  }
94 }