Back to Multi Support

Mohsin
Mohsin

conprefor16116 said:

Hi. Thanks for the appreciation. I would like to know, have you solved the issue yet? If not I can use this template to build the contact form again, zip it and send back to you with working code. Let me know.

Good evening Mohsin,

Yes, i've fixed it with your advisement. I got to thinking. Is it possible to send attachments?

I mean case if i add in the form <input type="file" ...>. In this case how it is possible check file properties (e.g. size, format, etc.) and attach to email.

I just think (imagine) about this, i don't have this task.

Yep I’m sure it’s possible. But I haven’t experimented with <input type=“file”>. I instead use this as it was far more simpler to use.

stephanec19464
stephanec19464

just looked over the whole thread. thank you a lot for your help @mohsin ! everything work perfect ! as for the input file, if you can help me on this, it would be really appreciated. I'm currently working on a job application form and people need to add their resume to the email form. I checked over the uploader plugin but not quite sure to understand how to implement it in a contact form.

thank you again for your help.

Mohsin
Mohsin

Your application form is written in a plugin I suppose?

So let me assume that you have a model Applicant to store the data of each person who applies.

  1. Add this to the model i.e. Applicant.php
    
    public $attachOne = [
    ‘resume' => ['System\Models\File']
    ];
  2. In the page where you show the application form drag and drop the fileUploader component. Set the allowed file type extensions as .pdf,.doc,.docx and mark deferred binding as true.
  3. In the same page, click the code tab and add this…
    function onInit()
    {
    $this->fileUploader->bindModel(‘resume', new Applicant);
    }
  4. In the partial where you typed the applicant form’s fields, change
    and
    to {{ form_open() }} and {{ form_close() }} respectively.
  5. In the ajax handler which performs the form submission, instead of calling save() call save(null, post('_session_key')).
stephanec19464
stephanec19464

currently the form is a basic html contact form sending an email to HR department with some basic infos (name, phone, email...etc...) and this not stored in database and have no model for that part. i am trying to add a file field to the form and attach this file to the email sent. that's maybe basic, but can't figure out to make it work in October.

Mohsin
Mohsin

stephanec19464 said:

currently the form is a basic html contact form sending an email to HR department with some basic infos (name, phone, email...etc...) and this not stored in database and have no model for that part. i am trying to add a file field to the form and attach this file to the email sent. that's maybe basic, but can't figure out to make it work in October.

You don’t even need an uploader if you are doing it this way, since you are not storing anything within the backend. Personally if I were to write this, I would not only save it to the backend but also have a backend list to display all the submissions. I would do this by making a plugin called Resume or something like that to let people upload their resumes directly to my website. Anyway, since you have made it work directly as a form and want to email it directly to your email ID than storing it, here’s how to do it:

In your form, in the

parameters add enctype="multipart/form-data” and also add

  <div class="form-group">
      <input name="resume" type="file">
  </div>

In the function onSend() make the changes to add / modify these

   // Collect input
    $file = Input::file('resume');
    ...
    // Validation section
    ...
    $to = System\Models\MailSettings::get('sender_email');
    $data = file_get_contents($file -> getPathname());
    Mail::send('yourappname.website::mail.contactform', $params, function($message) use ($to, $data) {
        $message->to($to);
        $message->subject('Resume Application');
        $message->attachData($data, 'resume.pdf');

    });

As you can see, the sendTo is replaced by send for finer control of content i.e. it allows attachments. SendTo is mainly used for quick sending and for finer control it’s better to use send.

I would recommend that instead of using with parameters use the form_open and form_close helper instead.. So your form section will look like this…

{{ form_open({ request: 'onSend', method: 'post', files: 'true' }) }}
    <div class="form-group">
        <input type="text" name="name" class="form-control" placeholder="Name" required>
    </div>
    <div class="form-group">
        <input type="email" name="email" class="form-control" placeholder="Email" required>
    </div>
    <div class="form-group">
        <input type="text" name="subject" class="form-control" placeholder="Subject" required>
    </div>
    <div class="form-group">
        <textarea name="msg" class="form-control" rows="8" placeholder="Message" required></textarea>
    </div>
    <div class="form-group">
        {{ form_file('resume') }}
    </div>

    <button type="submit" class="btn btn-primary">Send Message</button>
{{ form_close() }}

Last updated

bishankb.kkaam20476
bishankb.kkaam20476

How send mail with attachment without saving to database?in october cms?

Mohsin
Mohsin

bishankb.kkaam20476 said:

How send mail with attachment without saving to database?in october cms?

See the comment just above your comment. The solution is starring you in the face with big round eyes like: Overly Attached Girlfriend Eyes

Last updated

bishankb.kkaam20476
bishankb.kkaam20476

@mohsin sir thank you i didn notice this ... But $data = file_get_contents($file -> getPathname()); gives error getPathname() is null

Mohsin
Mohsin

bishankb.kkaam20476 said:

@mohsin sir thank you i didn notice this ... But $data = file_get_contents($file -> getPathname()); gives error getPathname() is null

Did you create the filename like...

    <div class="form-group">
        {{ form_file('resume') }}
    </div>

and used form_open and form_close? Share your form's html. I think something may be wrong. Or first try dd($file); If that is null then the file object never reaches the post method from the form.

Last updated

shoes222236
shoes222236

Mohsin said:

Goto partials > contact.htm

in form add data-request="onSend" data-request-success="alert('We have recieved your email.. we\'ll get back to you shortly')" data-request-error=“alert(jqXHR.responseText);” instead of method="post" action="#"

Now goto layout -> default.htm and switch from markup to the code tab and paste this

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

   // Form Validation
   $validator = Validator::make(
       [
           'name' => $name,
           'email' => $email,
           'subject' => $subject,
           'msg' => $msg,
       ],
       [
           'name' => 'required',
           'email' => 'required|email',
           'subject' => 'required',
           'msg' => 'required',
       ]
   );

   if ($validator->fails())
   {
       $messages = $validator->messages();
       throw new ApplicationException($messages->first());
   }

   // All is well -- Submit form
   $to = System\Models\MailSettings::get('sender_email');
   $params = compact('name','email','subject','msg');
   Mail::sendTo($to, 'yourappname.website::mail.contactform', $params);
   return true;
}

