Send an email with a link

This code outputs:

Call to a member function link() on a non-object 

I want to send the link by email. What is wrong with the code? thank

class User extends AppModel {
    public $helpers = array('Html');
    public function sendEmail($email_user, $code) {
            $email = new CakeEmail();
            $email->config('smtp');

            $pwrurl = "http://xxx/users?se=check&mail=".$email_user."&code=".$code;
            $link = $this->Html->link('Link name', $pwrurl, array('target' => '_blank'));
            $email->from(array('xxxn@gmail.com' => 'xxxx'))
            ->to($email_user)
            ->emailFormat('html')
            ->send('My message:: '.$link);
            return true;
        }
}

docs

Update: I get the same error.

App::uses('CakeEmail', 'Network/Email');
App::uses('AppHelper', 'View/Helper');

class AppController extends Controller {
      public $helpers = array('Form', 'Html');

      public function sendEmail($email_user, $codeLink) {
             //same code.
      }

}

+5
source share
3 answers

This should probably be in the controller:

class UsersController extends AppController {
   var $name = 'Users';
   var $helpers = array('Html');
   var $components = array('Email');

   function _email($email_user, $code){
      $pwrurl = "http://xxx/users?se=check&mail=".$email_user."&code=".$code;
      $this->Email->from = 'From: Someone';
      $this->Email->to = $email_user;
      $this->Email->subject  = 'Your Subject';
      $this->Email->sendAs = 'html';
      $this->Email->template = 'your_template';
      $this->set('pwrurl', $pwrurl);
      $this->Email->send();
   }
}

Then create an email template in the / elements / email / html views, named just like your template in the above code. For this example, your_template.ctp. Compose the message the way you want, and wherever you want the link to appear:

$this->Html->link('Link name', $pwrurl, array('target' => '_blank'));

Hope this helps.

+2
source

from here :

$pwrurl = '<a href="http://' . $_SERVER['SERVER_NAME'] ."/users/otpregister/".$email_user."&code=".$codeLink.'"> Registration link</a>'; 
+1
source

:

App::uses('HtmlHelper', 'View/Helper');
class User extends AppModel {
public function sendEmail($email_user, $code) {
        $email = new CakeEmail();
        $email->config('smtp');

        $pwrurl = "http://xxx/users?se=check&mail=".$email_user."&code=".$code;
        $link = $this->Html->link('Link name', $pwrurl, array('target' => '_blank'));
        $email->from(array('xxxn@gmail.com' => 'xxxx'))
        ->to($email_user)
        ->emailFormat('html')
        ->send('My message:: '.$link);
        return true;
    }
}

As due to $helpersis a valid data member for controllers, not for models. Also a good alternative is to write the method sendEmail()to your AppController.php and call this method in your controller method using$this->sendEmail($user_email, $code);

This documentation page can help you achieve the same.

Hope this works for you.

0
source

All Articles