【问题标题】:Cakephp email functionality giving Unknown email configuration "gmail"Cakephp 电子邮件功能提供未知电子邮件配置“gmail”
【发布时间】:2017-10-25 22:05:53
【问题描述】:

我在公共函数 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 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.'));
        }
      }
    }
 }

app/Config/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',
    );

}

【问题讨论】:

    标签: email cakephp


    【解决方案1】:

    您尚未在 EmailConfig 类中定义 gmail 电子邮件配置。这一行:-

    $Email = new CakeEmail('gmail');
    

    应该来自你的代码:-

    $Email = new CakeEmail('smtp');
    

    您传递给CakeEmail 的参数决定了您要使用EmailConfig 中定义的哪些配置。如果您想将gmail 传递给它,则需要将$gmail 属性添加到包含配置的EmailConfig 类中。

    【讨论】:

    • 我按照你的建议编辑了我的代码。但是它给出了错误说 stream_socket_client(): SSL operation failed with code 1. OpenSSL 错误消息:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol stream_socket_client (): 无法启用加密流_socket_client(): 无法连接到 ssl://smtp.gmail.com:25 (未知错误)
    • 由于该错误,我将端口更改为 465 。现在错误显示
    • @Manasa 这似乎是一个单独的问题。通过更改您的代码以使用我的答案,CakePHP 现在正在使用正确的配置,但您的配置似乎不正确。这可能会有所帮助:stackoverflow.com/questions/7915318/…
    猜你喜欢
    • 2014-07-07
    • 2021-08-10
    • 1970-01-01
    • 2014-06-23
    • 2016-03-10
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 2015-08-22
    相关资源
    最近更新 更多