Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email using Laravel?

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('[email protected]');
        $message->to('[email protected]', '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?

like image 972
Mohamed Elbiheiry Avatar asked Mar 06 '16 18:03

Mohamed Elbiheiry


1 Answers

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' => '[email protected]',
            'admin'    => '[email protected]',
            '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('[email protected]', '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.

like image 142
Maytham Avatar answered Oct 27 '22 06:10

Maytham