Exemple #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';
     }
 }
Exemple #2
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);
 }
Exemple #4
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];
     }
 }
Exemple #7
0
 /**
  * @NoAdminRequired
  *
  * @param string $username
  * @param string $password
  * @param array $groups
  * @param string $email
  * @return DataResponse
  */
 public function create($username, $password, array $groups = array(), $email = '')
 {
     if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
         return new DataResponse(array('message' => (string) $this->l10n->t('Invalid mail address')), Http::STATUS_UNPROCESSABLE_ENTITY);
     }
     $currentUser = $this->userSession->getUser();
     if (!$this->isAdmin) {
         if (!empty($groups)) {
             foreach ($groups as $key => $group) {
                 $groupObject = $this->groupManager->get($group);
                 if ($groupObject === null) {
                     unset($groups[$key]);
                     continue;
                 }
                 if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
                     unset($groups[$key]);
                 }
             }
         }
         if (empty($groups)) {
             $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($currentUser);
             // New class returns IGroup[] so convert back
             $gids = [];
             foreach ($groups as $group) {
                 $gids[] = $group->getGID();
             }
             $groups = $gids;
         }
     }
     if ($this->userManager->userExists($username)) {
         return new DataResponse(array('message' => (string) $this->l10n->t('A user with that name already exists.')), Http::STATUS_CONFLICT);
     }
     try {
         $user = $this->userManager->createUser($username, $password);
     } catch (\Exception $exception) {
         return new DataResponse(array('message' => (string) $this->l10n->t('Unable to create user.')), Http::STATUS_FORBIDDEN);
     }
     if ($user instanceof User) {
         if ($groups !== null) {
             foreach ($groups as $groupName) {
                 $group = $this->groupManager->get($groupName);
                 if (empty($group)) {
                     $group = $this->groupManager->createGroup($groupName);
                 }
                 $group->addUser($user);
             }
         }
         /**
          * Send new user mail only if a mail is set
          */
         if ($email !== '') {
             $this->config->setUserValue($username, 'settings', 'email', $email);
             // data for the mail template
             $mailData = array('username' => $username, 'url' => $this->urlGenerator->getAbsoluteURL('/'));
             $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank');
             $mailContent = $mail->render();
             $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank');
             $plainTextMailContent = $mail->render();
             $subject = $this->l10n->t('Your %s account was created', [$this->defaults->getName()]);
             try {
                 $message = $this->mailer->createMessage();
                 $message->setTo([$email => $username]);
                 $message->setSubject($subject);
                 $message->setHtmlBody($mailContent);
                 $message->setPlainBody($plainTextMailContent);
                 $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]);
                 $this->mailer->send($message);
             } catch (\Exception $e) {
                 $this->log->error("Can't send new user mail to {$email}: " . $e->getMessage(), array('app' => 'settings'));
             }
         }
         // fetch users groups
         $userGroups = $this->groupManager->getUserGroupIds($user);
         return new DataResponse($this->formatUserForIndex($user, $userGroups), Http::STATUS_CREATED);
     }
     return new DataResponse(array('message' => (string) $this->l10n->t('Unable to create user.')), Http::STATUS_FORBIDDEN);
 }