【问题标题】:My component CakeEmail don't send after submit but also don't give back error我的组件 CakeEmail 提交后不发送但也不返回错误
【发布时间】:2020-09-06 10:11:54
【问题描述】:

我试图弄清楚如何从我的网站发送一封简单的电子邮件。我有 CakePHP 2.10.19 版。我在文档中阅读了有关 CakeEmail 的信息,并像他们说的那样配置控制器。我的控制器和表单如下所示:

if($this->request->is('post')){
    $data = $this->data;

    $message = $data['Contact']['name'].'<br><br>'.$data['Contact']['description'].'<br><br>'.$data['Contact']['email'];
    $Email = new CakeEmail();
    $Email->from(array('office@kogni-fit.at' => 'My Site'))
        ->to('szymonjozefowicz1992@gmail.com')
        ->subject($data['Contact']['title'])
        ->message($message);

    $this->Session->setFlash('Nachricht gesendet');
    $this->redirect(array('action' => 'contact/'));
}

<form method="POST" action="" class="contact_form">
    <div class="form-group">
        <input type="text" class="form-control" id="name" name="data[Contact][name]" value="<?php echo $el['Contact']['name']; ?>" placeholder="Dein Name"/>
        <div class="error error_name"></div>
    </div>
    <div class="form-group">
        <input type="email" class="form-control" id="mail" aria-describedby="emailHelp" name="data[Contact][email]" value="<?php echo $el['Contact']['email']; ?>" placeholder="Deine E-Mail-Adresse"/>
        <div class="error error_email"></div>
    </div>
    <div class="form-group">
        <input type="text" class="form-control" id="title" name="data[Contact][title]" value="<?php echo $el['Contact']['title']; ?>" placeholder="Betreff"/>
        <div class="error error_title"></div>
    </div>
    <div class="form-group">
        <textarea class="form-control" id="description" name="data[Contact][description]" placeholder="Deine Nachricht" rows="10"><?php echo $el['Contact']['description']; ?></textarea>
        <div class="error error_description"></div>
    </div>
    <input type="submit" id="send" class="btn btn-primary" value="Senden">
</form>

我尝试查看日志以查找错误,但没有任何内容。但我的 gmail 中也没有发送电子邮件。

根据您的建议,我更改了代码。我编辑了我的控制器,现在它看起来像这样

if($this->request->is('post')){
    $data = $this->data;
    ini_set("SMTP","serwer1155486.home.pl");

    ini_set("smtp_port","465");

    ini_set('sendmail_from', 'office@kogni-fit.at');

    $Email = new CakeEmail();
    $Email->from(array('office@kogni-fit.at' => 'My Site'))
        ->to('szymonjozefowicz1992@gmail.com')
        ->subject($data['Contact']['title'])
        ->template('mail', 'default')
        ->viewVars(array('name' => $data['Contact']['name'], 'description' => $data['Contact']['description']))
        ->send();

    $this->Session->setFlash('Nachricht gesendet');
    $this->redirect(array('action' => 'contact/'));
}

但是在 sumit 之后是一个错误:
2020-05-21 19:26:19 错误:[SocketException] 无法发送电子邮件:未知 请求网址:/stroma/contact 堆栈跟踪: 0 /lib/Cake/Network/Email/MailTransport.php(52): MailTransport->_mail('szymonjozefowic...', '=?UTF-8?B?V2lhZ...', '\nSzymon J\xC3 \xB3...', '来自:我的网站 <... null mailtransport->send(对象(CakeEmail)) 2 /app/Controller/StronaController.php(649): CakeEmail->send() 3【内部函数】:StronaController->contact() 4 /lib/Cake/Controller/Controller.php(499): ReflectionMethod->invokeArgs(Object(StronaController), Array) 5 /lib/Cake/Routing/Dispatcher.php(193):控制器->invokeAction(Object(CakeRequest)) 6 /lib/Cake/Routing/Dispatcher.php(167): Dispatcher->_invoke(Object(StronaController), Object(CakeRequest)) 7 /app/webroot/index.php(117): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse)) 8 {主}
什么意思?

【问题讨论】:

  • 我想你忘了$Email-&gt;send()
  • 是的。我没有添加 $Email->send() 并且它必须添加,但是在添加它之后我得到了这个错误:调用数组上的成员函数 send()。还是不行。
  • 您确定您的 SMTP 服务器不需要身份验证吗?您还应该将配置放入app/config/email.php,而不是在控制器中硬编码。
  • 你说得对。它需要身份验证,并且配置不应在控制器中。我会改的。

标签: php cakephp-2.0


【解决方案1】:

在您的代码中,您没有设置消息正文,也没有发送消息。 message() will return a generated message。这就是为什么你不能在之后打电话给message($message)-&gt;send()

发送邮件的典型方式是使用模板并查看变量。

    $Email
        ->template('welcome', 'fancy')
        ->viewVars(array('name' => $data['Contact']['name'], 'description' => $data['Contact']['description']));
        ->from(array('office@kogni-fit.at' => 'My Site'))
        ->to('szymonjozefowicz1992@gmail.com')
        ->subject($data['Contact']['title'])
        ->send();

【讨论】:

  • 好的。我想我更接近了。还是有点问题。我添加了模板和 viewVars,创建了我的模板并采用了默认布局。我会将此代码添加到我上面的内容中。现在我有一个奇怪的错误。
  • 谢谢。我正在发送电子邮件。这很有帮助。
  • 可能我忽略了它
猜你喜欢
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 2013-03-21
  • 2016-11-18
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多