我在cakepp工作,想在用户注册时发送确认链接,但我对SMTP了解不多。以下是我所写的内容,我正在使用Token确认电子邮件,如果用户点击相同的确认链接,该电子邮件将在下次过期。这里是用户控制器/注册方法:
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');
}
}
}
这里是users/verify方法来确认用户是否点击了确认链接。
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()
尝试使用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',
],
],
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);
-
首先在你的应用程序控制器中包括电子邮件组件,如下所示:
public$components=数组('电子邮件');
-
在你的应用程序控制器中创建一个sendMail功能,如
公共函数_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;}其他{$toAraay=$to;}$this->电子邮件->smtpOptions=数组('port'=>"$port",'timeout'=>"$timeout",'host'=>"$host",'username'=>"$username",'password'=>"$password","client"=>"$client");$this->电子邮件->delivery='smtp';foreach($parsingParams为$key=>$value){$this->集合($key,$value);}foreach($toAraay作为$email){$this->电子邮件->to=$Email;$this->电子邮件->subject=$subject;$this->电子邮件->replyTo=$replyTo;$this->电子邮件->from=$from;if(!空($bcc)){$this->电子邮件->cc=$bcc[0];}if($attachments!="){$this->Email->attachments=array();$this->Email->attachments[0]=$attachments;}$this->Email->template=$element;$this->电子邮件->sendAs=$sendAs;$this->Email->send();$this->电子邮件->reset();}}
-
在View/Emails/html中创建sendmail.ctp文件
在文件中添加此内容,并添加您的页眉或页脚<?php echo$message;?>
-
现在,每当你想发送电子邮件时,都可以这样调用这个功能:
$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());