diff options
author | Lukas Reschke <lukas@owncloud.com> | 2015-02-12 13:53:27 +0100 |
---|---|---|
committer | Lukas Reschke <lukas@owncloud.com> | 2015-03-16 12:47:05 +0100 |
commit | 13486a5ada62e4355473ca01e07371c162951e84 (patch) | |
tree | c079e9c53691ca14e5e1898c40a93322d6e727f5 /lib/public | |
parent | 0d9f149dd93997085b85e2b174f5989a1b996263 (diff) | |
download | nextcloud-server-13486a5ada62e4355473ca01e07371c162951e84.tar.gz nextcloud-server-13486a5ada62e4355473ca01e07371c162951e84.zip |
Migrate to SwiftMail
Replaces the OC_Mail and phpmailer with SwiftMail allowing us to mock it properly.
Fixes the unit test execution on master on local machines and https://github.com/owncloud/core/issues/12014
Conflicts:
3rdparty
lib/private/server.php
lib/public/iservercontainer.php
tests/lib/mail.php
tests/settings/controller/mailsettingscontrollertest.php
Conflicts:
3rdparty
lib/private/mail.php
lib/private/server.php
lib/public/iservercontainer.php
settings/ajax/lostpassword.php
settings/application.php
Diffstat (limited to 'lib/public')
-rw-r--r-- | lib/public/iservercontainer.php | 8 | ||||
-rw-r--r-- | lib/public/mail/imailer.php | 49 | ||||
-rw-r--r-- | lib/public/mail/util.php | 26 | ||||
-rw-r--r-- | lib/public/util.php | 27 |
4 files changed, 105 insertions, 5 deletions
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index ec23cc52345..20345e61212 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -318,9 +318,15 @@ interface IServerContainer { * @return \bantu\IniGetWrapper\IniGetWrapper */ function getIniWrapper(); - /** * @return \OCP\Command\IBus */ function getCommandBus(); + + /** + * Creates a new mailer + * + * @return \OCP\Mail\IMailer + */ + function getMailer(); } diff --git a/lib/public/mail/imailer.php b/lib/public/mail/imailer.php new file mode 100644 index 00000000000..2a20ead612b --- /dev/null +++ b/lib/public/mail/imailer.php @@ -0,0 +1,49 @@ +<?php +/** + * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Mail; +use OC\Mail\Message; + +/** + * Class IMailer provides some basic functions to create a mail message that can be used in combination with + * \OC\Mail\Message. + * + * Example usage: + * + * $mailer = \OC::$server->getMailer(); + * $message = $mailer->createMessage(); + * $message->setSubject('Your Subject'); + * $message->setFrom(array('cloud@domain.org' => 'ownCloud Notifier'); + * $message->setTo(array('recipient@domain.org' => 'Recipient'); + * $message->setBody('The message text'); + * $mailer->send($message); + * + * This message can then be passed to send() of \OC\Mail\Mailer + * + * @package OCP\Mail + */ +interface IMailer { + /** + * Creates a new message object that can be passed to send() + * + * @return Message + */ + public function createMessage(); + + /** + * Send the specified message. Also sets the from address to the value defined in config.php + * if no-one has been passed. + * + * @param Message $message Message to send + * @return string[] Array with failed recipients. Be aware that this depends on the used mail backend and + * therefore should be considered + * @throws \Exception In case it was not possible to send the message. (for example if an invalid mail address + * has been supplied.) + */ + public function send(Message $message); +} diff --git a/lib/public/mail/util.php b/lib/public/mail/util.php new file mode 100644 index 00000000000..ea59649e620 --- /dev/null +++ b/lib/public/mail/util.php @@ -0,0 +1,26 @@ +<?php +/** + * Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OCP\Mail; + +/** + * Class Util provides some helpers for mail addresses + * + * @package OCP\Mail + */ +class Util { + /** + * Checks if an e-mail address is valid + * + * @param string $email Email address to be validated + * @return bool True if the mail address is valid, false otherwise + */ + public static function validateMailAddress($email) { + return \OC\Mail\Util::validateMailAddress($email); + } +} diff --git a/lib/public/util.php b/lib/public/util.php index aa6cd5ba012..71ab8cabce5 100644 --- a/lib/public/util.php +++ b/lib/public/util.php @@ -63,12 +63,31 @@ class Util { * @param string $ccaddress * @param string $ccname * @param string $bcc + * @deprecated Use \OCP\Mail\IMailer instead */ public static function sendMail( $toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname, $html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') { - // call the internal mail class - \OC_MAIL::send($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname, - $html, $altbody, $ccaddress, $ccname, $bcc); + $mailer = \OC::$server->getMailer(); + $message = $mailer->createMessage(); + $message->setTo(array($toaddress => $toname)); + $message->setSubject($subject); + $message->setPlainBody($mailtext); + $message->setFrom(array($fromaddress => $fromname)); + if($html === 1) { + $message->setHTMLBody($altbody); + } + if(!empty($ccaddress)) { + if(!empty($ccname)) { + $message->setCc(array($ccaddress => $ccname)); + } else { + $message->setCc(array($ccaddress)); + } + } + if(!empty($bcc)) { + $message->setBcc(array($bcc)); + } + + $mailer->send($message); } /** @@ -275,7 +294,7 @@ class Util { $host_name = \OC_Config::getValue('mail_domain', $host_name); $defaultEmailAddress = $user_part.'@'.$host_name; - if (\OC_Mail::validateAddress($defaultEmailAddress)) { + if (\OCP\Mail\Util::validateMailAddress($defaultEmailAddress)) { return $defaultEmailAddress; } |