我试图弄清楚如何从我的网站发送一个简单的电子邮件。我有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('[email protected]' => 'My Site'))
->to('[email protected]')
->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', '[email protected]');
$Email = new CakeEmail();
$Email->from(array('[email protected]' => 'My Site'))
->to('[email protected]')
->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 Error.[SocketException] Could not send email: unknownRequest URL: stronacontactStack Trace:0 libCakeNetworkEmailMailTransport(52): libCakeNetworkEmailMailTransport.php(52): 錯誤。SocketException] Could not send email: unknownRequest URL: stronacontactStack Trace:0 libCakeNetworkEmailMailTransport.php(52): MailTransport->_mail('szymonjozefowic...', '=?UTF-8?B?V2lhZ...', '\nSzymon J\xC3\xB3...', 'From: My Site <...', NULL)1 libCakeNetworkEmailCakeEmail.php(1183): MailTransport->send(Object(CakeEmail))2 appControllerStronaController.php(649): CakeEmail->send()3 [内部函数]。StronaController->联系()4 libCakeControllerController.php(499): ReflectionMethod->invokeArgs(Object(StronaController), Array)5 libCakeRoutingDispatcher.php(193): Controller->invokeAction(Object(CakeRequest))6 libCakeRoutingDispatcher.php(167): Dispatcher->_invoke(Object(StronaController), Object(CakeRequest))7 appwebrootindex.php(117): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))8 {main}。那是什么意思?
在你的代码中,你没有设置消息体,也没有发送消息。message()将返回一个生成的消息. 这就是为什么你不能叫 message($message)->send()
之后。
典型的发送邮件的方式是使用模板和查看变量。
$Email
->template('welcome', 'fancy')
->viewVars(array('name' => $data['Contact']['name'], 'description' => $data['Contact']['description']));
->from(array('[email protected]' => 'My Site'))
->to('[email protected]')
->subject($data['Contact']['title'])
->send();