Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ZurmoSwiftMailer.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 
37  Yii::import('ext.swiftmailer.SwiftMailer');
38 
42  class ZurmoSwiftMailer extends SwiftMailer
43  {
48  public $headers = array();
49 
54  public $parts = array();
55 
60  protected $sendResponseLog = array();
61 
62  protected $emailAccount;
63 
64  protected $emailMessage;
65 
70  public function smtpTransport($host = null, $port = null, $security = null)
71  {
72  return ZurmoSwiftSmtpTransport::newInstance($host, $port, $security);
73  }
74 
78  public function getSendResponseLog()
79  {
80  return $this->sendResponseLog;
81  }
82 
88  public function send()
89  {
90  $transport = $this->loadTransport();
91  $mailer = Swift_Mailer::newInstance($transport);
92  $message = Swift_Message::newInstance($this->Subject);
93  $message->setFrom($this->From);
94  if (!empty($this->toAddressesAndNames))
95  {
96  foreach ($this->toAddressesAndNames as $address => $name)
97  {
98  try
99  {
100  $message->addTo($address, $name);
101  }
102  catch (Swift_RfcComplianceException $e)
103  {
104  throw new OutboundEmailSendException($e->getMessage(), $e->getCode(), $e);
105  }
106  }
107  }
108 
109  if (!empty($this->ccAddressesAndNames))
110  {
111  foreach ($this->ccAddressesAndNames as $address => $name)
112  {
113  try
114  {
115  $message->addCc($address, $name);
116  }
117  catch (Swift_RfcComplianceException $e)
118  {
119  throw new OutboundEmailSendException($e->getMessage(), $e->getCode(), $e);
120  }
121  }
122  }
123 
124  if (!empty($this->bccAddressesAndNames))
125  {
126  foreach ($this->bccAddressesAndNames as $address => $name)
127  {
128  try
129  {
130  $message->addBcc($address, $name);
131  }
132  catch (Swift_RfcComplianceException $e)
133  {
134  throw new OutboundEmailSendException($e->getMessage(), $e->getCode(), $e);
135  }
136  }
137  }
138 
139  if (!empty($this->headers))
140  {
141  $headersObject = $message->getHeaders();
142  foreach ($this->headers as $headerKey => $headerValue)
143  {
144  $method = "addTextHeader";
145  if ($headerKey == "Return-Path")
146  {
147  // this is a special header type unlike text headers.
148  $method = "addPathHeader";
149  }
150  $headersObject->$method($headerKey, $headerValue);
151  }
152  }
153 
154  if (!empty($this->attachments))
155  {
156  foreach ($this->attachments as $attachment)
157  {
158  $message->attach($attachment);
159  }
160  }
161 
162  if ($this->body)
163  {
164  $message->addPart($this->body, 'text/html');
165  }
166  if ($this->altBody)
167  {
168  $message->setBody($this->altBody);
169  }
170  if (!empty($this->parts))
171  {
172  foreach ($this->parts as $part)
173  {
174  $message->addPart($part[0], $part[1], $part[2]);
175  }
176  }
177  try
178  {
179  $result = $mailer->send($message);
180  $this->sendResponseLog = $transport->getResponseLog();
181  }
182  catch (Swift_SwiftException $e)
183  {
184  $this->sendResponseLog = $transport->getResponseLog();
185  throw new OutboundEmailSendException($e->getMessage(), $e->getCode(), $e);
186  }
187  $this->clearAddresses();
188  return $result;
189  }
190 
196  public function __construct(EmailMessage $emailMessage, $emailAccount)
197  {
198  parent::init();
199  $this->emailAccount = $emailAccount;
200  $this->emailMessage = $emailMessage;
201  $this->populateSettings();
202  $this->populateMessage();
203  }
204 
210  protected function populateSettings()
211  {
212  if ($this->emailAccount != null && $this->emailAccount->outboundHost != null &&
213  $this->emailAccount->outboundPort != null &&
214  $this->emailAccount->outboundUsername != null &&
215  $this->emailAccount->outboundPassword != null)
216  {
217  $this->host = $this->emailAccount->outboundHost;
218  $this->port = $this->emailAccount->outboundPort;
219  $this->username = $this->emailAccount->outboundUsername;
220  $this->password = ZurmoPasswordSecurityUtil::decrypt($this->emailAccount->outboundPassword);
221  $this->security = $this->emailAccount->outboundSecurity;
222  }
223  else
224  {
225  $this->mailer = Yii::app()->emailHelper->outboundType;
226  $this->host = Yii::app()->emailHelper->outboundHost;
227  $this->port = Yii::app()->emailHelper->outboundPort;
228  $this->username = Yii::app()->emailHelper->outboundUsername;
229  $this->password = Yii::app()->emailHelper->outboundPassword;
230  $this->security = Yii::app()->emailHelper->outboundSecurity;
231  }
232  }
233 
238  public function populateMessage()
239  {
240  $emailMessage = $this->emailMessage;
241  $this->Subject = $emailMessage->subject;
242  $this->headers = unserialize($emailMessage->headers);
243  if (!empty($emailMessage->content->textContent))
244  {
245  $this->altBody = $emailMessage->content->textContent;
246  }
247  if (!empty($emailMessage->content->htmlContent))
248  {
249  $this->body = ZurmoCssInlineConverterUtil::convertAndPrettifyEmailByHtmlContent(
250  $emailMessage->content->htmlContent,
251  Yii::app()->emailHelper->htmlConverter);
252  }
253  $this->From = array($emailMessage->sender->fromAddress => $emailMessage->sender->fromName);
254  foreach ($emailMessage->recipients as $recipient)
255  {
256  $this->addAddressByType($recipient->toAddress, $recipient->toName, $recipient->type);
257  }
258 
259  if (isset($emailMessage->files) && !empty($emailMessage->files))
260  {
261  foreach ($emailMessage->files as $file)
262  {
263  $this->attachDynamicContent($file->fileContent->content, $file->name, $file->type);
264  //$emailMessage->attach($attachment);
265  }
266  }
267  }
268 
274  public function sendEmail()
275  {
276  $emailMessage = $this->emailMessage;
277  try
278  {
279  $emailMessage->sendAttempts = $emailMessage->sendAttempts + 1;
280  $acceptedRecipients = $this->send();
281  // Code below is quick fix, we need to think about better solution
282  // Here is related PT story: https://www.pivotaltracker.com/projects/380027#!/stories/45841753
283  if ($acceptedRecipients != $emailMessage->recipients->count() && $acceptedRecipients <= 0)
284  {
285  $content = Zurmo::t('EmailMessagesModule', 'Response from Server') . "\n";
286  foreach ($this->getSendResponseLog() as $logMessage)
287  {
288  $content .= $logMessage . "\n";
289  }
290  $emailMessageSendError = new EmailMessageSendError();
291  $data = array();
292  $data['message'] = $content;
293  $emailMessageSendError->serializedData = serialize($data);
294  $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox,
295  EmailFolder::TYPE_OUTBOX_ERROR);
296  $emailMessage->error = $emailMessageSendError;
297  }
298  else
299  {
300  $emailMessage->error = null;
301  $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox, EmailFolder::TYPE_SENT);
302  $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
303  }
304  }
305  catch (OutboundEmailSendException $e)
306  {
307  $emailMessageSendError = new EmailMessageSendError();
308  $data = array();
309  $data['code'] = $e->getCode();
310  $data['message'] = $e->getMessage();
311  //$data['trace'] = $e->getPrevious();
312  $emailMessageSendError->serializedData = serialize($data);
313  $emailMessage->folder = EmailFolder::getByBoxAndType($emailMessage->folder->emailBox, EmailFolder::TYPE_OUTBOX_ERROR);
314  $emailMessage->error = $emailMessageSendError;
315  }
316  $this->updateEmailMessageForSending($emailMessage, false);
317  }
318 
323  protected function updateEmailMessageForSending(EmailMessage $emailMessage, $useSQL = false)
324  {
325  if (!$useSQL)
326  {
327  Yii::log("EmailMessage should have been saved by this point. Anyways, saving now", CLogger::LEVEL_INFO);
328  // we save it and return. No need to call SP as the message is saved already;
329  $emailMessage->save(false);
330  return;
331  }
332  $nowTimestamp = "'" . DateTimeUtil::convertTimestampToDbFormatDateTime(time()) . "'";
333  $sendAttempts = ($emailMessage->sendAttempts)? $emailMessage->sendAttempts : 1;
334  $sentDateTime = ($emailMessage->sentDateTime)? "'" . $emailMessage->sentDateTime . "'" : 'null';
335  $serializedData = ($emailMessage->error->serializedData)?
336  "'" . $emailMessage->error->serializedData . "'" : 'null';
337  $sql = '`update_email_message_for_sending`(
338  ' . $emailMessage->id . ',
339  ' . $sendAttempts . ',
340  ' . $sentDateTime . ',
341  ' . $emailMessage->folder->id . ',
342  ' . $serializedData . ',
343  ' . $nowTimestamp .')';
345  $emailMessage->forget();
346  }
347 
353  public function sendTestEmail($isUser = false)
354  {
355  $this->emailMessage->mailerType = 'smtp';
356  if ($isUser)
357  {
358  $this->emailMessage->mailerSettings = 'personal';
359  }
360  if ($this->emailMessage->validate())
361  {
362  $this->emailMessage->save();
363  $this->sendEmail();
364  }
365  return $this->emailMessage;
366  }
367 
368  public function getEmailAccount()
369  {
370  return $this->emailAccount;
371  }
372 
373  public function getEmailMessage()
374  {
375  return $this->emailMessage;
376  }
377  }
378 ?>
updateEmailMessageForSending(EmailMessage $emailMessage, $useSQL=false)
static decrypt($value, $salt=ZURMO_PASSWORD_SALT)
__construct(EmailMessage $emailMessage, $emailAccount)
smtpTransport($host=null, $port=null, $security=null)
sendTestEmail($isUser=false)
Generated on Wed Jul 8 2020 07:10:30
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.