我已经编写了在公共函数add()中生成电子邮件功能。 我的想法是将我发送给已注册的用户。 我的添加功能看起来像这样。 添加功能工作正常,但邮件生成给出错误说未知的电子邮件配置" gmail"。请帮忙。 应用程序/控制器/ 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 user\r\n";
$message.="Thank You for Registering\r\n";
$message.="your login details are as given below\r\n";
$message.="username:$email_name\r\n";
$message.="password:$password\r\n";
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.'));
}
}
}
}
应用程序/配置/ email.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',
);
}
答案 0 :(得分:2)
您尚未在gmail
课程中定义EmailConfig
电子邮件配置。这一行: -
$Email = new CakeEmail('gmail');
应该来自您的代码: -
$Email = new CakeEmail('smtp');
您传递给CakeEmail
的参数决定了您要使用的EmailConfig
中定义的配置。如果您想传递它gmail
,则需要向包含配置的$gmail
类添加EmailConfig
属性。