我的组件 CakeEmail 提交后不发送但也不返回错误
My component CakeEmail don't send after submit but also don't give back error
我想知道如何从我的网站发送一封简单的电子邮件。我有 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/'));
}
但是提交后报错:
2020-05-21 19:26:19 错误:[SocketException] 无法发送电子邮件:未知
请求 URL:/strona/contact
堆栈跟踪:
0 /lib/Cake/Network/Email/MailTransport.php(52): MailTransport->_mail('szymonjozefowic...', '=?UTF-8?B?V2lhZ...', '\nSzymon J\xC3\xB3 ...', 'From: My Site <...', 空)
1 /lib/Cake/Network/Email/CakeEmail.php(1183): MailTransport->send(Object(CakeEmail))
2 /app/Controller/StronaController.php(649): CakeEmail->发送()
3 [内部函数]: StronaController->contact()
4 /lib/Cake/Controller/Controller.php(499): ReflectionMethod->invokeArgs(对象(StronaController), 数组)
5 /lib/Cake/Routing/Dispatcher.php(193): Controller->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 {主要}
这是什么意思?
在您的代码中,您没有设置邮件正文,也没有发送邮件。
message() will return a generated message。这就是为什么你之后不能调用 message($message)->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();
我想知道如何从我的网站发送一封简单的电子邮件。我有 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/'));
}
但是提交后报错:
2020-05-21 19:26:19 错误:[SocketException] 无法发送电子邮件:未知
请求 URL:/strona/contact
堆栈跟踪:
0 /lib/Cake/Network/Email/MailTransport.php(52): MailTransport->_mail('szymonjozefowic...', '=?UTF-8?B?V2lhZ...', '\nSzymon J\xC3\xB3 ...', 'From: My Site <...', 空)
1 /lib/Cake/Network/Email/CakeEmail.php(1183): MailTransport->send(Object(CakeEmail))
2 /app/Controller/StronaController.php(649): CakeEmail->发送()
3 [内部函数]: StronaController->contact()
4 /lib/Cake/Controller/Controller.php(499): ReflectionMethod->invokeArgs(对象(StronaController), 数组)
5 /lib/Cake/Routing/Dispatcher.php(193): Controller->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 {主要}
这是什么意思?
在您的代码中,您没有设置邮件正文,也没有发送邮件。
message() will return a generated message。这就是为什么你之后不能调用 message($message)->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();