Skip to content

Instantly share code, notes, and snippets.

@pravnkay
Last active March 9, 2021 11:03
Show Gist options
  • Save pravnkay/0075a182ee6636c2b799341693cb02e9 to your computer and use it in GitHub Desktop.
Save pravnkay/0075a182ee6636c2b799341693cb02e9 to your computer and use it in GitHub Desktop.
Using Mailjet to send mails from Laravel

Using Mailjet in Laravel

Using Mailjet wrapper for Laravel to send mails using their API service

Support

You can read more on this in my Medium blog post here. Do let me know your thoughts through comments or through mail [email protected]

Have a good day !

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Mailjet\LaravelMailjet\Facades\Mailjet;
use Mailjet\Resources;
class EnquiryController extends Controller
{
public function index(Request $request)
{
$input = $request->all();
Validator::make($input, [
'name' => [
'required',
'string',
],
'email' => [
'required',
'string',
'email',
'max:255',
],
'phone' => [
'required',
'digits:10',
'max:10',
],
])->validate();
$formdata = $request->except('_token');
$mj = Mailjet::getClient();
$body = [
'FromEmail' => "[email protected]",
'FromName' => "From name to be displayed in Inbox",
'Subject' => "New Website Enquiry",
'MJ-TemplateID' => 123456789,
'MJ-TemplateLanguage' => true,
'Vars' => json_decode(json_encode($formdata), true),
'Recipients' => [['Email' => "[email protected]"]]
];
$response = $mj->post(Resources::$Email, ['body' => $body]);
if($response->success()){
- do success event callback -
} else {
- do failure event callback -
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment