All Data Structures Functions Variables Pages
EmailArchivingJob.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 
41  {
45  public static function getDisplayName()
46  {
47  return Zurmo::t('EmailMessagesModule', 'Process Inbound Email Job');
48  }
49 
53  public static function getType()
54  {
55  return 'EmailArchiving';
56  }
57 
58  public static function getRecommendedRunFrequencyContent()
59  {
60  return Zurmo::t('EmailMessagesModule', 'Every 5 minutes.');
61  }
62 
63  protected function processMessage(ImapMessage $message)
64  {
65  return $this->saveEmailMessage($message);
66  }
67 
68  protected function getLastImapDropboxCheckTime()
69  {
70  return EmailMessagesModule::getLastArchivingImapDropboxCheckTime();
71  }
72 
73  protected function setLastImapDropboxCheckTime($time)
74  {
75  EmailMessagesModule::setLastArchivingImapDropboxCheckTime($time);
76  }
77 
78  protected function resolveImapObject()
79  {
80  if (!isset($this->imapManager))
81  {
82  $this->imapManager = Yii::app()->imap;
83  }
84  return $this->imapManager;
85  }
86 
94  protected function resolveMessageSubjectAndContentAndSendSystemMessage($messageType, $originalMessage)
95  {
96  $sendNotification = false;
97  switch ($messageType)
98  {
99  case "OwnerNotExist":
100  $subject = Zurmo::t('EmailMessagesModule', 'Invalid email address');
101  $textContent = Zurmo::t('EmailMessagesModule', 'Email address does not exist in system') . "\n\n" . $originalMessage->textBody;
102  $htmlContent = Zurmo::t('EmailMessagesModule', 'Email address does not exist in system') . "<br><br>" . $originalMessage->htmlBody;
103  $sendNotification = true;
104  break;
105  case "SenderNotExtracted":
106  $subject = Zurmo::t('EmailMessagesModule', "Sender info can't be extracted from email message");
107  $textContent = Zurmo::t('EmailMessagesModule', "Sender info can't be extracted from email message") . "\n\n" . $originalMessage->textBody;
108  $htmlContent = Zurmo::t('EmailMessagesModule', "Sender info can't be extracted from email message") . "<br><br>" . $originalMessage->htmlBody;
109  break;
110  case "RecipientNotExtracted":
111  $subject = Zurmo::t('EmailMessagesModule', "Recipient info can't be extracted from email message");
112  $textContent = Zurmo::t('EmailMessagesModule', "Recipient info can't be extracted from email message") . "\n\n" . $originalMessage->textBody;
113  $htmlContent = Zurmo::t('EmailMessagesModule', "Recipient info can't be extracted from email message") . "<br><br>" . $originalMessage->htmlBody;
114  break;
115  case "EmailMessageNotValidated":
116  $subject = Zurmo::t('EmailMessagesModule', 'Email message could not be validated');
117  $textContent = Zurmo::t('EmailMessagesModule', 'Email message could not be validated') . "\n\n" . $originalMessage->textBody;
118  $htmlContent = Zurmo::t('EmailMessagesModule', 'Email message could not be validated') . "<br><br>" . $originalMessage->htmlBody;
119  break;
120  case "EmailMessageNotSaved":
121  $subject = Zurmo::t('EmailMessagesModule', 'Email message could not be saved');
122  $textContent = Zurmo::t('EmailMessagesModule', 'Email message could not be saved') . "\n\n" . $originalMessage->textBody;
123  $htmlContent = Zurmo::t('EmailMessagesModule', 'Email message could not be saved') . "<br><br>" . $originalMessage->htmlBody;
124  break;
125  default:
126  throw new NotSupportedException();
127  }
128  if ($sendNotification)
129  {
130  $notificationMessage = new NotificationMessage();
131  $notificationMessage->textContent = $textContent;
132  $notificationMessage->htmlContent = DataUtil::purifyHtml($htmlContent);
134  NotificationsUtil::submit($notificationMessage, $rules);
135  }
136  else
137  {
138  return EmailMessageHelper::sendSystemEmail($subject, array($originalMessage->fromEmail), $textContent, $htmlContent);
139  }
140  }
141 
149  public function saveEmailMessage(ImapMessage $message)
150  {
151  // Get owner for message
152  try
153  {
154  $emailOwner = EmailArchivingUtil::resolveOwnerOfEmailMessage($message);
155  }
156  catch (CException $e)
157  {
158  // User not found, so inform user about issue and continue with next email.
159  $this->resolveMessageSubjectAndContentAndSendSystemMessage('OwnerNotExist', $message);
160  return false;
161  }
162  $emailSenderOrRecipientEmailFoundInSystem = false;
163  $userCanAccessContacts = RightsUtil::canUserAccessModule('ContactsModule', $emailOwner);
164  $userCanAccessLeads = RightsUtil::canUserAccessModule('LeadsModule', $emailOwner);
165  $userCanAccessAccounts = RightsUtil::canUserAccessModule('AccountsModule', $emailOwner);
166 
168  if (!$senderInfo)
169  {
170  $this->resolveMessageSubjectAndContentAndSendSystemMessage('SenderNotExtracted', $message);
171  return false;
172  }
173  else
174  {
175  $sender = EmailArchivingUtil::createEmailMessageSender($senderInfo, $userCanAccessContacts,
176  $userCanAccessLeads, $userCanAccessAccounts);
177 
178  if ($sender->personsOrAccounts->count() > 0)
179  {
180  $emailSenderOrRecipientEmailFoundInSystem = true;
181  }
182  }
183 
184  try
185  {
187  }
188  catch (NotSupportedException $exception)
189  {
190  $this->resolveMessageSubjectAndContentAndSendSystemMessage('RecipientNotExtracted', $message);
191  return false;
192  }
193 
194  $emailMessage = new EmailMessage();
195  $emailMessage->owner = $emailOwner;
196  $emailMessage->subject = $message->subject;
197 
198  $emailContent = new EmailMessageContent();
199  $emailContent->textContent = $message->textBody;
200  $emailContent->htmlContent = $message->htmlBody;
201  $emailMessage->content = $emailContent;
202  $emailMessage->sender = $sender;
203 
204  $emailRecipientFoundInSystem = false;
205  foreach ($recipientsInfo as $recipientInfo)
206  {
207  $recipient = EmailArchivingUtil::createEmailMessageRecipient($recipientInfo, $userCanAccessContacts,
208  $userCanAccessLeads, $userCanAccessAccounts);
209  $emailMessage->recipients->add($recipient);
210  // Check if at least one recipient email can't be found in Contacts, Leads, Account and User emails
211  // so we will save email message in EmailFolder::TYPE_ARCHIVED_UNMATCHED folder, and user will
212  // be able to match emails with items(Contacts, Accounts...) emails in systems
213  if ($recipient->personsOrAccounts->count() > 0)
214  {
215  $emailRecipientFoundInSystem = true;
216  }
217  }
218 
219  // Override $emailSenderOrRecipientEmailFoundInSystem only if there are no errors
220  if ($emailSenderOrRecipientEmailFoundInSystem == true)
221  {
222  $emailSenderOrRecipientEmailFoundInSystem = $emailRecipientFoundInSystem;
223  }
224  if ($emailOwner instanceof User)
225  {
226  $box = EmailBoxUtil::getDefaultEmailBoxByUser($emailOwner);
227  }
228  else
229  {
230  $box = EmailBox::resolveAndGetByName(EmailBox::NOTIFICATIONS_NAME);
231  }
232  if (!$emailSenderOrRecipientEmailFoundInSystem)
233  {
234  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_ARCHIVED_UNMATCHED);
235  $notificationMessage = new NotificationMessage();
236  $notificationMessage->textContent = Zurmo::t('EmailMessagesModule', 'At least one archived email message does ' .
237  'not match any records in the system. ' .
238  'To manually match them use this link: {url}.',
239  array(
240  '{url}' => Yii::app()->createUrl('emailMessages/default/matchingList'),
241  )
242  );
243  $notificationMessage->htmlContent = Zurmo::t('EmailMessagesModule', 'At least one archived email message does ' .
244  'not match any records in the system. ' .
245  '<a href="{url}" target="_blank">Click here</a> to manually match them.',
246  array(
247  '{url}' => Yii::app()->createUrl('emailMessages/default/matchingList'),
248  )
249  );
250  if ($emailOwner instanceof User)
251  {
253  $rules->addUser($emailOwner);
254  NotificationsUtil::submit($notificationMessage, $rules);
255  }
256  }
257  else
258  {
259  $emailMessage->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_ARCHIVED);
260  }
261 
262  if (!empty($message->attachments))
263  {
264  foreach ($message->attachments as $attachment)
265  {
266  if (!$attachment['is_attachment'])
267  {
268  continue;
269  }
270  $file = EmailArchivingUtil::createEmailAttachment($attachment);
271  if ($file instanceof FileModel)
272  {
273  $emailMessage->files->add($file);
274  }
275  }
276  }
277  $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
278  $validated = $emailMessage->validate();
279  if (!$validated)
280  {
281  // Email message couldn't be validated(some related models can't be validated). Email user.
282  $this->resolveMessageSubjectAndContentAndSendSystemMessage('EmailMessageNotValidated', $message);
283  return false;
284  }
285 
287  $saved = $emailMessage->save();
288  try
289  {
290  if (!$saved)
291  {
292  throw new NotSupportedException();
293  }
294  if (isset($message->uid)) // For tests uid will not be setup
295  {
296  $this->imapManager->deleteMessage($message->uid);
297  $this->getMessageLogger()->addDebugMessage('Deleted Message id: ' . $message->uid);
298  }
299  }
300  catch (NotSupportedException $e)
301  {
302  // Email message couldn't be saved. Email user.
303  $this->resolveMessageSubjectAndContentAndSendSystemMessage('EmailMessageNotSaved', $message);
304  return false;
305  }
306  return true;
307  }
308  }
309 ?>
Definition: User.php:37
static resolveEmailSenderFromEmailMessage(ImapMessage $emailMessage)
static resolveEmailRecipientsFromEmailMessage(ImapMessage $emailMessage)
saveEmailMessage(ImapMessage $message)
static sendSystemEmail($subject, $recipients, $textContent= '', $htmlContent= '')
resolveMessageSubjectAndContentAndSendSystemMessage($messageType, $originalMessage)
static createEmailMessageSender($senderInfo, $userCanAccessContacts, $userCanAccessLeads, $userCanAccessAccounts)
static resolveOwnerOfEmailMessage(ImapMessage $emailMessage)
static canUserAccessModule($moduleClassName, $user)
Definition: RightsUtil.php:103
static createEmailAttachment($attachment, $base64encoded=false)
static purifyHtml($text)
Definition: DataUtil.php:165
static resolveSanitizeFromImapToUtf8(EmailMessage $message)
static submit(NotificationMessage $message, NotificationRules $rules)
static getDefaultEmailBoxByUser(User $user, $refreshUserModel=true)
static createEmailMessageRecipient($recipientInfo, $userCanAccessContacts, $userCanAccessLeads, $userCanAccessAccounts)
Generated on Sat Feb 22 2020 07:10:29