This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

nealh
nealh

Hi I'm having a problem sending emails. I have tried the email plugin and placing a function into the code section of the editor, but I still do not receive any emails. I can send a test message from the settings/Mail Templates area and this comes through fine.

I have the following code as the email function :

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

    // Form Validation
    $validator = Validator::make(
        [
            'name' => $name,
            'email' => $email,
            'subject' => $subject,
            'body' => $body,
        ],
        [
            'name' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'body' => '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','body');
    Mail::sendTo($to, 'nealh.website::mail.contactform', $params);
    return true;
}

And this code for the form:

    <form class="form-horizontal"id="form" name="contact-form" data-request="onSend()" data-request-success="alert('Message Sent')">
                    <div class="row">
                        <div class="col-md-6 no-padding">
                            <div class="col-md-12 required">
                                <input type="text" class="subfont" value=""  name="name" placeholder="Enter name" required>
                            </div>
                            <div class="col-md-12 required">
                                <input type="email" class="subfont" value="" name="email" placeholder="Enter Email" required>
                            </div>
                            <div class="col-md-12 required">
                                <input type="text" class="subfont" value="" name="subject" placeholder="Enter Subject" required>
                            </div>
                        </div>
                        <div class="col-md-6 col-xs-12 required">
                            <textarea class="form-control subfont" value="" name="body" placeholder="Enter Message" cols="40" required></textarea>
                        </div>
                    </div>

                   <div class="row">
                        <div class="col-md-12 col-xs-12">
                            <button type="submit" class="btn dark pull-right">Send</button?
                        </div>
                    </div>

                </form>

I'm new to October CMS so not sure if I am missing something simple.

Any help appreciated. Thanks NealH

Last updated

Cpt.Meatball
Cpt.Meatball

Does it send if you hardcode the $to variable? So instead of getting it from your model, you just put in your email address.

pratyushpundir6424
pratyushpundir6424

Yeah, I agree with what Cpt.Meatball says. Looks like the $to variable might not be what you're expecting it to be. Why don't you dd() or log the value to see what it is and then take it from there. Worst case you can just fallback to a hardcoded email address (I know not the best way to do it).

pramit.acharya2721921
pramit.acharya2721921

here my code for server side validation and sending mail. display error as undefined index. I jave seen tutorials as i understood from tutorials i have done but error appears. i want to display error of specific field as it validate all the fields. as soon as it identifies the error, specific field of error must appeared. php code public function onSubmit() { $all=[]; $validator=Validator::make($all, [ 'name'=>Input::get('name'), 'email'=>Input::get('email'), 'password'=>Input::get('password'), 'subject'=>Input::get('subject'), 'message'=>Input::get('message')], [ 'name'=>'required', 'email'=>'required|email|unique:users', 'password'=>'required|min:15' 'subject'=>'required|min:20' 'message'=>'required|min:100000' ]); if($all->fails()) { foreach ($all->messages('

  • '+$all+'is wrong. Do it again'+'
  • ')) { return Redirect::to('Drop your Message'); } } else { Mail::send('codesastra.com::mail.message',$all,function($message){ $message->to('admin@domain.tld','Admin Person'); $message->subject('new website'); $message->message('hfahfhahfa'); }); } }

    html code for contact:

    {% for error in error.all() %}
    {{error}}
    {% endfor %}

    pramit.acharya2721921
    pramit.acharya2721921

    i am just a beginner. i rarely watch tutorials so i don't have knowledge to much

    1-5 of 5

    You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.