0

i can send the email without any problem, but form still like not submitted (refresh) or redirect to another page>>>>

this is my function to send an email:

function sendEmail(Request $request, $sendTo) {
            $request->validate([
                'name' => ['required', 'min:3'],
                'email' => ['required', 'email'],
                'subject' => ['required', 'min:3'],
                'message' => ['required', 'min:3', 'max:300'],
            ]);

            try {

                $data = (object) $request->all();
                event(new ContactEvent($data, $sendTo));
                redirect()->back()->with('msg', "Email has sent successfully");
            }catch(\Exception $e) {
                redirect()->back()->with('msg', "Email not sent");
            }

here the event:

class ContactEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */

     public $data;
     public $sendTo;

    public function __construct($data, $sendTo)
    {
        $this->data = $data;
        $this->data = $sendTo;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }

and the listener:

class ContactListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */

    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\ContactEvent  $event
     * @return void
     */
    public function handle(ContactEvent $event)
    {
       Mail::to($event->sendTo)->send(new ContactMail($event->data));
    }

and the email class:

class ContactMail extends Mailable implements ShouldQueue
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject($this->data->subject)->view('email.contact');
    }
}

and the form to send an email:

<form action="{{ route('send-email', $info->email) }}" method="post" class="php-email-form">
                @csrf
              <div class="form-row">
                <div class="form-group col-md-12">
                  <label for="name">Your Name</label>
                  <input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
                  <div class="validate"></div>
                </div>
                <div class="form-group col-md-12">
                  <label for="name">Your Email</label>
                  <input type="email" class="form-control" name="email" id="email" data-rule="email" data-msg="Please enter a valid email" />
                  <div class="validate"></div>
                </div>
              </div>
              <div class="form-group">
                <label for="name">Subject</label>
                <input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
                <div class="validate"></div>
              </div>
              <div class="form-group">
                <label for="name">Message</label>
                <textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please write something for us"></textarea>
              </div>

              <div class="mb-3">
                <div class="loading">Loading</div>
                <div class="error-message"></div>
                <div class="sent-message">Your message has been sent. Thank you!</div>
              </div>

              <div class="text-center"><input type="submit" class="btn btn-dark rounded" value="Send"></div>
            </form>

1 Answer 1

0

You are missing the return like so:

return redirect()->back()->with('msg', "Email has sent successfully");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, but still same problem
can you delete all codes except this code here, just for the process of elimination.
it is a big project, and i am 100% sure only above code related with the mail send process
i had answer like this from previous post , he said "Redirect to the GET version of the page after mail has successfully been sent to clear the form fields and make it possible to refresh the page without sending the mail again", but I didn't understand how can I implement that
then there must be wrong somewhere else that you did not include on your post, because you said that the email is sending but it is not redirecting back which it should because your redirecting code is right under your emailing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.