protected function makeEmailMessage(Contact $contact, $subject = null)
 {
     $interval = mt_rand(4, 35) * 86400;
     if (!isset($subject)) {
         $subject = 'A test archived sent email';
     }
     //#1 Create Archived - Sent
     $emailMessage = new EmailMessage();
     $emailMessage->setScenario('importModel');
     $emailMessage->owner = $contact->owner;
     $emailMessage->subject = $subject;
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $emailMessage->content = $emailContent;
     //Sending is current user (super)
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Super User';
     $sender->personsOrAccounts->add(Yii::app()->user->userModel);
     $emailMessage->sender = $sender;
     //Recipient is BobMessage
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = strval($contact);
     $recipient->personsOrAccounts->add($contact);
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->recipients->add($recipient);
     $emailMessage->folder = EmailFolder::getByBoxAndType($this->emailBox, EmailFolder::TYPE_SENT);
     $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - $interval);
     $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - $interval + mt_rand(0, 3) * 86400);
     $emailMessage->addPermissions(Group::getByName(Group::EVERYONE_GROUP_NAME), Permission::READ_WRITE_CHANGE_PERMISSIONS_CHANGE_OWNER);
     $saved = $emailMessage->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $emailMessage = EmailMessage::getById($emailMessage->id);
     AllPermissionsOptimizationUtil::securableItemGivenPermissionsForGroup($emailMessage, Group::getByName(Group::EVERYONE_GROUP_NAME));
     $emailMessage->save();
     return $emailMessage;
 }
 private function createEmailMessage($contact, $emailMessageSentDateTime)
 {
     $emailBox = $this->emailBox;
     $emailMessage = new EmailMessage();
     $emailMessage->setScenario('importModel');
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Super User';
     $sender->personOrAccount = Yii::app()->user->userModel;
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = strval($contact);
     $recipient->personOrAccount = $contact;
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->owner = Yii::app()->user->userModel;
     $emailMessage->subject = 'A test archived sent email';
     $emailMessage->content = $emailContent;
     $emailMessage->sender = $sender;
     if (isset($emailMessageSentDateTime)) {
         $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(strtotime($emailMessageSentDateTime));
         $emailMessage->createdDateTime = $emailMessage->sentDateTime;
     }
     $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_ARCHIVED);
     $emailMessage->recipients->add($recipient);
     return $emailMessage;
 }
