1

我在 cakephp 工作,想在用户注册时发送确认链接,但我对SMTP了解不多。这是我写的我正在使用令牌来确认电子邮件,如果用户点击相同的确认链接,它将在下次过期。 这是用户控制器/注册方法:

 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()
4

3 回答 3

0

尝试使用mailjet并配置您的服务器(wamp或xampp)目录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',
    ],

],
于 2016-03-27T23:09:02.833 回答
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);
于 2016-03-28T09:21:58.280 回答
0
  1. 首先在您的 App Controller 中包含电子邮件组件,如下所示:

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

  2. 像这样在您的 App 控制器中创建一个 sendMail 函数

    公共函数 _sendMail($to, $from, $replyTo, $subject, $element,$parsingParams = array(),$attachments="", $sendAs = 'html', $bcc = array()){       
        $端口 = '';
        $timeout = '';
        $主机='';
        $用户名 = '';
        $密码='';
        $客户端 = '';

        $toAraay = 数组();
        if (!is_array($to)) {
            $toAraay[] = $to;
        } 别的 {
            $toAraay = $to;
        }
        $this->Email->smtpOptions = array(
            '端口' => "$端口",
            '超时' => "$timeout",
            '主机' => "$主机",
            '用户名' => "$用户名",  
            '密码' => "$密码",  
            '客户' => "$客户"
        );
        $this->Email->delivery = 'smtp';
        foreach ($parsingParams as $key => $value) {
            $this->set($key, $value);
        }
        foreach ($toAraay as $email) {
            $this->Email->to = $email;
            $this->电子邮件->主题 = $主题;
            $this->Email->replyTo = $replyTo;
            $this->Email->from = $from;
            如果(!空($bcc)){
                $this->电子邮件->cc = $bcc[0];
            }

             if ($attachments!="") {
                $this->Email->附件 = array();
                $this->Email->attachments[0] = $attachments ;
            }
            $this->Email->template = $element;
            $this->Email->sendAs = $sendAs;
            $this->电子邮件->发送();

            $this->Email->reset();
        }
    }
  1. 在 View/Emails/html 中创建 sendmail.ctp 文件 在文件中
    添加此内容并添加页眉或页脚

    <?php 回显 $message; ?>

  2. 现在,每当您想发送电子邮件时,请像这样调用此函数:

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

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

$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());
于 2016-03-29T09:47:42.593 回答