代码之家  ›  专栏  ›  技术社区  ›  Salaktarus

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

  •  0
  • Salaktarus  · 技术社区  · 11 年前

    你好,我想在注册后发送邮件,但是 我有一个问题,这是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>

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

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

    我做什么?

    1 回复  |  直到 11 年前
        1
  •  0
  •   marian0 Jorge Mora Pérez    11 年前

    您的中缺少一些选项 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 .