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)); }
/** * 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]; } }
/** * Get the sender data * @param string $setting Either `email` or `name` * @return string */ protected function getSenderData($setting) { if (empty($this->senderAddress)) { $this->senderAddress = Util::getDefaultEmailAddress('no-reply'); } if (empty($this->senderName)) { $defaults = new Defaults(); $this->senderName = $defaults->getName(); } if ($setting === 'email') { return $this->senderAddress; } return $this->senderName; }