Now goto Settings page from the nav menu and in Mail choose mail template. Create a new template with code as yourappname.website::mail.contactform

Fill the subject field as Message from YourAppName In the body fill

Name: {{name}}
Email: {{email}}
Subject: {{subject}}
Message: {{msg}}

It should look like this..

Contact Form Mail Template

Press create and close, and open the template you just created and send a test email. If the mail fails to send and gives a flash error you'll need to goto Mail Settings in the same section and fix the settings (I use Mailgun). Now if the mail test works, go try the live form in the website with random data. It should work fine!

Minor Update: I changed message into msg as the mail template gave an error on using the word message. Assume this is a reserved word and do not use it in mail templates.

Does my form name need to be named "contactform"? This code isn't working for me 100% yet. It sends me a message, but all of the post('name') etc is empty. As well, are there any script dependencies needed for this to work? If so, where do I call them from? I am using the Octaskin theme.

Last updated

Mohsin
Mohsin

shoes222236 said:

Mohsin said:

Goto partials > contact.htm

in form add data-request="onSend" data-request-success="alert('We have recieved your email.. we\'ll get back to you shortly')" data-request-error=“alert(jqXHR.responseText);” instead of method="post" action="#"

Now goto layout -> default.htm and switch from markup to the code tab and paste this

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

   // Form Validation
   $validator = Validator::make(
       [
           'name' => $name,
           'email' => $email,
           'subject' => $subject,
           'msg' => $msg,
       ],
       [
           'name' => 'required',
           'email' => 'required|email',
           'subject' => 'required',
           'msg' => 'required',
       ]
   );

   if ($validator->fails())
   {
       $messages = $validator->messages();
       throw new ApplicationException($messages->first());
   }

   // All is well -- Submit form
   $to = System\Models\MailSettings::get('sender_email');
   $params = compact('name','email','subject','msg');
   Mail::sendTo($to, 'yourappname.website::mail.contactform', $params);
   return true;
}

Now goto Settings page from the nav menu and in Mail choose mail template. Create a new template with code as yourappname.website::mail.contactform

Fill the subject field as Message from YourAppName In the body fill

Name: {{name}}
Email: {{email}}
Subject: {{subject}}
Message: {{msg}}

It should look like this..

Contact Form Mail Template

Press create and close, and open the template you just created and send a test email. If the mail fails to send and gives a flash error you'll need to goto Mail Settings in the same section and fix the settings (I use Mailgun). Now if the mail test works, go try the live form in the website with random data. It should work fine!

Minor Update: I changed message into msg as the mail template gave an error on using the word message. Assume this is a reserved word and do not use it in mail templates.

Does my form name need to be named "contactform"? This code isn't working for me 100% yet. It sends me a message, but all of the post('name') etc is empty. As well, are there any script dependencies needed for this to work? If so, where do I call them from? I am using the Octaskin theme.

