Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ZurmoSendGridMailer.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  Yii::import('ext.sendgrid.lib.*');
37  Yii::import('ext.sendgrid.lib.SendGrid.*');
38  Yii::import('ext.sendgrid.lib.Smtpapi.*');
39 
44  {
45  protected $emailHelper;
46 
47  protected $fromUser;
48 
49  protected $toAddresses;
50 
51  protected $ccAddresses;
52 
53  protected $bccAddresses;
54 
55  protected $fromUserEmailData;
56 
57  protected $emailAccount;
58 
59  protected $emailMessage;
60 
66  public function __construct(EmailMessage $emailMessage, $emailAccount)
67  {
68  SendGrid::register_autoloader();
69  Smtpapi::register_autoloader();
70  $from = array('address' => $emailMessage->sender->fromAddress, 'name' => $emailMessage->sender->fromName);
71  $this->fromUserEmailData = $from;
72  $this->emailMessage = $emailMessage;
74  $this->emailAccount = $emailAccount;
75  }
76 
82  public function sendTestEmail($isUser = true)
83  {
84  $this->emailMessage->mailerType = 'sendgrid';
85  if ($isUser)
86  {
87  $this->emailMessage->mailerSettings = 'personal';
88  }
89  if ($this->emailMessage->validate())
90  {
91  $this->emailMessage->save();
92  self::resolveRecipientAddressesByType();
93  $this->sendEmail();
94  }
95  return $this->emailMessage;
96  }
97 
101  public function sendEmail()
102  {
103  $emailMessage = $this->emailMessage;
104  if ($this->emailAccount == null)
105  {
106  $apiUser = Yii::app()->sendGridEmailHelper->apiUsername;
107  $apiPassword = Yii::app()->sendGridEmailHelper->apiPassword;
108  }
109  else
110  {
111  $apiUser = $this->emailAccount->apiUsername;
112  $apiPassword = ZurmoPasswordSecurityUtil::decrypt($this->emailAccount->apiPassword);
113  }
115  $sendgrid = new SendGrid($apiUser, $apiPassword, array("turn_off_ssl_verification" => true));
116  $email = new SendGrid\Email();
117  $email->setFrom($this->fromUserEmailData['address'])->
118  setFromName($this->fromUserEmailData['name'])->
119  setSubject($emailMessage->subject)->
120  setText($emailMessage->content->textContent)->
121  setHtml($emailMessage->content->htmlContent)->
122  addUniqueArg("zurmoToken", md5(ZURMO_TOKEN))->
123  addHeader('X-Sent-Using', 'SendGrid-API')->
124  addHeader('X-Transport', 'web');
125 
126  //Check if campaign and if yes, associate to email.
127  if ($itemData != null)
128  {
129  list($itemId, $itemClass, $personId) = $itemData;
130  $email->addUniqueArg("itemId", $itemId);
131  $email->addUniqueArg("itemClass", $itemClass);
132  $email->addUniqueArg("personId", $personId);
133  }
134  foreach ($this->toAddresses as $emailAddress => $name)
135  {
136  $email->addTo($emailAddress, $name);
137  }
138  foreach ($this->ccAddresses as $emailAddress => $name)
139  {
140  $email->addCc($emailAddress);
141  }
142  foreach ($this->bccAddresses as $emailAddress => $name)
143  {
144  $email->addBcc($emailAddress);
145  }
146  //Attachments
147  $attachmentsData = array();
148  $tempAttachmentPath = Yii::app()->getRuntimePath() . DIRECTORY_SEPARATOR . 'emailAttachments';
149  if (!file_exists($tempAttachmentPath))
150  {
151  mkdir($tempAttachmentPath);
152  }
153  if (!empty($emailMessage->files))
154  {
155  foreach ($emailMessage->files as $file)
156  {
157  $fileName = tempnam($tempAttachmentPath, 'zurmo_');
158  $fp = fopen($fileName, 'wb');
159  fwrite($fp, $file->fileContent->content);
160  fclose($fp);
161  $email->addAttachment($fileName, $file->name);
162  $attachmentsData[] = $fileName;
163  }
164  }
165  $emailMessage->sendAttempts = $emailMessage->sendAttempts + 1;
166  $response = $sendgrid->send($email);
167  if ($response->message == 'success')
168  {
169  //Here we need to check if
170  $emailMessage->error = null;
171  $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox, EmailFolder::TYPE_SENT);
172  $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
173  }
174  //In case message is not delivered but there is no api related error than also flow would not enter here.
175  elseif ($response->message == 'error')
176  {
177  $content = Zurmo::t('EmailMessagesModule', 'Response from Server') . "\n";
178  foreach ($response->errors as $error)
179  {
180  $content .= $error;
181  }
182  $emailMessageSendError = new EmailMessageSendError();
183  $data = array();
184  $data['message'] = $content;
185  $emailMessageSendError->serializedData = serialize($data);
186  $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox,
187  EmailFolder::TYPE_OUTBOX_ERROR);
188  $emailMessage->error = $emailMessageSendError;
189  }
190  if (count($attachmentsData) > 0)
191  {
192  foreach ($attachmentsData as $path)
193  {
194  unlink($path);
195  }
196  }
197  $saved = $emailMessage->save(false);
198  if (!$saved)
199  {
200  throw new FailedToSaveModelException();
201  }
202  }
203 
208  {
209  $emailMessage = $this->emailMessage;
210  list($toAddresses, $ccAddresses, $bccAddresses) = SendGridEmailHelper::resolveRecipientAddressesByType($emailMessage);
211  $this->toAddresses = $toAddresses;
212  $this->ccAddresses = $ccAddresses;
213  $this->bccAddresses = $bccAddresses;
214  }
215 
220  public function setFromUser($userToSendMessagesFrom)
221  {
222  if (is_array($userToSendMessagesFrom))
223  {
224  $this->fromUserEmailData = $userToSendMessagesFrom;
225  }
226  elseif (is_object($userToSendMessagesFrom) && $userToSendMessagesFrom instanceof User)
227  {
228  $this->fromUser = $userToSendMessagesFrom;
229  }
230  }
231 
232  public function setToAddresses($toAddresses)
233  {
234  $this->toAddresses = $toAddresses;
235  }
236 
237  public function setCcAddresses($ccAddresses)
238  {
239  $this->ccAddresses = $ccAddresses;
240  }
241 
242  public function setBccAddresses($bccAddresses)
243  {
244  $this->bccAddresses = $bccAddresses;
245  }
246 
247  public function getEmailAccount()
248  {
249  return $this->emailAccount;
250  }
251 
252  public function getEmailMessage()
253  {
254  return $this->emailMessage;
255  }
256  }
257 ?>
__construct(EmailMessage $emailMessage, $emailAccount)
setFromUser($userToSendMessagesFrom)
Definition: User.php:37
static decrypt($value, $salt=ZURMO_PASSWORD_SALT)
static resolveRecipientAddressesByType($emailMessage)
static getCampaignOrAutoresponderDataByEmailMessage(EmailMessage $emailMessage)
Generated on Fri Jul 3 2020 07:10:35
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.