Example #1
0
 /**
  * Event handler for the 'schedule' event.
  *
  * @param ITip\Message $iTipMessage
  * @return void
  */
 function schedule(ITip\Message $iTipMessage)
 {
     // Not sending any emails if the system considers the update
     // insignificant.
     if (!$iTipMessage->significantChange) {
         if (!$iTipMessage->scheduleStatus) {
             $iTipMessage->scheduleStatus = '1.0;We got the message, but it\'s not significant enough to warrant an email';
         }
         return;
     }
     $summary = $iTipMessage->message->VEVENT->SUMMARY;
     if (parse_url($iTipMessage->sender, PHP_URL_SCHEME) !== 'mailto') {
         return;
     }
     if (parse_url($iTipMessage->recipient, PHP_URL_SCHEME) !== 'mailto') {
         return;
     }
     $sender = substr($iTipMessage->sender, 7);
     $recipient = substr($iTipMessage->recipient, 7);
     $senderName = $iTipMessage->senderName ? $iTipMessage->senderName : null;
     $recipientName = $iTipMessage->recipientName ? $iTipMessage->recipientName : null;
     $subject = 'SabreDAV iTIP message';
     switch (strtoupper($iTipMessage->method)) {
         case 'REPLY':
             $subject = 'Re: ' . $summary;
             break;
         case 'REQUEST':
             $subject = $summary;
             break;
         case 'CANCEL':
             $subject = 'Cancelled: ' . $summary;
             break;
     }
     $contentType = 'text/calendar; charset=UTF-8; method=' . $iTipMessage->method;
     $message = $this->mailer->createMessage();
     $message->setReplyTo([$sender => $senderName])->setTo([$recipient => $recipientName])->setSubject($subject)->setBody($iTipMessage->message->serialize(), $contentType);
     try {
         $failed = $this->mailer->send($message);
         if ($failed) {
             $this->logger->error('Unable to deliver message to {failed}', ['app' => 'dav', 'failed' => implode(', ', $failed)]);
             $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
         }
         $iTipMessage->scheduleStatus = '1.1; Scheduling message is sent via iMip';
     } catch (\Exception $ex) {
         $this->logger->logException($ex, ['app' => 'dav']);
         $iTipMessage->scheduleStatus = '5.0; EMail delivery failed';
     }
 }
Example #2
0
 /**
  * Set the mail address of a user
  *
  * @NoAdminRequired
  * @NoSubadminRequired
  *
  * @param string $id
  * @param string $mailAddress
  * @return DataResponse
  */
 public function setMailAddress($id, $mailAddress)
 {
     $userId = $this->userSession->getUser()->getUID();
     $user = $this->userManager->get($id);
     if ($userId !== $id && !$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Forbidden'))), Http::STATUS_FORBIDDEN);
     }
     if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Invalid mail address'))), Http::STATUS_UNPROCESSABLE_ENTITY);
     }
     if (!$user) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Invalid user'))), Http::STATUS_UNPROCESSABLE_ENTITY);
     }
     // this is the only permission a backend provides and is also used
     // for the permission of setting a email address
     if (!$user->canChangeDisplayName()) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Unable to change mail address'))), Http::STATUS_FORBIDDEN);
     }
     // delete user value if email address is empty
     if ($mailAddress === '') {
         $this->config->deleteUserValue($id, 'settings', 'email');
     } else {
         $this->config->setUserValue($id, 'settings', 'email', $mailAddress);
     }
     return new DataResponse(array('status' => 'success', 'data' => array('username' => $id, 'mailAddress' => $mailAddress, 'message' => (string) $this->l10n->t('Email saved'))), Http::STATUS_OK);
 }
