如何在Cakephp 2中注册后发送邮件确认

时间:2015-03-16 23:37:58

标签: javascript php cakephp cakephp-2.0

你好我想在注册后发送邮件,但是 我有一个问题,这是UsersController:



public function add() {
		if ($this->request->is('post')) {
			$this->User->create();
			if ($this->User->save($this->request->data)) {
$link = array('controller'=>'users','action'=>'activate', $this->User->id.'-'.md5($this->request->data['User']['password']));				
App::uses('CakeEmail','Network/Email');
$mail = new CakeEmail();
$mail->from('dafhermcslama@gmail.com')
	 ->to($this->request->data['User']['email'])
	 ->subject('Test :: Inscription')
	 ->emailFormat('html')
	 ->template('signup')
	 ->viewVars(array('username'=>$this->request->data['User']['username'], 'link'=>$link))
	 ->send();


				$this->Session->setFlash(__('The user has been saved.'));
				$this->Auth->login($this->data);
				return $this->redirect(array('controller'=>'users','action' => 'index'));
				//$this->redirect('/users/index');
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
			}
		}
		$countries = $this->User->Country->find('list');
		$cities = $this->User->City->find('list');
		$permits = $this->User->Permit->find('list');
		$this->set(compact('countries', 'cities', 'permits'));
		$this->set('countries', $this->User->City->Country->find('list'));
	}




这是email.php



class EmailConfig {

	public $default = array(
		'transport' => 'Mail',
		'from' => 'dafhermcslama@gmail.com',
		//'charset' => 'utf-8',
		//'headerCharset' => 'utf-8',
	);




这是php.ini:



[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = smtp.gmail.com
; http://php.net/smtp-port
smtp_port = 25

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = dafhermcslama@gmail.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =
;sendmail_path="C:\wamp\sendmail\sendmail.exe -t -i"

;sendmail_path = "\"C:\wamp\sendmail\sendmail.exe\" -t"




这是View / Email / html / signup.ctp



<p>
	<strong>Bonjour <?php echo $username; ?></strong>
</p>
<p>
	To Activate your profile clic here : 
</p>
<p>
	<?php echo $this->Html->link('Activate', $this->Html->url($link, true)); ?>
</p>
&#13;
&#13;
&#13;

但是当我注册时,这是消息错误:

&#13;
&#13;
mail(): SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. p1sm178880wib.23 - gsmtp
Error: An Internal Error Has Occurred.
&#13;
&#13;
&#13;

我做什么?

1 个答案:

答案 0 :(得分:0)

email.php配置文件中缺少一些选项。对于gmail,它应该看起来像:

public $default = array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => 'my@gmail.com',
    'password' => 'secret',
    'transport' => 'Smtp'
);

如果您使用CakePHP 2.3.x +:或

public $default = array(
    'host' => 'smtp.gmail.com',
    'port' => 465,
    'username' => 'my@gmail.com',
    'password' => 'secret',
    'transport' => 'Smtp',
    'tls' => true
}

您可以在CakeEmail documentation找到更多详细信息。