如何通过smtp Cakephp发送电子邮件确认链接

时间:2016-03-26 17:50:00

标签: email cakephp smtp email-validation confirmation-email

我在cakephp工作,想要发送用户注册的确认链接,但我不太了解 SMTP 。 以下是我所写的内容我正在使用令牌确认电子邮件,如果用户点击相同的确认链接,该邮件将在下次到期。 这是usercontroller / signup 方法:

 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');
        }
    }
}

以下是用户/验证方法,以确认用户是否点击确认链接。

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');
    }

}

  

错误是这样的:

mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

3 个答案:

答案 0 :(得分:0)

尝试使用mailjet并配置您的服务器(wamp或xampp)directorie sendmail并配置您的app.php

'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',
    ],

],

答案 1 :(得分: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);

答案 2 :(得分:0)

  1. 首先在App Controller中包含电子邮件组件,如下所示:

    public $ components = array(     '电子邮件'  );

  2. 在App控制器中创建一个sendMail函数,如下所示

  3.     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. 在View / Emails / html中创建sendmail.ctp文件 在文件中添加此内容,并添加页眉或页脚

      <?php echo $ message; ?>

    2. 现在,每当您想发送电子邮件时,请按以下方式调用此函数:

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

    3. 现在,您可以像这样实现验证电子邮件的逻辑:

      $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());