BryanDavis has uploaded a new change for review.
https://gerrit.wikimedia.org/r/98463
Change subject: Use PHPMailer to send mail
......................................................................
Use PHPMailer to send mail
Replace PHP's built-in mail() function with PHPMailer so that mail can
be sent via an SMTP relay rather than requiring sendmail on the
application server. The initial implementation assumes that an SMTP
relay will be available on localhost, but this is temporary until other
changesets that introduce better configuration management land in
master.
Bug: 57565
Change-Id: I58221cb7c9c42183ab48f81c4c861f2054be68a4
---
M src/Wikimania/Scholarship/App.php
M src/Wikimania/Scholarship/Controller.php
M src/Wikimania/Scholarship/Controllers/Admin/User.php
M src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
A src/Wikimania/Scholarship/Mailer.php
5 files changed, 148 insertions(+), 22 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/wikimedia/wikimania-scholarships
refs/changes/63/98463/1
diff --git a/src/Wikimania/Scholarship/App.php
b/src/Wikimania/Scholarship/App.php
index 22daafb..cb930af 100644
--- a/src/Wikimania/Scholarship/App.php
+++ b/src/Wikimania/Scholarship/App.php
@@ -54,6 +54,8 @@
'log.buffer' => 25,
'view' => new \Slim\Views\Twig(),
'view.cache' => "{$this->deployDir}/data/cache",
+ // FIXME: allow configuration as soon as I9529988a is
merged
+ 'smtp.host' => 'localhost',
'templates.path' => "{$this->deployDir}/data/templates",
'i18n.path' => "{$this->deployDir}/data/i18n",
));
@@ -138,6 +140,15 @@
return new \Wikimania\Scholarship\Forms\Apply( $dao );
});
+ $container->singleton( 'mailer', function ( $c ) {
+ return new \Wikimania\Scholarship\Mailer(
+ array(
+ 'Host' => $c->settings['smtp.host'],
+ ),
+ $c->log
+ );
+ });
+
// replace default logger with monolog
$container->singleton( 'log', function ( $c ) {
$log = new \Monolog\Logger( 'wikimania-scholarship' );
@@ -209,6 +220,7 @@
$slim->post( 'apply', function () use ( $slim ) {
$page = new Controllers\ScholarshipApplication(
$slim );
$page->setForm( $slim->applyForm );
+ $page->setMailer( $slim->mailer );
$page();
})->name( 'apply_post' );
@@ -386,6 +398,7 @@
$slim->post( 'user.post', function () use ( $slim ) {
$page = new Controllers\Admin\User( $slim );
$page->setDao( $slim->userDao );
+ $page->setMailer( $slim->mailer );
$page();
})->name( 'admin_user_post' );
diff --git a/src/Wikimania/Scholarship/Controller.php
b/src/Wikimania/Scholarship/Controller.php
index 30e88f6..5e0363e 100644
--- a/src/Wikimania/Scholarship/Controller.php
+++ b/src/Wikimania/Scholarship/Controller.php
@@ -46,8 +46,13 @@
*/
protected $form;
+ /**
+ * @var \Wikimania\Scholarship\Mailer $mailer
+ */
+ protected $mailer;
- public function __construct(\Slim\Slim $slim = null) {
+
+ public function __construct( \Slim\Slim $slim = null ) {
$this->slim = $slim ?: \Slim\Slim::getInstance();
$this->form = new Form();
}
@@ -63,6 +68,11 @@
}
+ public function setMailer( $mailer ) {
+ $this->mailer = $mailer;
+ }
+
+
protected function handle() {
$this->slim->pass();
}
diff --git a/src/Wikimania/Scholarship/Controllers/Admin/User.php
b/src/Wikimania/Scholarship/Controllers/Admin/User.php
index 41eafec..ba8923e 100644
--- a/src/Wikimania/Scholarship/Controllers/Admin/User.php
+++ b/src/Wikimania/Scholarship/Controllers/Admin/User.php
@@ -87,21 +87,20 @@
$this->flash( 'info', "User {$newId}
created." );
$id = $newId;
- //FIXME: using mail directly?
- mail( $user['email'],
+ $sent = $this->mailer->mail(
+ $user['email'],
$this->wgLang->message(
'new-account-subject' ),
- wordwrap(
- sprintf(
$this->wgLang->message( 'new-account-email' ),
+ sprintf(
+ $this->wgLang->message(
'new-account-email' ),
$user['username'],
$this->form->get( 'password' )
- ),
- 72
- ),
- "From: Wikimania Scholarships
<[email protected]>\r\n" .
- "MIME-Version: 1.0\r\n" .
- "X-Mailer: Wikimania registration
system\r\n" .
- "Content-type: text/plain;
charset=utf-8\r\n" .
- "Content-Transfer-Encoding: 8bit"
- );
+ )
+ );
+ if ( !$sent ) {
+ $this->flash(
+ 'error',
+ 'Failed to send account
creation message. Check logs.'
+ );
+ }
} else {
$this->flash( 'error', 'User creation
failed. Check logs.' );
diff --git a/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
b/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
index d9625dc..310a32d 100644
--- a/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
+++ b/src/Wikimania/Scholarship/Controllers/ScholarshipApplication.php
@@ -56,15 +56,11 @@
$message = preg_replace(
'/\$1/',
"{$this->form->get('fname')} {$this->form->get('lname')}", $message );
- //FIXME: using mail directly
seems wrong
- mail( $this->form->get('email'),
+ $this->mailer->mail(
+
$this->form->get('email'),
$this->slim->wgLang->message( 'form-email-subject' ),
- wordwrap( $message, 72
),
- "From: Wikimania
Scholarships <[email protected]>\r\n" .
- "MIME-Version: 1.0\r\n"
.
- "X-Mailer: Wikimania
registration system\r\n" .
- "Content-type:
text/plain; charset=utf-8\r\n" .
-
"Content-Transfer-Encoding: 8bit" );
+ $message
+ );
$submitted = true;
}
}
diff --git a/src/Wikimania/Scholarship/Mailer.php
b/src/Wikimania/Scholarship/Mailer.php
new file mode 100644
index 0000000..cdfe667
--- /dev/null
+++ b/src/Wikimania/Scholarship/Mailer.php
@@ -0,0 +1,108 @@
+<?php
+/**
+ * @section LICENSE
+ * This file is part of Wikimania Scholarship Application.
+ *
+ * Wikimania Scholarship Application is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
+ *
+ * Wikimania Scholarship Application is distributed in the hope that it will
+ * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+ * Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with Wikimania Scholarship Application. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * @file
+ */
+
+namespace Wikimania\Scholarship;
+
+use \PHPMailer;
+use \phpmailerException;
+use Psr\Log\LoggerInterface;
+
+/**
+ * Wrapper around PHPMailer
+ *
+ * @author Bryan Davis <[email protected]>
+ * @copyright © 2013 Bryan Davis and Wikimedia Foundation.
+ */
+class Mailer {
+
+ /**
+ * @var LoggerInterface $logger
+ */
+ protected $logger;
+
+ /**
+ * @var array $settings
+ */
+ protected $settings = array(
+ 'AllowEmpty' => false,
+ 'CharSet' => 'utf-8',
+ 'ContentType' => 'text/plain',
+ 'From' => '[email protected]',
+ 'FromName' => 'Wikimania Scholarships',
+ 'Mailer' => 'smtp',
+ 'WordWrap' => 72,
+ 'XMailer' => 'Wikimania registration system',
+ );
+
+
+ /**
+ * @param array $settings Configuration settings for PHPMailer
+ * @param LoggerInterface $logger Log channel
+ */
+ public function __construct( $settings = array(), $logger = null ) {
+ $this->logger = $logger ?: new \Psr\Log\NullLogger();
+ $settings = is_array( $settings ) ? $settings : array();
+ $this->settings = array_merge( $this->settings, $settings );
+ }
+
+
+ /**
+ * @param string $to Recipent(s)
+ * @param string $subject Subject
+ * @param string $message Message
+ * @param array $settings Additional settings
+ */
+ public function mail( $to, $subject, $message, $settings = array() ) {
+ try {
+ $mailer = $this->createMailer( $settings );
+ $mailer->addAddress( $to );
+ $mailer->Subject = $subject;
+ $mailer->Body = $message;
+ return $mailer->send();
+
+ } catch ( phpmailerException $e ) {
+ $this->logger->error( 'Failed to send message:
{message}', array(
+ 'method' => __METHOD__,
+ 'exception' => $e,
+ 'message' => $e->getMessage(),
+ ) );
+ }
+ }
+
+
+ /**
+ * Create and configure a PHPMailer instance.
+ *
+ * @param array $settings Configuration settings
+ * @return PHPMailer New mailer configured with default, instance and
local
+ * settings
+ */
+ protected function createMailer( $settings = null ) {
+ $settings = is_array( $settings ) ? $settings : array();
+ $mailer = new PHPMailer( true );
+ foreach ( array_merge( $this->settings, $settings ) as $key =>
$value ) {
+ $mailer->set( $key, $value );
+ }
+ return $mailer;
+ }
+
+} //end Mailer
--
To view, visit https://gerrit.wikimedia.org/r/98463
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I58221cb7c9c42183ab48f81c4c861f2054be68a4
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/wikimania-scholarships
Gerrit-Branch: master
Gerrit-Owner: BryanDavis <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits