使用CakeEmail发送电子邮件 [英] Sending email with CakeEmail
问题描述
在CakePHP应用程序中,我有一个电子邮件表格,当单击电子邮件超链接时,该表格会打开。然后如何传递表单中的数据,以便可以使用CakeEmail发送数据?抱歉,我已经尝试了很长时间,并仔细阅读了 http://book.cakephp.org/2.0/en/core-utility-libraries/email.html ,仍然无法弄清。
In my CakePHP application I have an email form I have made myself that opens when an email hyperlink is clicked. How do I then pass the data from the form so that it can be sent using CakeEmail? Sorry, I've tried this for ages and checked through all the documentation on http://book.cakephp.org/2.0/en/core-utility-libraries/email.html, still can't figure it out.
这是我的代码...
email.ctp
<?php $this->Html->addCrumb('New Email', '#'); ?>
<div id="email_page" class="span12">
<div class="row">
<?php
echo $this->Form->create('Email', array('controller'=>'person', 'action'=>'email_send'));
echo $this->Form->input('email', array('class'=>'email_form','label'=>'To: ','value'=>$email['Person']['primEmail']));
echo $this->Form->input('subject', array('class'=>'email_form','label'=>'Subject: '));
echo $this->Form->input('message', array('class'=>'email_form email_body', 'type'=>'textarea','label'=>'Message: '));
echo $this->Form->end('Send', array('class'=>'pull-right'));
?>
</div>
</div>
email_send.php
<?php
$email = new CakeEmail('default');
$email->to('email');
$email->subject('subject');
$email->send('message');
?>
感谢您的帮助!
推荐答案
表单数据将在Controller中的 $ this-> request-> data
(可写)或 $ this->数据
(可读)。提交表单后,您的表单称为电子邮件。所有数据将在 $ this-> request-> data ['Email']
下可用。
Form data will be available in the Controller in $this->request->data
(writable) or $this->data
(readable). As your form is called Email all data will be available under $this->request->data['Email']
after the form is submitted.
我不确定为什么您会在 email_send.php
中使用电子邮件代码,而不使用Controller方法。表单期望在PersonsController中使用 email_send
方法,因为表单操作设置为 / persons / email_send
。所以我将电子邮件代码放在 PersonsController.php
的 email_send()
内。
I'm not sure why you would have the email code in email_send.php
instead of using a Controller method. The form expects an email_send
method present in the PersonsController, as the form action is set to /persons/email_send
. So I would place the email code inside email_send()
in PersonsController.php
.
所以:
<?php
public function email_send() {
$email = new CakeEmail('default');
$email->to($this->request->data['Email']['email']);
$email->subject($this->request->data['Email']['subject']);
$email->send($this->request->data['Email']['message']);
}
?>
当然,当所有这些都起作用时,您应该设置正确的验证并检查 $ this-> request->数据是否
填充有相关数据。
Of course, when all this is working, you should set up proper validation and check if $this->request->data
is populated with the relevant data.
这篇关于使用CakeEmail发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!