1

I am working in cakephp and want to send confirmation link on user signup but i do not know much about SMTP. Here is What i have written I am using Token to confirm email which will expire next time if user hit the same confirmation link. Here is usercontroller/signup method:

 public function signup()
{
    $this->layout = 'main';
    if ($this->request->is('post')) {
        $this->User->create();
        $this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
        $hash = sha1($this->request->data['User']['username'] . rand(0, 100));
        $this->request->data['User']['tokenhash'] = $hash;
        if ($this->User->validates()) {
            $this->User->save($this->request->data);

            $ms = 'Click on the link below to complete registration ';
            $ms .= 'http://localhost/FindTutor/users/verify/t:' . $hash . '/n:' . $this->data['User']['username'] . '';
            $ms = wordwrap($ms, 70);
            $this->Email->from = 'usman.jamil0308@gmail.com';
            $this->Email->to = $this->request->data['User']['email'];
            $this->Email->subject = 'Confirm Registration..';
            $this->Email->send($ms);
            $this->Session->setFlash('Please Check your email for validation Link');
            $this->redirect('/users/login');
        }
    }
}

Here is users/verify method to confirm if user hit the confirmation link.

public function verify(){
    //check if the token is valid
    if (!empty($this->passedArgs['n']) && !empty($this->passedArgs['t'])){
        $name = $this->passedArgs['n'];
        $tokenhash = $this->passedArgs['t'];
        $results = $this->User->findByUsername($name);
        if ($results['User']['activate']==0){
            //check the token
            if($results['User']['tokenhash']==$tokenhash)
            {
                $results['User']['activate']=1;
                //Save the data
                $this->User->save($results);
                $this->Session->setFlash('Your registration is complete');
                $this->redirect('/users/login');
                exit;
            }
            else{
                $this->Session->setFlash('Your registration failed please try again');
                $this->redirect('/users/register');
            }
        }
        else {
            $this->Session->setFlash('Token has alredy been used');
            $this->redirect('/users/register');
        }
    }else
    {
        $this->Session->setFlash('Token corrupted. Please re-register');
        $this->redirect('/users/register');
    }

}

Error is somthing like this:

mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
Muhammad Usman
  • 2,419
  • 1
  • 11
  • 18

3 Answers3

0

try to use mailjet and configure your sever (wamp or xampp)the directorie sendmail and configure your app.php like that

'EmailTransport' => [
    'default' => [
        'className' => 'Mail',
        // The following keys are used in SMTP transports
        'host' => 'localhost',
        'port' => 25,
        'timeout' => 30,
        'username' => '',
        'password' => '',
        'client' => null,
        'tls' => null,
    ],
    'mailjet' => [
        'className' => 'smtp',

        // The following keys are used in SMTP transports
        'host' => 'in-v3.mailjet.com',

        'username' => 'copy that from your account mailjet',

        'password' => 'copy that from your account mailjet',

        'port' => 587,
        'timeout' => 3000,
        'client' => null,
        'tls' => null,
    ],
],

'Email' => [
    'default' => [
        'transport' => 'mailjet',
        'from' => 'xxxxxx@gmail.com',
        //'charset' => 'utf-8',
        //'headerCharset' => 'utf-8',
    ],

],
Ikbel
  • 63
  • 7
0
  public $smtp = array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'your email id',
    'password' => 'your password',
    'transport' => 'Smtp',
    );


App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('smtp');
$Email->from('info@email.com');
$Email->to($email);
$message = "hello";
$Email->send($message);
Salman
  • 67
  • 4
0
  1. First Include Email Component in your App Controller like this :

    public $components = array( 'Email' );

  2. Create a sendMail function in your App controller like this

    public function _sendMail( $to, $from, $replyTo, $subject, $element,$parsingParams = array(),$attachments ="", $sendAs = 'html', $bcc = array()){       
        $port        =    '';
        $timeout     =    '';
        $host        =    '';
        $username    =    '';
        $password    =    '';
        $client      =    '';

        $toAraay = array();
        if (!is_array($to)) {
            $toAraay[] = $to;
        } else {
            $toAraay = $to;
        }
        $this->Email->smtpOptions = array(
            'port'     => "$port",
            'timeout'  =>  "$timeout",
            'host'     =>  "$host",
            'username' => "$username",  
            'password' => "$password",  
            'client'   => "$client"
        );
        $this->Email->delivery    = 'smtp';
        foreach ($parsingParams as $key => $value) {
            $this->set($key, $value);
        }
        foreach ($toAraay as $email) {
            $this->Email->to      = $email;
            $this->Email->subject = $subject;
            $this->Email->replyTo = $replyTo;
            $this->Email->from    = $from;
            if(!empty($bcc)){
                $this->Email->cc      = $bcc[0];
            }

             if ($attachments!="") {
                $this->Email->attachments    = array();
                $this->Email->attachments[0] = $attachments ;
            }
            $this->Email->template = $element;
            $this->Email->sendAs = $sendAs;
            $this->Email->send();

            $this->Email->reset();
        } 
    }
  1. Create a sendmail.ctp file in View/Emails/html
    Add this content in file and also add your header or footer

    <?php echo $message; ?>

  2. Now whenever you want to send email call this function like this :

    $this->_sendMail($to, $from, $replyTo, $subject, 'sendmail', array('message' => $message), "", 'html', $bcc = array());

Now you can implement your logic for verification email like this :

$message = 'Click on the link below to complete registration ';
$message .= 'http://localhost/FindTutor/users/verify/t:' . $hash . '/n:' . $this->data['User']['username'] . '';

$from    = 'usman.jamil0308@gmail.com';
$to      = $this->request->data['User']['email'];
$subject = 'Confirm Registration..';
$replyTo =  'usman.jamil0308@gmail.com';    
$this->_sendMail($to, $from, $replyTo, $subject, 'sendmail', array('message' => $message), "", 'html', $bcc = array());
Akshay Sharma
  • 1,042
  • 1
  • 8
  • 21