From 13486a5ada62e4355473ca01e07371c162951e84 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 12 Feb 2015 13:53:27 +0100 Subject: 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 --- lib/public/iservercontainer.php | 8 ++++++- lib/public/mail/imailer.php | 49 +++++++++++++++++++++++++++++++++++++++++ lib/public/mail/util.php | 26 ++++++++++++++++++++++ lib/public/util.php | 27 +++++++++++++++++++---- 4 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 lib/public/mail/imailer.php create mode 100644 lib/public/mail/util.php (limited to 'lib/public') 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 @@ + + * 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 @@ + + * 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; } -- cgit v1.2.3