我已经在公共函数add((中编写了生成电子邮件功能的文章。我的想法是将我的发送给已注册的用户。我的添加功能如下所示。添加功能工作正常,但邮件生成错误显示未知电子邮件配置"gmail"。请帮忙。app/Controller/UsersController.php
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$data[] = $this->request->data;
foreach($data as $row){
$email_name = $row['User']['username'];
$password = $row['User']['password'];
}
$data = array();
$subject = "Visualization Tool Login credentials";
// From
$header="manasasirsi17@gmail.com";
// Your message
$message="welcome userrn";
$message.="Thank You for Registeringrn";
$message.="your login details are as given belowrn";
$message.="username:$email_namern";
$message.="password:$passwordrn";
App::uses('CakeEmail', 'Network/Email');
$smtp = new CakeEmail('smtp');
$smtp->from(array($header => $header));
$smtp->to($email_name);
$smtp->subject($subject);
$smtp->emailFormat('html');
$Email->send($message);
$this->Session->setFlash(__('Login Details are sent to You via Email.'));
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('controller' => 'Users', 'action' => 'login'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
}
应用程序/配置/电子邮件.php
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => 'you@localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 25,
'timeout' => 30,
'username' => 'manasasirsi17@gmail.com',
'password' => 'secure',
'client' => null,
'log' => false
);
public $fast = array(
'from' => 'you@localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'Smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => true,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
您尚未在EmailConfig
类中定义gmail
电子邮件配置。这一行:-
$Email = new CakeEmail('gmail');
应该从你的代码的外观来看:-
$Email = new CakeEmail('smtp');
传递给CakeEmail
的参数确定要使用的EmailConfig
中定义的配置中的哪一个。如果要gmail
传递它,则需要将$gmail
属性添加到包含配置的EmailConfig
类中。