Permalink
Browse files

Replaced native mail() call with SwiftMailer so it can be replaced wi…

…th Amazon SES implementation
  • Loading branch information...
1 parent 18cf200 commit 1cb998ff7c387d5b01dbb2fd02fe60f139459d1d @sergeychernyshev sergeychernyshev committed Apr 22, 2016
Showing with 59 additions and 17 deletions.
  1. +3 −0 .gitmodules
  2. +10 −5 classes/Invitation.php
  3. +11 −5 classes/User.php
  4. +22 −0 default_config.php
  5. +12 −7 modules/usernamepass/forgotpassword.php
  6. +1 −0 swiftmailer
View
@@ -34,3 +34,6 @@
[submodule "imagesloaded"]
path = imagesloaded
url = git://github.com/desandro/imagesloaded.git
+[submodule "swiftmailer"]
+ path = swiftmailer
+ url = git://github.com/swiftmailer/swiftmailer.git
@@ -423,15 +423,20 @@ public static function sendUserInvitation($issuer, $name, $email, $note = null,
*/
public function send() {
$email = $this->getSentToEmail();
+ $name = $this->getSentToName();
- $message = call_user_func_array(UserConfig::$onRenderInvitationEmailMessage, array($this));
+ $message_body = call_user_func_array(UserConfig::$onRenderInvitationEmailMessage, array($this));
$subject = call_user_func_array(UserConfig::$onRenderInvitationEmailSubject, array($this));
- $headers = 'From: ' . UserConfig::$supportEmailFrom . "\r\n" .
- 'Reply-To: ' . UserConfig::$supportEmailReplyTo . "\r\n" .
- 'X-Mailer: ' . UserConfig::$supportEmailXMailer;
+ $message = Swift_Message::newInstance($subject, $message_body);
+ $message->setFrom(array(UserConfig::$supportEmailFromEmail => UserConfig::$supportEmailFromName));
+ $message->setTo(array($email => $name));
+ $message->setReplyTo(array(UserConfig::$supportEmailReplyTo));
- mail($email, $subject, $message, $headers);
+ $headers = $message->getHeaders();
+ $headers->addTextHeader('X-Mailer', UserConfig::$supportEmailXMailer);
+
+ UserConfig::$mailer->send($message);
}
/**
View
@@ -6,6 +6,7 @@
require_once(__DIR__ . '/CookieStorage.php');
require_once(__DIR__ . '/CampaignTracker.php');
require_once(__DIR__ . '/Invitation.php');
+require_once(dirname(__DIR__) . '/swiftmailer/lib/swift_required.php');
/**
* This class represents a registerd user in the system
@@ -639,6 +640,7 @@ public static function verifyEmailLinkCode($code, $user = null) {
*/
public function sendEmailVerificationCode() {
$email = $this->getEmail();
+ $name = $this->getName();
// Silently fail to avoid email discovery
if (is_null($email)) {
@@ -667,15 +669,19 @@ public function sendEmailVerificationCode() {
$verification_link = UserConfig::$USERSROOTFULLURL . '/verify_email.php?code=' . urlencode($code);
- $message = call_user_func_array(UserConfig::$onRenderVerificationCodeEmail, array($verification_link, $code));
+ $message_body = call_user_func_array(UserConfig::$onRenderVerificationCodeEmail, array($verification_link, $code));
$subject = UserConfig::$emailVerificationSubject;
- $headers = 'From: ' . UserConfig::$supportEmailFrom . "\r\n" .
- 'Reply-To: ' . UserConfig::$supportEmailReplyTo . "\r\n" .
- 'X-Mailer: ' . UserConfig::$supportEmailXMailer;
+ $message = Swift_Message::newInstance($subject, $message_body);
+ $message->setFrom(array(UserConfig::$supportEmailFromEmail => UserConfig::$supportEmailFromName));
+ $message->setTo(array($email => $name));
+ $message->setReplyTo(array(UserConfig::$supportEmailReplyTo));
- mail($email, $subject, $message, $headers);
+ $headers = $message->getHeaders();
+ $headers->addTextHeader('X-Mailer', UserConfig::$supportEmailXMailer);
+
+ UserConfig::$mailer->send($message);
}
/**
View
@@ -10,6 +10,7 @@
require_once(__DIR__ . '/classes/Badge.php');
require_once(__DIR__ . '/classes/StartupAPIModule.php');
require_once(__DIR__ . '/tools.php');
+require_once(__DIR__ . '/swiftmailer/lib/swift_required.php');
/**
* This class contains a bunch of static variables defining how Startup API instance
@@ -620,12 +621,30 @@ class UserConfig {
*
* ===================================================================== */
+ /**
+ * @var Swift_Mailer Transactional mailer object
+ */
+ public static $mailer;
+
/**
* @var string Name and email to send invitations from (e.g. 'User Support <support@example.com>')
+ * @deprecated
*/
public static $supportEmailFrom = 'User Support <support@example.com>';
/**
+ * Name to send invitations from (e.g. 'User Support')
+ * @var string
+ */
+ public static $supportEmailFromName = 'User Support';
+
+ /**
+ * Email to send invitations from (e.g. 'support@example.com')
+ * @var string
+ */
+ public static $supportEmailFromEmail = 'support@example.com';
+
+ /**
* @var string Reply-To email address for return emails
*/
public static $supportEmailReplyTo = 'support@example.com';
@@ -1147,6 +1166,9 @@ public static function init() {
UserConfig::$supportEmailXMailer = 'Startup API (PHP/' . phpversion() . ')';
+ // Instantiating email sending object
+ UserConfig::$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
+
// Built in activities
/**
@@ -9,10 +9,6 @@
$subject = UserConfig::$passwordRecoveryEmailSubject;
- $headers = 'From: ' . UserConfig::$supportEmailFrom . "\r\n" .
- 'Reply-To: ' . UserConfig::$supportEmailReplyTo . "\r\n" .
- 'X-Mailer: ' . UserConfig::$supportEmailXMailer;
-
if (!is_null(UserConfig::$onRenderTemporaryPasswordEmail)) {
$baseurl = UserConfig::$USERSROOTFULLURL . '/login.php';
@@ -24,10 +20,19 @@
$name_enc = urlencode($username);
$email = $user->getEmail();
+ $name = $user->getName();
+
+ $message_body = call_user_func_array(UserConfig::$onRenderTemporaryPasswordEmail, array($baseurl, $username, $temppass));
+
+ $message = Swift_Message::newInstance($subject, $message_body);
+ $message->setFrom(array(UserConfig::$supportEmailFromEmail => UserConfig::$supportEmailFromName));
+ $message->setTo(array($email => $name));
+ $message->setReplyTo(array(UserConfig::$supportEmailReplyTo));
- $message = call_user_func_array(UserConfig::$onRenderTemporaryPasswordEmail, array($baseurl, $username, $temppass));
+ $headers = $message->getHeaders();
+ $headers->addTextHeader('X-Mailer', UserConfig::$supportEmailXMailer);
- mail($email, $subject, $message, $headers);
+ UserConfig::$mailer->send($message);
}
// We always report "sent" to avoid information disclosure
@@ -48,4 +53,4 @@
$template_info['slug'] = 'usernamepass';
-StartupAPI::$template->display('@startupapi/modules/usernamepass/forgotpassword.html.twig', $template_info);
+StartupAPI::$template->display('@startupapi/modules/usernamepass/forgotpassword.html.twig', $template_info);
Submodule swiftmailer added at fffbc0

0 comments on commit 1cb998f

Please sign in to comment.