Example #3
0
 /**
  * @param string $user
  * @throws \Exception
  */
 protected function sendEmail($user)
 {
     if (!$this->userManager->userExists($user)) {
         throw new \Exception($this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.'));
     }
     $email = $this->config->getUserValue($user, 'settings', 'email');
     if (empty($email)) {
         throw new \Exception($this->l10n->t('Couldn\'t send reset email because there is no ' . 'email address for this username. Please ' . 'contact your administrator.'));
     }
     $token = $this->secureRandom->getMediumStrengthGenerator()->generate(21, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER);
     $this->config->setUserValue($user, 'owncloud', 'lostpassword', $token);
     $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user, 'token' => $token));
     $tmpl = new \OC_Template('core/lostpassword', 'email');
     $tmpl->assign('link', $link, false);
     $msg = $tmpl->fetchPage();
     try {
         $message = $this->mailer->createMessage();
         $message->setTo([$email => $user]);
         $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()]));
         $message->setPlainBody($msg);
         $message->setFrom([$this->from => $this->defaults->getName()]);
         $this->mailer->send($message);
     } catch (\Exception $e) {
         throw new \Exception($this->l10n->t('Couldn\'t send reset email. Please contact your administrator.'));
     }
 }
 /**
  * Send a notification to one user
  *
  * @param string $userName Username of the recipient
  * @param string $email Email address of the recipient
  * @param string $lang Selected language of the recipient
  * @param string $timezone Selected timezone of the recipient
  * @param int $maxTime
  */
 public function sendEmailToUser($userName, $email, $lang, $timezone, $maxTime)
 {
     $user = $this->userManager->get($userName);
     if (!$user instanceof IUser) {
         return;
     }
     list($mailData, $skippedCount) = $this->getItemsForUser($userName, $maxTime);
     $l = $this->getLanguage($lang);
     $this->dataHelper->setUser($userName);
     $this->dataHelper->setL10n($l);
     $activityList = array();
     foreach ($mailData as $activity) {
         $relativeDateTime = $this->dateFormatter->formatDateTimeRelativeDay($activity['amq_timestamp'], 'long', 'medium', new \DateTimeZone($timezone), $l);
         $activityList[] = array($this->dataHelper->translation($activity['amq_appid'], $activity['amq_subject'], $this->dataHelper->getParameters($activity['amq_subjectparams'])), $relativeDateTime);
     }
     $alttext = new Template('activity', 'email.notification', '');
     $alttext->assign('username', $user->getDisplayName());
     $alttext->assign('activities', $activityList);
     $alttext->assign('skippedCount', $skippedCount);
     $alttext->assign('owncloud_installation', $this->urlGenerator->getAbsoluteURL('/'));
     $alttext->assign('overwriteL10N', $l);
     $emailText = $alttext->fetchPage();
     $message = $this->mailer->createMessage();
     $message->setTo([$email => $user->getDisplayName()]);
     $message->setSubject((string) $l->t('Activity notification'));
     $message->setPlainBody($emailText);
     $message->setFrom([$this->getSenderData('email') => $this->getSenderData('name')]);
     $this->mailer->send($message);
 }
 public function testSendLinkShareMailException()
 {
     $message = $this->getMockBuilder('\\OC\\Mail\\Message')->disableOriginalConstructor()->getMock();
     $message->expects($this->once())->method('setSubject')->with('TestUser shared »MyFile« with you');
     $message->expects($this->once())->method('setTo')->with(['*****@*****.**']);
     $message->expects($this->once())->method('setHtmlBody');
     $message->expects($this->once())->method('setPlainBody');
     $message->expects($this->once())->method('setFrom')->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
     $this->mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
     $this->mailer->expects($this->once())->method('send')->with($message)->will($this->throwException(new Exception('Some Exception Message')));
     $this->defaults->expects($this->once())->method('getName')->will($this->returnValue('UnitTestCloud'));
     $this->config->expects($this->at(0))->method('getUserValue')->with('TestUser', 'settings', 'email', null)->will($this->returnValue('*****@*****.**'));
     $mailNotifications = new MailNotifications('TestUser', $this->config, $this->l10n, $this->mailer, $this->logger, $this->defaults);
     $this->assertSame(['*****@*****.**'], $mailNotifications->sendLinkShareMail('*****@*****.**', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
 }
Example #6
0
 /**
  * send encryption key passwords to the users by mail
  */
 protected function sendPasswordsByMail()
 {
     $noMail = [];
     $this->output->writeln('');
     $progress = new ProgressBar($this->output, count($this->userPasswords));
     $progress->start();
     foreach ($this->userPasswords as $uid => $password) {
         $progress->advance();
         if (!empty($password)) {
             $recipient = $this->userManager->get($uid);
             $recipientDisplayName = $recipient->getDisplayName();
             $to = $recipient->getEMailAddress();
             if ($to === '') {
                 $noMail[] = $uid;
                 continue;
             }
             $subject = (string) $this->l->t('one-time password for server-side-encryption');
             list($htmlBody, $textBody) = $this->createMailBody($password);
             // send it out now
             try {
                 $message = $this->mailer->createMessage();
                 $message->setSubject($subject);
                 $message->setTo([$to => $recipientDisplayName]);
                 $message->setHtmlBody($htmlBody);
                 $message->setPlainBody($textBody);
                 $message->setFrom([Util::getDefaultEmailAddress('admin-noreply')]);
                 $this->mailer->send($message);
             } catch (\Exception $e) {
                 $noMail[] = $uid;
             }
         }
     }
     $progress->finish();
     if (empty($noMail)) {
         $this->output->writeln("\n\nPassword successfully send to all users");
     } else {
         $table = new Table($this->output);
         $table->setHeaders(array('Username', 'Private key password'));
         $this->output->writeln("\n\nCould not send password to following users:\n");
         $rows = [];
         foreach ($noMail as $uid) {
             $rows[] = [$uid, $this->userPasswords[$uid]];
         }
         $table->setRows($rows);
         $table->render();
     }
 }
 /**
  * Send a mail to test the settings
  * @return array
  */
 public function sendTestMail()
 {
     $email = $this->config->getUserValue($this->userSession->getUser()->getUID(), $this->appName, 'email', '');
     if (!empty($email)) {
         try {
             $message = $this->mailer->createMessage();
             $message->setTo([$email => $this->userSession->getUser()->getDisplayName()]);
             $message->setFrom([$this->defaultMailAddress]);
             $message->setSubject($this->l10n->t('test email settings'));
             $message->setPlainBody('If you received this email, the settings seem to be correct.');
             $this->mailer->send($message);
         } catch (\Exception $e) {
             return ['data' => ['message' => (string) $this->l10n->t('A problem occurred while sending the email. Please revise your settings. (Error: %s)', [$e->getMessage()])], 'status' => 'error'];
         }
         return array('data' => array('message' => (string) $this->l10n->t('Email sent')), 'status' => 'success');
     }
     return array('data' => array('message' => (string) $this->l10n->t('You need to set your user email before being able to send test emails.')), 'status' => 'error');
 }
 /**
  * inform recipient about public link share
  *
  * @param string $recipient recipient email address
  * @param string $filename the shared file
  * @param string $link the public link
  * @param int $expiration expiration date (timestamp)
  * @return array $result of failed recipients
  */
 public function sendLinkShareMail($recipient, $filename, $link, $expiration)
 {
     $subject = (string) $this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
     list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
     try {
         $message = $this->mailer->createMessage();
         $message->setSubject($subject);
         $message->setTo([$recipient]);
         $message->setHtmlBody($htmlBody);
         $message->setPlainBody($textBody);
         $message->setFrom([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => (string) $this->l->t('%s via %s', [$this->senderDisplayName, $this->defaults->getName()])]);
         if (!is_null($this->replyTo)) {
             $message->setReplyTo([$this->replyTo]);
         }
         return $this->mailer->send($message);
     } catch (\Exception $e) {
         $this->logger->error("Can't send mail with public link to {$recipient}: " . $e->getMessage(), ['app' => 'sharing']);
         return [$recipient];
     }
 }
 public function testSendEmailToUser()
 {
     $maxTime = 200;
     $user = '******';
     $userDisplayName = 'user two';
     $email = $user . '@localhost';
     $this->mailer->expects($this->once())->method('send')->with($this->message);
     $this->message->expects($this->once())->method('setTo')->with([$email => $userDisplayName]);
     $this->message->expects($this->once())->method('setSubject');
     $this->message->expects($this->once())->method('setPlainBody');
     $this->message->expects($this->once())->method('setFrom');
     $userObject = $this->getMock('OCP\\IUser');
     $userObject->expects($this->any())->method('getDisplayName')->willReturn($userDisplayName);
     $this->userManager->expects($this->any())->method('get')->willReturnMap([[$user, $userObject], [$user . $user, null]]);
     $users = $this->mailQueueHandler->getAffectedUsers(1, $maxTime);
     $this->assertEquals([$user], $users);
     $this->mailQueueHandler->sendEmailToUser($user, $email, 'en', 'UTC', $maxTime);
     // Invalid user, no object no email
     $this->mailQueueHandler->sendEmailToUser($user . $user, $email, 'en', 'UTC', $maxTime);
 }