Ejemplo n.º 3
0
 /**
  * Special method to load a campaign with all types of campaignItemActivity
  */
 public function actionLoadCampaignWithAllItemActivityTypes()
 {
     if (!Group::isUserASuperAdministrator(Yii::app()->user->userModel)) {
         throw new NotSupportedException();
     }
     $emailBox = EmailBoxUtil::getDefaultEmailBoxByUser(Yii::app()->user->userModel);
     $marketingList = new MarketingList();
     $marketingList->name = 'Demo Marketing List';
     $marketingList->save();
     $campaign = new Campaign();
     $campaign->marketingList = $marketingList;
     $campaign->name = 'Campaign with all campaignItemActivity';
     $campaign->subject = 'Demo for all types of campaignItemActivities';
     $campaign->status = Campaign::STATUS_COMPLETED;
     $campaign->sendOnDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $campaign->supportsRichText = true;
     $campaign->htmlContent = 'Demo content';
     $campaign->fromName = 'Zurmo';
     $campaign->fromAddress = '*****@*****.**';
     $campaign->enableTracking = true;
     $saved = $campaign->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $contacts = Contact::getAll();
     //Awaiting queue
     $contact = $contacts[0];
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $campaignItem->unrestrictedSave();
     //Contact is not subscribed
     $contact = $contacts[1];
     $marketingList->addNewMember($contact->id, true);
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->type = CampaignItemActivity::TYPE_SKIP;
     $activity->save();
     $campaignItem->unrestrictedSave();
     //Skipped, both primary and secondary are opted out
     $contact = $contacts[2];
     $contact->primaryEmail->emailAddress = $contact->firstName . '*****@*****.**';
     $contact->primaryEmail->optOut = true;
     $contact->secondaryEmail->emailAddress = $contact->firstName . '*****@*****.**';
     $contact->secondaryEmail->optOut = true;
     $contact->save();
     $marketingList->addNewMember($contact->id);
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->type = CampaignItemActivity::TYPE_SKIP;
     $activity->save();
     $campaignItem->unrestrictedSave();
     //Skipped, primary is opted out but secondary is not
     $contact = $contacts[3];
     $contact->primaryEmail->emailAddress = $contact->firstName . '*****@*****.**';
     $contact->primaryEmail->optOut = true;
     $contact->secondaryEmail->emailAddress = $contact->firstName . '*****@*****.**';
     $contact->secondaryEmail->optOut = false;
     $contact->save();
     $marketingList->addNewMember($contact->id);
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->type = CampaignItemActivity::TYPE_SKIP;
     $activity->save();
     $campaignItem->unrestrictedSave();
     //Skipped, primary and secondary not set
     $contact = $contacts[4];
     $contact->primaryEmail = null;
     $contact->secondaryEmail = null;
     $contact->save();
     $marketingList->addNewMember($contact->id);
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->type = CampaignItemActivity::TYPE_SKIP;
     $activity->save();
     $campaignItem->unrestrictedSave();
     //Skipped, primary not set but secondary is set
     $contact = $contacts[5];
     $contact->primaryEmail = null;
     $contact->secondaryEmail->emailAddress = $contact->firstName . '@zurmo.org';
     $contact->secondaryEmail->optOut = false;
     $contact->save();
     $marketingList->addNewMember($contact->id);
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->type = CampaignItemActivity::TYPE_SKIP;
     $activity->save();
     $campaignItem->unrestrictedSave();
     //Queued
     $contact = $contacts[6];
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->save();
     $emailMessage = new EmailMessage();
     $emailMessage->setScenario('importModel');
     $emailMessage->owner = $contact->owner;
     $emailMessage->subject = 'Subject';
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $emailMessage->content = $emailContent;
     //Sending is current user (super)
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Zurmo';
     $sender->personsOrAccounts->add(Yii::app()->user->userModel);
     $emailMessage->sender = $sender;
     //Recipient is BobMessage
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = strval($contact);
     $recipient->personsOrAccounts->add($contact);
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->recipients->add($recipient);
     $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_OUTBOX);
     $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $emailMessage->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $campaignItem->emailMessage = $emailMessage;
     $campaignItem->unrestrictedSave();
     //Queued with error
     $contact = $contacts[7];
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->save();
     $emailMessage = new EmailMessage();
     $emailMessage->setScenario('importModel');
     $emailMessage->owner = $contact->owner;
     $emailMessage->subject = 'Subject';
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $emailMessage->content = $emailContent;
     //Sending is current user (super)
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Zurmo';
     $sender->personsOrAccounts->add(Yii::app()->user->userModel);
     $emailMessage->sender = $sender;
     //Recipient is BobMessage
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = strval($contact);
     $recipient->personsOrAccounts->add($contact);
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->recipients->add($recipient);
     $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_OUTBOX_ERROR);
     $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $emailMessage->sendAttempts = 2;
     $emailMessageError = new EmailMessageSendError();
     $emailMessageError->serializedData = serialize(array('code' => '0001', 'message' => 'Error Message'));
     $emailMessage->error = $emailMessageError;
     $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $emailMessage->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $campaignItem->emailMessage = $emailMessage;
     $campaignItem->unrestrictedSave();
     //Failure
     $contact = $contacts[8];
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->quantity = 1;
     $activity->save();
     $emailMessage = new EmailMessage();
     $emailMessage->setScenario('importModel');
     $emailMessage->owner = $contact->owner;
     $emailMessage->subject = 'Subject';
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $emailMessage->content = $emailContent;
     //Sending is current user (super)
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Zurmo';
     $sender->personsOrAccounts->add(Yii::app()->user->userModel);
     $emailMessage->sender = $sender;
     //Recipient is BobMessage
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = strval($contact);
     $recipient->personsOrAccounts->add($contact);
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->recipients->add($recipient);
     $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_OUTBOX_FAILURE);
     $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $emailMessage->sendAttempts = 3;
     $emailMessageError = new EmailMessageSendError();
     $emailMessageError->serializedData = serialize(array('code' => '0001', 'message' => 'Error Message'));
     $emailMessage->error = $emailMessageError;
     $emailMessage->createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $emailMessage->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $campaignItem->emailMessage = $emailMessage;
     $campaignItem->unrestrictedSave();
     //Sent, open, click, bounce
     $contact = $contacts[9];
     $campaignItem = new CampaignItem();
     $campaignItem->processed = true;
     $campaignItem->campaign = $campaign;
     $campaignItem->contact = $contact;
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->type = CampaignItemActivity::TYPE_CLICK;
     $activity->quantity = rand(1, 50);
     $activity->latestDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + rand(100, 1000));
     $activity->latestSourceIP = '10.11.12.13';
     $activity->save();
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->type = CampaignItemActivity::TYPE_OPEN;
     $activity->quantity = rand(1, 50);
     $activity->latestDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + rand(100, 1000));
     $activity->latestSourceIP = '10.11.12.13';
     $activity->save();
     $activity = new CampaignItemActivity();
     $activity->person = $contact;
     $activity->campaignItem = $campaignItem;
     $activity->type = CampaignItemActivity::TYPE_BOUNCE;
     $activity->quantity = rand(1, 50);
     $activity->latestDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() + rand(100, 1000));
     $activity->latestSourceIP = '10.11.12.13';
     $activity->save();
     $emailMessage = new EmailMessage();
     $emailMessage->setScenario('importModel');
     $emailMessage->owner = $contact->owner;
     $emailMessage->subject = 'Subject';
     $emailContent = new EmailMessageContent();
     $emailContent->textContent = 'My First Message';
     $emailContent->htmlContent = 'Some fake HTML content';
     $emailMessage->content = $emailContent;
     //Sending is current user (super)
     $sender = new EmailMessageSender();
     $sender->fromAddress = '*****@*****.**';
     $sender->fromName = 'Zurmo';
     $sender->personsOrAccounts->add(Yii::app()->user->userModel);
     $emailMessage->sender = $sender;
     //Recipient is BobMessage
     $recipient = new EmailMessageRecipient();
     $recipient->toAddress = '*****@*****.**';
     $recipient->toName = strval($contact);
     $recipient->personsOrAccounts->add($contact);
     $recipient->type = EmailMessageRecipient::TYPE_TO;
     $emailMessage->recipients->add($recipient);
     $emailMessage->folder = EmailFolder::getByBoxAndType($emailBox, EmailFolder::TYPE_SENT);
     $emailMessage->sentDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time());
     $emailMessage->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $emailMessage->save();
     $campaignItem->emailMessage = $emailMessage;
     $campaignItem->unrestrictedSave();
 }