PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 20, 2022

[FIXED] How to send email using Laravel?

 February 20, 2022     laravel, laravel-5, php     No comments   

Issue

I'm trying to send an email but I can't move anymore and that's my view:

<h1>Contact TODOParrot</h1>
<form action="contact" method="post">

<div class="form-group">
 <label>Your First Name</label>
 <input type="text" name="Fname" placeholder="Your First Name" />
</div>

<div class="form-group">
  <label>Your Last Name</label>
  <input type="text" name="Lname" placeholder="Your Last Name" />
</div>

<div class="form-group">
<label>Your Email</label>
  <input type="email" name="Email" placeholder="Your Email" />
</div>
<div class="form-group">
<label>Your Phone Number</label>
  <input type="text" name="Phone" placeholder="Your Phone" />
</div>
<div class="form-group">
<label>Your Order</label>
  <input type="text" name="Order" placeholder="Your Order" />
</div>

<div class="form-group">
    <button class="btn btn-default" name="Submit" type="Submit">Send Order</button>
</div>
</form>

and that is my controller :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;  
use App\Http\Requests\ContactFormRequest;
    
class aboutController extends Controller
{
    //
    public function create()
    {
        return view('about.contact');
    }

    public function store(ContactFormRequest $request)
    {
        \Mail::send('about.contact',
        array(
            'Fname' => $request->get('Fname'),
            'Lname' => $request->get('Lname'),
            'Email' => $request->get('Email'),
            'Phone' => $request->get('Phone'),
            'Order' => $request->get('Order')
        ), function($message)
    {
        $message->from('mohamedsasa201042@yahoo.com');
        $message->to('elbiheiry2@gmail.com', 'elbiheiry')->subject('TODOParrot Feedback');
    });

        return \Redirect::route('contact')
      ->with('message', 'Thanks for contacting us!');
    }
}

And that's my route:

Route::get('contact', 
  ['as' => 'contact', 'uses' => 'AboutController@create']);
Route::post('contact', 
  ['as' => 'contact', 'uses' => 'AboutController@store']);

And that's the configuration in the .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=ssl

And I removed the name and password in the question when I press send it gives me 'Forbidden' as a message.

Can anyone help?


Solution

After chatting while with OP, here is the answer.

The main problem:

Your ContactFormRequest.php has the following rules function:

public function rules()
    {
        return [
        'name'    => 'required',
        'email'   => 'required|email',
        'message' => 'required',
        ];
    }

But your form does not have name and messages, so you need to delete not existing elements or modify them if required, for my testing purpose I did only kept email:

public function rules()
    {
        return [
            'Email' => 'required|email',
        ];
    }

And it is a good practice to keep name conventions like if you use Email with capital E than use Email every where.

Therefore the form was never submitted to be send.

I suggest also you structure your store function which I have did and test and it works, you can modified it to fit your requirement:

$data = [
            'no-reply' => 'contact-from-web@nomail.com',
            'admin'    => 'mohamedsasa201042@yahoo.com',
            'Fname'    => $request->get('Fname'),
            'Lname'    => $request->get('Lname'),
            'Email'    => $request->get('Email'),
            'Phone'    => $request->get('Phone'),
            'Order'    => $request->get('Order'),
        ];

        \Mail::send('about.contact', ['data' => $data],
            function ($message) use ($data)
            {
                $message
                    ->from($data['no-reply'])
                    ->to($data['admin'])->subject('Some body wrote to you online')
                    ->to($data['Email'])->subject('Your submitted information')
                    ->to('elbiheiry2@gmail.com', 'elbiheiry')->subject('Feedback');
            });

and it should works,

I have test it only with Mandrill API email service, but you can give it a try with SMTP or API, it is up to you.

If you want to make an email confirmation, you need to create email confirmation view forward your data to it like following:

\Mail::send('about.emailconfirmation', ['data' => $data],

and your view could looks like this:

<tr>
    <td>
        <h1>Contact form</h1>
        <p>Dear {{ $data['Fname'] }},</p>
        <p>Thank you for contacting me.</p>
        <p>I will respond to your inquiry as quickly as possible.</p>
        <hr/>
        <p><b>Provided email</b></p>
        <p>Email: {{ $data['Email'] }},</p>
    </td>
</tr>

This is only example but you can further modify it.



Answered By - Maytham
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,213,629

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing