OctoberCMS邮政表格

时间:2015-04-02 09:52:02

标签: php ajax laravel sendmail octobercms

我的表格:

<form id="main-contact-form" name="contact-form" ata-request="onSend" data-request-success="alert('Message Sent')">

我似乎无法找到一张表格;我在哪里放这个文件?我编辑哪个文件使其将表单数据字段发送到我的电子邮件?我已经设置了后端邮件设置:

function onSend()
{
    // Collect input
    $name = post('name');
    $email = post('email');
    $message = post('message');


    // Submit form
    $to = System\Models\MailSettings::get('sender_email');
    $params = compact('name','email');
    Mail::sendTo($to, 'temp.website::mail.newrequest', $params);
    return true;
}

3 个答案:

答案 0 :(得分:6)

请参阅文档:Plugin Components

您可以创建一个组件(SomeForm.php

<?php namespace My\Plugin\Components;

use Cms\Classes\ComponentBase;

class SomeForm extends ComponentBase
{
    public function componentDetails()
    {
        return [
            'name'        => 'Form',
            'description' => 'Some form'
        ];
    }

    public function onSend()
    {
        // Collect input
        $name = post('name');
        $email = post('email');
        $message = post('message');

        // Submit form
        $to = System\Models\MailSettings::get('sender_email');
        $params = compact('name','email');
        Mail::sendTo($to, 'temp.website::mail.newrequest', $params);
        return true;
    }
}

然后为其创建一个视图(例如default.htm

<form id="main-contact-form" name="contact-form" data-request="{{ __SELF__ }}::onSend" data-request-success="alert('Message Sent')">
    ...
</form>

页面/布局中的用法:

[someForm]
==
{% component "someForm" %}

答案 1 :(得分:2)

您转到后端的CMS部分并将其粘贴到default.htm布局的“代码”部分。我已经在OctoberCMS.com论坛上回答了这个问题。你可以阅读它here。确保您使用此格式的任何形式都有data-request="onSend"否则它将无效。这就是它最终的样子......

enter image description here

答案 2 :(得分:2)

您可以在组件partials目录,Theme的部分目录中添加Form的HTML,或者直接将其添加到任何页面/布局。这并不重要。

详细了解如何加入Partials

{% partial "contact-form.htm" %} 

或者

{% partial __SELF__ ~ "::contact-form.htm" %} // reference to component's partial

十月的AJAX框架需要使用JavaScript API或数据属性。您在示例中如何做到这一点很好但忘记在onSend Handler之前添加Component的名称

data-request="SendEmails::onSend" 

如果SendEmails =页面中给出的组件名称或别名,如果表单位于组件的部分名称中,请使用{{ __SELF__ }}::onSend

或使用JavaScript API,只需:

$.request('onSend', {
    data:{email:email, message:message, name:name},
    success: function (data) {
      //
     },
    error:function(e){
      //
    }
 });

然后在处理请求的组件中创建一个函数onSend

<?php namespace AuthorName\PluginName\Components;


use Cms\Classes\ComponentBase;
use Mail;
use Url;
use Input;
use Request;
use Response;
use ApplicationException;
use Validator;
use ValidationException;

class SendEmails extends ComponentBase
{

   public function onSend()
    {
        if (Request::ajax()) {

            try {

                $data = post();

                // Quick Validation rules for E-mail, Name & Message
                if (!array_key_exists('email', $data)) {
                    $data['email'] = post('email');
                }
                if (!array_key_exists('norad', $data)) {
                    $data['message'] = post('message');
                }
                if (!array_key_exists('name', $data)) {
                    $data['name'] = post('name');
                }    

                $rules = [
                    'email' => 'required|email|between:6,255',
                    'name' => 'required|between:4,255'
                    //..
                ];

                $validation = Validator::make($data, $rules);
                if ($validation->fails()) {
                    throw new ValidationException($validation);
                }

                // Check if E-mail Template Exists @ "author.plugin::mail.templatename"

                if (View::exists("author.plugin::mail.templatename")) {

                    Mail::send("author.plugin::mail.templatename", $data, function ($message)  {
                        $message->from('noreply@yourdomain.com', 'Site Name');
                        $message->to($data['email'], $data['name']);
                        $message->subject('Subject here..');

                    });

                    // Handle Erros
                    if (count(Mail::failures()) > 0) {
                        echo "Failed to send Mail "; // Handle Failure
                    } else {
                        // Mail sent
                        echo "Mail Sent!"; // Handle Success
                    }

                }

            } catch (Exception $ex) {

                throw $ex;
            }
        }
    }

}