Update# use \OCP\Mail to send multipart email, containing both html and

plaintext
This commit is contained in:
Pellaeon Lin 2015-11-14 12:34:42 +08:00
parent 72da241dc9
commit 6865cfd9d0
7 changed files with 47 additions and 59 deletions

View File

@ -33,7 +33,7 @@ class Application extends App {
return new RegisterController(
$c->query('AppName'),
$c->query('Request'),
$c->query('Mail'),
$c->query('Mailer'),
$c->query('L10N'),
$c->query('URLGenerator'),
$c->query('PendingRegist'),
@ -73,8 +73,8 @@ class Application extends App {
return $c->query('ServerContainer')->getConfig();
});
$container->registerService('Mail', function($c) {
return new Wrapper\Mail;
$container->registerService('Mailer', function($c) {
return $c->query('ServerContainer')->getMailer();
});
$container->registerService('L10N', function($c) {

View File

@ -6,5 +6,5 @@
<licence>AGPL</licence>
<author>Pellaeon Lin</author>
<version>0.0.7</version>
<requiremin>8.0.0</requiremin>
<requiremin>8.1.0</requiremin>
</info>

View File

@ -21,10 +21,11 @@ use \OCP\IUserManager;
use \OCP\IGroupManager;
use \OCP\IL10N;
use \OCP\IConfig;
use \OCP\Mail\IMailer;
class RegisterController extends Controller {
private $mail;
private $mailer;
private $l10n;
private $urlgenerator;
private $pendingreg;
@ -33,9 +34,9 @@ class RegisterController extends Controller {
private $groupmanager;
protected $appName;
public function __construct($appName, IRequest $request, Wrapper\Mail $mail, IL10N $l10n, $urlgenerator,
public function __construct($appName, IRequest $request, IMailer $mailer, IL10N $l10n, $urlgenerator,
$pendingreg, IUserManager $usermanager, IConfig $config, IGroupManager $groupmanager){
$this->mail = $mail;
$this->mailer = $mailer;
$this->l10n = $l10n;
$this->urlgenerator = $urlgenerator;
$this->pendingreg = $pendingreg;
@ -63,6 +64,7 @@ class RegisterController extends Controller {
*/
public function validateEmail() {
$email = $this->request->getParam('email');
// TODO use Mailer::validateMailAddress
if ( !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
return new TemplateResponse('', 'error', array(
'errors' => array(array(
@ -75,13 +77,9 @@ class RegisterController extends Controller {
if ( $this->pendingreg->find($email) ) {
$this->pendingreg->delete($email);
$token = $this->pendingreg->save($email);
$link = $this->urlgenerator->linkToRoute('registration.register.verifyToken', array('token' => $token));
$link = $this->urlgenerator->getAbsoluteURL($link);
$from = Util::getDefaultEmailAddress('register');
$res = new TemplateResponse('registration', 'email', array('link' => $link), 'blank');
$msg = $res->render();
try {
$this->mail->sendMail($email, 'ownCloud User', $this->l10n->t('Verify your ownCloud registration request'), $msg, $from, 'ownCloud');
$this->sendValidationEmail($token, $email);
} catch (\Exception $e) {
return new TemplateResponse('', 'error', array(
'errors' => array(array(
@ -131,14 +129,8 @@ class RegisterController extends Controller {
}
$token = $this->pendingreg->save($email);
//TODO: check for error
$link = $this->urlgenerator->linkToRoute('registration.register.verifyToken', array('token' => $token));
$link = $this->urlgenerator->getAbsoluteURL($link);
$from = Util::getDefaultEmailAddress('register');
$res = new TemplateResponse('registration', 'email', array('link' => $link), 'blank');
$msg = $res->render();
try {
$this->mail->sendMail($email, 'ownCloud User', $this->l10n->t('Verify your ownCloud registration request'), $msg, $from, 'ownCloud');
$this->sendValidationEmail($token, $email);
} catch (\Exception $e) {
return new TemplateResponse('', 'error', array(
'errors' => array(array(
@ -248,4 +240,32 @@ class RegisterController extends Controller {
), 'guest');
}
}
/**
* Sends validation email
* @param string $token
* @param string $to
* @return null
* @throws \Exception
*/
private function sendValidationEmail(string $token, string $to) {
$link = $this->urlgenerator->linkToRoute('registration.register.verifyToken', array('token' => $token));
$link = $this->urlgenerator->getAbsoluteURL($link);
$html_template = new TemplateResponse('registration', 'email_html', array('link' => $link), 'blank');
$html_part = $html_template->render();
$plaintext_template = new TemplateResponse('registration', 'email_plaintext', array('link' => $link), 'blank');
$plaintext_part = $plaintext_template->render();
$subject = $this->l10n->t('Verify your ownCloud registration request');
$from = Util::getDefaultEmailAddress('register');
$message = $this->mailer->createMessage();
$message->setFrom([$from]);
$message->setTo([$to]);
$message->setSubject($subject);
$message->setPlainBody($plaintext_part);
$message->setHtmlBody($html_part);
$failed_recipients = $this->mailer->send($message);
if ( !empty($failed_recipients) )
throw new \Exception('Failed recipients: '.print_r($failed_recipients, true));
}
}

View File

@ -1,3 +0,0 @@
<?php
echo str_replace('{link}', $_['link'], $l->t('To create a new account on ownCloud, just click the following link:<br/><br/>
<a href="{link}">{link}</a>'));

4
templates/email_html.php Normal file
View File

@ -0,0 +1,4 @@
<?php
// TODO use OC_Default to get site name
echo $l->t('To create a new account on ownCloud, just click the following link:');
echo str_replace('{link}', $_['link'], '<br/><br/><a href="{link}">{link}</a>');

View File

@ -0,0 +1,3 @@
<?php
echo $l->t("To create a new account on ownCloud, just click the following link:");
echo str_replace('{link}', $_['link'], "\n\n{link}");

View File

@ -1,36 +0,0 @@
<?php
/**
* ownCloud
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Pellaeon Lin <pellaeon@cnmc.tw>
* @copyright Pellaeon Lin, 2014
*/
namespace OCA\Registration\Wrapper;
class Mail {
/**
* send an email
* @param string $toaddress
* @param string $toname
* @param string $subject
* @param string $mailtext
* @param string $fromaddress
* @param string $fromname
* @param int $html
* @param string $altbody
* @param string $ccaddress
* @param string $ccname
* @param string $bcc
*/
public function sendMail( $toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
$html = 0, $altbody = '', $ccaddress = '', $ccname = '', $bcc = '') {
// call the internal mail class
\OCP\Util::sendMail($toaddress, $toname, $subject, $mailtext, $fromaddress, $fromname,
$html, $altbody, $ccaddress, $ccname, $bcc);
}
}