Do you want to use Mailtrap in your Laravel project? Just follow the steps below. Success!
Step 1:
Create an account on Mailtrap and log in. You'll get 50 free emails. So use it carefully.
Step 2:
Click the Start Testing button and this page will show.
Then you have to choose the programming language you need. We need Laravel 7+.
Step 3:
After choosing Laravel 7+, you need to replace the Mailtrap settings in your .env file.
You need to change the email address and email name too.
MAIL_FROM_ADDRESS="example@gmail.com"
MAIL_FROM_NAME="Hello World"
Step 4:
Now we need to create a Mail folder, this is the path App\Mail;
We need to run some command in the terminal.
This command will create a _Mail folder with file ContactMail _inside the App folder. You can name your file what ever you like, for this example I used ContactMail.
From the command above, you see that I used -m emails.contactEmailView, this will create emails folder inside the views folder.
You can create them separate, but I find this 1 line command easy and saves time :).
Inside the file contactEmailView is the template form Laravel. You use it or edit it with your style. Remember that this template is the one you'll see in your inbox in Mailtrap.
I didn't use the Laravel template, here is the example.
<!DOCTYPE html>
<html>
<head>
<style>
YOUR STYLE HERE
</style>
<body>
<div class="card" style="display: flex; flex-direction: column; align-items: center; text-align: center;">
<h1>Hi there,</h1>
<p>Thank you for contacting us. We have received your message and will get back to you soon.</p>
<h2>Details:</h2>
<ul>
<li><strong>Name:</strong> {{ $contact->name }}</li>
<li><strong>Email:</strong> {{ $contact->email }}</li>
<li><strong>Message:</strong> {{ $contact->message }}</li>
</ul>
<p class="signature">Best regards,<br><br><br>Hi</p>
<p class="website-link">
Visit our website: <a href="https://www.10dots.be" target="_blank">www.hello.com</a>
</p>
</div>
</body>
</html>
Step 5:
MVC, you know this very well.
In this project, I saved the input in my database. Here is the example of my Controller.
class ContactController extends Controller
{
public function create()
{
return view('Contact');
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'message' => 'required'
]);
$contact = Contact::create($request->all());
Mail::to($request->email)->send(new ContactMail($contact));
return redirect()->route('homepage')->with('success', 'Thank you for contacting us. We will get back to you soon.');
}
}
Use store() and validate the input from the form.
This code means to create all the validated input and save in the database.
_ $contact = Contact::create($request->all());_
Mail::to($request->email) this means Mail to the email address, in this case your email address in your .env file.
->send(new ContactMail($contact)); It creates a new instance of the ContactMail class and passes the $contact variable as an argument. The send method, indicating that you want to send the email.
Step 6:
Now time to set up the ContactMail file. The code below is the example from my project.
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use App\Models\Contact;
class ContactMail extends Mailable
{
use Queueable, SerializesModels;
public $contact;
/**
* Create a new message instance.
* @param Contact $contact
* @return void
*/
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.contactEmailView')
->subject('Thank you');
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Contact Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.contactEmailView',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
- The __construct method is a constructor method for the mail class. It is executed when an instance of the mail class is created.
- The build method is a required method in a Laravel mail class. It is responsible for constructing the email message and returning it.
Don't forget to create your Routes.








Top comments (0)