Merge pull request #79 from neeraj-lad/AIRAVATA-2509

[AIRAVATA-2509] Account confirmation, password reset emails not making it to user
diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php
index 083bff0..d18900c 100644
--- a/app/controllers/AccountController.php
+++ b/app/controllers/AccountController.php
@@ -30,6 +30,7 @@
             "password" => self::PASSWORD_VALIDATION,
             "confirm_password" => "required|same:password",
             "email" => "required|email",
+            "confirm_email" => "required|same:email",
         );
 
         $messages = array(
@@ -40,7 +41,7 @@
         $validator = Validator::make(Input::all(), $rules, $messages);
         if ($validator->fails()) {
             return Redirect::to("create")
-                ->withInput(Input::except('password', 'password_confirm'))
+                ->withInput(Input::except('password', 'confirm_password', 'email', 'confirm_email'))
                 ->withErrors($validator);
         }
 
@@ -52,7 +53,7 @@
 
         if (Keycloak::usernameExists($username)) {
             return Redirect::to("create")
-                ->withInput(Input::except('password', 'password_confirm'))
+                ->withInput(Input::except('password', 'confirm_password'))
                 ->with("username_exists", true);
         } else {
 
diff --git a/app/libraries/EmailUtilities.php b/app/libraries/EmailUtilities.php
index 561cd88..ff9ce9e 100644
--- a/app/libraries/EmailUtilities.php
+++ b/app/libraries/EmailUtilities.php
@@ -19,7 +19,12 @@
         $body = str_replace("\$lastName", $lastName, $body);
         $body = str_replace("\$validTime", $validTime, $body);
 
-        EmailUtilities::sendEmail($subject, [$email], $body);
+        $recipient = array();
+        $recipient['firstName'] = $firstName;
+        $recipient['lastName'] = $lastName;
+        $recipient['email'] = $email;
+
+        EmailUtilities::sendEmail($subject, [$recipient], $body);
     }
 
     public static function verifyEmailVerification($username, $code){
@@ -47,7 +52,12 @@
         $body = str_replace("\$lastName", $lastName, $body);
         $body = str_replace("\$validTime", $validTime, $body);
 
-        EmailUtilities::sendEmail($subject, [$email], $body);
+        $recipient = array();
+        $recipient['firstName'] = $firstName;
+        $recipient['lastName'] = $lastName;
+        $recipient['email'] = $email;
+
+        EmailUtilities::sendEmail($subject, [$recipient], $body);
     }
 
     public static function verifyUpdatedEmailAccount($username, $code){
@@ -76,7 +86,12 @@
         $body = str_replace("\$lastName", $lastName, $body);
         $body = str_replace("\$validTime", $validTime, $body);
 
-        EmailUtilities::sendEmail($subject, [$email], $body);
+        $recipient = array();
+        $recipient['firstName'] = $firstName;
+        $recipient['lastName'] = $lastName;
+        $recipient['email'] = $email;
+
+        EmailUtilities::sendEmail($subject, [$recipient], $body);
     }
 
     public static function verifyPasswordResetCode($username, $code){
@@ -90,7 +105,7 @@
     }
 
     //PGA sends email to Admin about new request
-    public static function gatewayRequestMail($firstName, $lastName, $email, $gatewayName){
+    public static function gatewayRequestMail($firstName, $lastName, $emails, $gatewayName){
 
         $emailTemplates = json_decode(File::get(app_path() . '/config/email_templates.json'));
         $subject = $emailTemplates->gateway_request->subject;
@@ -101,7 +116,13 @@
         $body = str_replace("\$lastName", $lastName, $body);
         $body = str_replace("\$gatewayName", $gatewayName, $body);
 
-        EmailUtilities::sendEmail($subject, $email, $body);
+        $recipients = array();
+        foreach($emails as $email) {
+            $recipient['email'] = $email;
+            array_push($recipients, $recipient);
+        }
+
+        EmailUtilities::sendEmail($subject, $recipients, $body);
 
     }
 
@@ -115,13 +136,15 @@
         $body = str_replace("\$url", URL::to('/') . '/admin/dashboard', $body);
         $body = str_replace("\$gatewayId", $gatewayId, $body);
 
+        $recipient = array();
+        $recipient['email'] = $email;
 
-        EmailUtilities::sendEmail($subject, [$email], $body);
+        EmailUtilities::sendEmail($subject, [$recipient], $body);
 
     }
 
     //PGA sends email to Admin when Gateway is UPDATED
-    public static function gatewayUpdateMailToAdmin($email, $gatewayId){
+    public static function gatewayUpdateMailToAdmin($emails, $gatewayId){
 
         $emailTemplates = json_decode(File::get(app_path() . '/config/email_templates.json'));
         $subject = $emailTemplates->update_to_admin->subject;
@@ -130,7 +153,13 @@
         $body = str_replace("\$url", URL::to('/') . '/admin/dashboard/gateway', $body);
         $body = str_replace("\$gatewayId", $gatewayId, $body);
 
-        EmailUtilities::sendEmail($subject, $email, $body);
+        $recipients = array();
+        foreach($emails as $email) {
+            $recipient['email'] = $email;
+            array_push($recipients, $recipient);
+        }
+
+        EmailUtilities::sendEmail($subject, $recipients, $body);
 
     }
 
@@ -159,7 +188,12 @@
         $mail->ContentType = 'text/html; charset=utf-8\r\n';
 
         foreach($recipients as $recipient){
-            $mail->addAddress($recipient);
+            if (array_key_exists('firstName', $recipient) && array_key_exists('lastName', $recipient)) {
+                $mail->addAddress($recipient['email'], $recipient['firstName'] . " " . $recipient['lastName']);
+            }
+            else {
+                $mail->addAddress($recipient['email']);
+            }
         }
 
         $mail->Subject = $subject;
diff --git a/app/views/account/create.blade.php b/app/views/account/create.blade.php
index c86e223..f931ac9 100644
--- a/app/views/account/create.blade.php
+++ b/app/views/account/create.blade.php
@@ -72,8 +72,14 @@
 
             <div><input class="form-control" id="email" name="email" placeholder="email@example.com"
                         required="required" title="" type="email" value="{{Input::old('email') }}"
-                         data-toggle="popover" data-placement="left" data-content="Please make sure that you enter a correct email address as a verification mail will be sent to this addresss."/></div>
+                         data-toggle="popover" data-placement="left" data-content="Please make sure that you enter a correct email address as a verification mail will be sent to this address."/></div>
         </div>
+        <div class="form-group required"><label class="control-label">E-mail (again)</label>
+
+                <div><input class="form-control" id="confirm_email" name="confirm_email" placeholder="email@example.com (again)"
+                            required="required" title="" type="email" value="{{Input::old('confirm_email') }}"
+                            data-toggle="popover" data-placement="left" data-content="Please make sure that you enter the same email address as above as a verification mail will be sent to this address."/></div>
+            </div>
         <div class="form-group required"><label class="control-label">First Name</label>
 
             <div><input class="form-control" id="first_name" maxlength="30" name="first_name"