This is because octoskin doesn’t use the same values for the inputs in it’s contact form. If you goto the form, and right click and inspect element on each field you can see something like:

<input type="text" class="validate" id="first_name”>

Find the same in the actual file in the theme and change this to

<input type="text" class="validate" name="first_name" id="first_name">

Do the same for all the remaining fields as well. Add the name=“SOME-VALUE” field to every input you want to collect. Now in the onSend function you collect the input like:

function onSend()
{
   // Collect input
   $first_name = post('SOME-VALUE’);
  …
  …

So basically you change the variables to the ones in your form. Edit the rest of the function to reflect the new variable names and it will work fine. The form name need not be contactform, it can be anything since the onSend function doesn’t look at the form name but only the input names.

Last updated

SGD
SGD

Creating a running form without a plugin in October is realy Horror....

i have make these Steps: setup php code in default.htm

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

// Form Validation
$validator = Validator::make(
    [
        'name' => $name,
        'email' => $email,
        'subject' => $subject,
        'msg' => $msg,
    ],
    [
        'name' => 'required',
        'email' => 'required|email',
        'subject' => 'required',
        'msg' => 'required',
    ]
);

if ($validator->fails())
{
    $messages = $validator->messages();
    throw new ApplicationException($messages->first());
}

// All is well -- Submit form
$to = System\Models\MailSettings::get('sender_email');
$params = compact('name','email','subject','msg');
Mail::sendTo($to, 'myapp.website::mail.contactform', $params);
return true;

}

  1. I have create a mail Template. and send a test Message - testmessage works !

  1. I create a partital with a form :

Fill the form with a Testmessage click on send -> Page loads more than a minute and then nothing happens .. Form not send messages !!!

Have someone a full running code i dont will use a plugin.

the form:

<form id="main-contact-form" name="contact-form" data-request="onSend" data-request-success="alert('We have recieved your email.. we\'ll get back to you shortly')" data-request-error=“alert(jqXHR.responseText);">

Last updated

Rsl sandoval
Rsl sandoval

hi.......

When I try to send test messages it gives me this error

escapeshellcmd() has been disabled for security reasons............. in the backend..

class System\Models\MailSettings no found on line 41...........cms/cache/97/34/contact.htm.php

Someone could help me with this error

tony_p4620282
tony_p4620282

has anyone managed to get octaskin contact form to work without creating a plugin I have tried Moshin's method but I get an error "AJAX handler 'onSend' was not found."

rob25798
rob25798

Hi,

I am new to OctoberCMS and am looking for some advice. I have my contact form setup and the ajax works fine, but when there is a validation error it ALWAYS uses a standard javascript alert to show the message. I can't find any way to suppress this default behaviour. I want to receive the ajax error text and show my own on-screen alerts.

Any help would be greatly appreciated!

esawi44269193
esawi44269193

Mohsin said:

Goto partials > contact.htm

in form add data-request="onSend" data-request-success="alert('We have recieved your email.. we\'ll get back to you shortly')" data-request-error=“alert(jqXHR.responseText);” instead of method="post" action="#"

Now goto layout -> default.htm and switch from markup to the code tab and paste this

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

   // Form Validation
   $validator = Validator::make(
       [
           'name' => $name,
           'email' => $email,
           'subject' => $subject,
           'msg' => $msg,
       ],
       [
           'name' => 'required',
           'email' => 'required|email',
           'subject' => 'required',
           'msg' => 'required',
       ]
   );

   if ($validator->fails())
   {
       $messages = $validator->messages();
       throw new ApplicationException($messages->first());
   }

   // All is well -- Submit form
   $to = System\Models\MailSettings::get('sender_email');
   $params = compact('name','email','subject','msg');
   Mail::sendTo($to, 'yourappname.website::mail.contactform', $params);
   return true;
}

Now goto Settings page from the nav menu and in Mail choose mail template. Create a new template with code as yourappname.website::mail.contactform

Fill the subject field as Message from YourAppName In the body fill

Name: {{name}}
Email: {{email}}
Subject: {{subject}}
Message: {{msg}}

It should look like this..

Contact Form Mail Template

Press create and close, and open the template you just created and send a test email. If the mail fails to send and gives a flash error you'll need to goto Mail Settings in the same section and fix the settings (I use Mailgun). Now if the mail test works, go try the live form in the website with random data. It should work fine!

Minor Update: I changed message into msg as the mail template gave an error on using the word message. Assume this is a reserved word and do not use it in mail templates.

Thanks for the answer. but what if i have two forms in the same page? i need to seperate "onSend" functions, how can i do that?

21-36 of 36