All Data Structures Functions Variables Pages
SendGridEmailEventsJob.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
42  {
46  public static function getDisplayName()
47  {
48  return Zurmo::t('SendGridModule', 'SendGrid Email Events Job');
49  }
50 
54  public static function getType()
55  {
56  return 'SendGridEmailEvents';
57  }
58 
59  public static function getRecommendedRunFrequencyContent()
60  {
61  return Zurmo::t('SendGridModule', 'Every 5 minute.');
62  }
63 
68  public function run()
69  {
70  $sendGridEmailAccounts = SendGridEmailAccount::getAll();
71  foreach ($sendGridEmailAccounts as $sendGridEmailAccount)
72  {
73  $eventWebhookFilePath = SendGridLogUtil::getLogFilePath($sendGridEmailAccount->apiUsername);
74  if ($eventWebhookFilePath != null && file_exists($eventWebhookFilePath))
75  {
76  $this->processEventData($eventWebhookFilePath);
77  @file_put_contents($eventWebhookFilePath, '');
78  }
79  }
80  //Global
81  if (Yii::app()->sendGridEmailHelper->apiUsername != '')
82  {
83  $globalWebHookFilePath = SendGridLogUtil::getLogFilePath(Yii::app()->sendGridEmailHelper->apiUsername);
84  if ($globalWebHookFilePath != null && file_exists($globalWebHookFilePath))
85  {
86  $this->processEventData($globalWebHookFilePath);
87  @file_put_contents($globalWebHookFilePath, '');
88  }
89  }
90  return true;
91  }
92 
97  protected static function resolveAndUpdateEventInformationByStatus(& $value)
98  {
99  if ($value['event'] == 'spamreport')
100  {
101  $value['type'] = EmailMessageActivity::TYPE_SPAM;
102  $value['reason'] = Zurmo::t('SendGridModule', 'Marked as spam');
103  }
104  if ($value['event'] == 'bounce')
105  {
106  if (strpos($value['status'], "4") == 0)
107  {
108  $value['type'] = EmailMessageActivity::TYPE_SOFT_BOUNCE;
109  }
110  if (strpos($value['status'], "5") == 0)
111  {
112  $value['type'] = EmailMessageActivity::TYPE_HARD_BOUNCE;
113  }
114  }
115  if ($value['event'] == 'dropped')
116  {
117  $models = ExternalApiEmailMessageActivity::getByEmailAddress($value['email'], "sendgrid", false);
118  if (count($models) == 1)
119  {
120  $value['type'] = $models[0]->type;
121  }
122  else
123  {
124  $value['type'] = EmailMessageActivity::TYPE_BOUNCE;
125  }
126  }
127  }
128 
134  protected function processEventData($eventWebhookFilePath)
135  {
136  if ($eventWebhookFilePath != null)
137  {
138  $content = $this->resolveFileContent($eventWebhookFilePath);
139  preg_match_all('/\[{(.*?)}\]/i', $content, $matches);
140  $data = array();
141  foreach ($matches[1] as $string)
142  {
143  $data[] = json_decode('{' . $string . '}', true);
144  }
145  foreach ($data as $value)
146  {
147  if ($value['event'] == 'bounce' || $value['event'] == 'spamreport' || $value['event'] == 'dropped')
148  {
149  if (ArrayUtil::getArrayValue($value, 'itemClass', false) !== false &&
150  ArrayUtil::getArrayValue($value, 'itemId', false) !== false)
151  {
152  $this->processActivityInformation($value);
153  }
154  }
155  }
156  }
157  }
158 
164  protected function saveExternalApiEmailMessageActivity(EmailMessageActivity $emailMessageActivity, $value)
165  {
166  $externalApiEmailMessageActivity = new ExternalApiEmailMessageActivity();
167  $externalApiEmailMessageActivity->emailMessageActivity = $emailMessageActivity;
168  $externalApiEmailMessageActivity->api = 'sendgrid';
169  $externalApiEmailMessageActivity->type = $value['type'];
170  $externalApiEmailMessageActivity->reason = $value['reason'];
171  $externalApiEmailMessageActivity->itemClass = $value['itemClass'];
172  $externalApiEmailMessageActivity->itemId = $value['itemId'];
173  $externalApiEmailMessageActivity->emailAddress = $value['email'];
174  $externalApiEmailMessageActivity->save();
175  }
176 
182  protected function resolveFileContent($eventWebhookFilePath)
183  {
184  return file_get_contents($eventWebhookFilePath);
185  }
186 
192  protected function getActivityTypeByEvent($value)
193  {
194  if ($value['event'] == 'bounce' || $value['event'] == 'dropped')
195  {
196  $type = EmailMessageActivity::TYPE_BOUNCE;
197  }
198  else
199  {
200  $type = EmailMessageActivity::TYPE_SPAM;
201  }
202  return $type;
203  }
204 
209  protected function processActivityInformation($value)
210  {
211  $type = $this->getActivityTypeByEvent($value);
212  $activityClassName = EmailMessageActivityUtil::resolveModelClassNameByModelType($value['itemClass']);
213  $activityUtilClassName = $activityClassName . 'Util';
214  $activityData = array('modelId' => $value['itemId'],
215  'modelType' => $value['itemClass'],
216  'personId' => $value['personId'],
217  'url' => null,
218  'type' => $type);
219  if ($activityUtilClassName::createOrUpdateActivity($activityData))
220  {
221  //$activityClassName = CampaignItemActivity
222  $emailMessageActivities = $activityClassName::getByTypeAndModelIdAndPersonIdAndUrl($type, $value['itemId'], $value['personId'], null);
223  self::resolveAndUpdateEventInformationByStatus($value);
224  $externalMessageActivityCount = ExternalApiEmailMessageActivity::getByTypeAndEmailMessageActivity($value['type'], $emailMessageActivities[0], "sendgrid");
225  if ($externalMessageActivityCount == 0)
226  {
227  $this->saveExternalApiEmailMessageActivity($emailMessageActivities[0], $value);
228  }
229  $this->resolveMarkingPersonPrimaryEmailAsInvalid($value['type'], $value['personId']);
230  }
231  }
232 
233  public function resolveMarkingPersonPrimaryEmailAsInvalid($type, $zurmoPersonId)
234  {
235  if ($type == EmailMessageActivity::TYPE_HARD_BOUNCE)
236  {
238  }
239  }
240  }
241 ?>
static resolveAndUpdateEventInformationByStatus(&$value)
static getArrayValue($array, $element, $defaultValue=null)
Definition: ArrayUtil.php:45
static markPersonPrimaryEmailAsInvalid($personId)
static getByEmailAddress($email, $api, $isCount=true)
saveExternalApiEmailMessageActivity(EmailMessageActivity $emailMessageActivity, $value)
static getLogFilePath($apiUsername)
resolveFileContent($eventWebhookFilePath)
static getAll($orderBy=null, $sortDescending=false, $modelClassName=null)
static getByTypeAndEmailMessageActivity($type, $itemActivity, $api, $isCount=true)
processEventData($eventWebhookFilePath)
Generated on Sun May 31 2020 07:10:35