Mohamed Elbiheiry
Mohamed Elbiheiry

Reputation: 353

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?

Upvotes: 6

Views: 12239

Answers (5)

aakash rajput
aakash rajput

Reputation: 131

Here are three methods to send email in laravel 8.
First one is through our email id.
Second one is through Mailgun.
Third one is through SendinBlue.


# For smtp

# MAIL_MAILER=smtp
# MAIL_HOST=smtp.gmail.com
# MAIL_PORT=587
# [email protected]
# MAIL_PASSWORD=xxxxxxxxxxxxxxx
# MAIL_ENCRYPTION=tls
# [email protected]
# MAIL_FROM_NAME="${APP_NAME}"

# For Mailgun

# MAIL_MAILER=mailgun
# MAIL_HOST=smtp.mailgun.org
# MAIL_PORT=587
# MAIL_USERNAME=sandboxxxxxxxxxxxxxxxxxxxxxxxxx.mailgun.org
# MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# MAIL_ENCRYPTION=tls
# MAIL_FROM_ADDRESS=xxxxxxxxxxxxxxxxxxxxxxxxx
# MAIL_FROM_NAME="${APP_NAME}"
# MAILGUN_SECRET=API private key
# MAILGUN_DOMAIN=sandboxxxxxxxxxxxxxxxxxxxxx.mailgun.org

# For sendinblue
# MAIL_DRIVER=smtp
# MAIL_HOST=smtp-relay.sendinblue.com
# MAIL_PORT=587
# [email protected]
# MAIL_PASSWORD=xxxxxxxxxxxxxxxxxxxxxx
# MAIL_ENCRYPTION=tls
# [email protected]
# MAIL_FROM_NAME="${APP_NAME}"

Upvotes: 0

Balaji
Balaji

Reputation: 11017

Laravel app

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465          
MAIL_USERNAME=<<your email address>>
MAIL_PASSWORD=<<app password>>

MAIL_ENCRYPTION= ssl   

In your Controller Setting

faced

use Illuminate\Support\Facades\Mail;

to Send mail

$to_name = "RECEIVER_NAME";
$to_email = "[email protected]";
$data = array("name"=>"Cloudways (sender_name)", "body" => "A test mail");

Mail::send([], $data, function($message) use ($to_name, $to_email) {
$message->to($to_email, $to_name)
->subject("Laravel Test Mail");
$message->from("[email protected]","Test Mail");
});

Note: from mail and to mail

make sure enable SucureLess mail in gmail setting

Upvotes: 2

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

i had some similar problem and changed the MAIL_DRIVER = sendmail from smtp

Upvotes: 0

Maytham Fahmi
Maytham Fahmi

Reputation: 33437

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.

Upvotes: 5

Qazi
Qazi

Reputation: 5135

As per my understanding, I think you are missing authorize part in your ContactFormRequest Goto your this App/Http/Request/ContactFormRequest and you will this method

public function authorize()
{
    return false;
}

so just set return as true. It will allow further process.

public function authorize()
{
    return true;
}

Edited

change your Contact POST Route to this

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

and in your form action, just change this.

<form action="contact/store" ........

Upvotes: 0

Related Questions