Tuesday, January 5, 2016

CakePHP - no emails are being sent and there are no error messages

Hi, i am new to cakephp and currently working on a website that has been developed using it. As part of the site members can refer friends and family via email where their reference number is sent as part of the sign up link. These emails are not sending and there are no error messages to tell if anything is going wrong - i've even tried to use the normal mail() function over CakeEmail() but still no luck. I posted my problem on stackoverflow but it has been put on hold as they think it is not appropriate to ask on that site.

i would really appreciate any help.

i have attached the code below: 

UsersController.php :

public function send_invitation() {
          $user
= $this->Auth->user();
         
//echo "<pre>"; print_r($user); die;
          $this
->layout = "dashboard";
         
if (!empty($this->request->data)) {
               $this
->Reference->set($this->request->data);
               $this
->Reference->setValidation('add');
               
if ($this->Reference->validates()) {
                    $check
= $this->User->find('first', array('conditions' => array('User.email' => $this->request->data['Reference']['email'])));
                   
if (!empty($check)) {
                         $response
['status'] = "error";
                         $response
['message'] = 'Email already registered.';
                         $this
->Session->setFlash($response['message'], $response['status']);
                         
//$this->redirect($this->referer());
                   
}
                    $this
->Reference->save($this->request->data);
                    $language
= $this->Session->read('Config.language');
                   
if ($language == "en" || $language == "") {
                         $emailTemp
= $this->EmailTemplate->find('first', array('conditions' => array('EmailTemplate.title' => 'invitation')));
                   
} else {
                         $emailTemp
= $this->EmailTemplate->find('first', array('conditions' => array('EmailTemplate.title' => 'invitation', 'EmailTemplate.locale' => $language)));
                   
}


                    $emailContent
= $emailTemp['EmailTemplate']['html_content'];
                    $link
= WWWROOT . 'homes/register/' . $user['referral_code'];
                    $senderInfo
= $this->User->find('first', array('conditions' => array('User.id' => $this->request->data['Reference']['user_id']), 'fields' => array('User.fname', 'User.lname')));
                    $sender
= ucwords($senderInfo['User']['fname'] . ' ' . $senderInfo['User']['lname']);
                    $receiver
= $this->request->data['Reference']['name'];
                    $message
= $this->request->data['Reference']['message'];
                    $emailContent
= str_replace(array('$link', '$sender', '$receiver', '$message', '$code'), array($link, $sender, $receiver, $message, $user['referral_code']), $emailContent);
           
           


                    $email
= new CakeEmail();
                    $email
->config('gmail');
                    $email
->template('email_template');
                    $email
->emailFormat('html');
                    $email
->viewVars(array('emailContent' => $emailContent));
                    $email
->to($this->request->data['Reference']['email']);
                    $email
->from(array($emailTemp['EmailTemplate']['from_email'] => 'CoverMyClass'));
                    $email
->subject($emailTemp['EmailTemplate']['subject']);
                   
//echo "<pre>hello..."; print_r($email); die;
                   
try { $email->send(); } catch (Exception $e) { print_r($e); }
                   
                   
//$response['status'] = "success";
                   
//$response['message'] = 'Invitation has been sent successfully.';
                   
//$this->Session->setFlash($response['message'], $response['status']);
                   
/*$this->redirect($this->referer());*/
               
}
         
}

          $this
->set("user", $user);
     
}

send_invitation.ctp:

<?php ?>
<div class="dashboard-container-right">
           
<div class="dashboard-innerright">
               
<div class="upper-dashboard"><h1> <?php echo __("Send Invitations"); ?> </h1></div>    
               
<div class="botm-forcolor">
                   
<div class="midd-dashord">
                       
<?php echo __("Your Referral Code is"); ?> <strong><?php echo $user["referral_code"];?> </strong>
                       
<div class="invite-content">
                         
<!--- Instructor Login -->
                     
<div class="tab-pane active" id="instructor">
                           
<?php echo $this->Form->create('Reference' , array('id'=>'send_invites' , 'url' => array('controller' => 'users', 'action' => 'send_invitation' ) , 'novalidate'=>'novalidate')); ?>        
                   
<?php  $form=$this->Form;?>
                       
<div class="padding_15tabs">
                           
<div class="col-md-12  show-grid row">
                   
<?php $name=__("Name"); ?>    
                   
<?php echo $form->input('Reference.name',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$name, 'div' => false,'label'=>false))?>                            


                               
</div>
                           
<div class="col-md-12  show-grid row">
                   
<?php $email=__("Email"); ?>    
                   
<?php echo $form->input('Reference.email',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$email, 'div' => false,'label'=>false))?>                            
                               
</div>
                               
<div class="col-md-12  show-grid row">
                   
<?php $message=__("Message"); ?>    
                   
<?php echo $form->textarea('Reference.message',array('class'=>'col-xs-12 col-sm-12 col-lg-12 text-style', 'placeholder'=>$message, 'div' => false,'label'=>false))?>                            


                               
</div>
                            <input type="hidden" name="data[Reference][user_id]" value="
<?php echo $user['id']; ?>">                    
                           
<div class="col-md-12  show-grid row">
                               
<div class="align-center">
                                   
<span class="span_float">
                                        <input type="submit" value="
<?php echo __('Invite'); ?>" class="create-account btn btn-primary"  /><br/>

                                   
</span>
                               
</div>
                           
</div>
                               
                       
</div>    
                       
</form>
                     
</div>
                       
                       
                       
                   
</div>
                   
               
</div>
           
</div>
       
</div>

app/config/email.php :

class EmailConfig {

   
public $default = array(
       
'transport' => 'Mail',
       
'from' => 'noreply@me.today',
       
'charset' => 'utf-8',
       
'headerCharset' => 'utf-8',
   
);

   
public $gmail = array(
       
'host' => 'smtp.gmail.com',
       
'port' => 465,
       
'username' => 'me@gmail.com',
       
'password' => 'password',
       
'transport' => 'Smtp',
       
'tls' => true
   
);
}


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at https://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: