Last active
December 9, 2021 06:40
-
-
Save beisong7/c96a819e5c654b64de05929daf5ff80a to your computer and use it in GitHub Desktop.
Mailgun API email sending
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits\General; | |
use Illuminate\Support\Facades\Mail; | |
use Illuminate\Support\Str; | |
use Illuminate\Support\Facades\URL; | |
trait Mailer{ | |
public function sendMail($title, $to, $subject, $user, $data, $view, array $attachment=null){ | |
try{ | |
$from = "[email protected]"; | |
Mail::send($view, $data, function ($mail) use ($from, $to, $title,$subject, $user, $attachment) { | |
$mail->from($from, $title); | |
$mail->to($to, $user->name)->subject($subject); | |
if(!empty($attachment)){ | |
foreach ($attachment as $file){ | |
$mail->attach($file); | |
} | |
} | |
}); | |
}catch (\Exception $e){ | |
//logic to notify or record failed send | |
} | |
} | |
} | |
//update service in directory - config/services | |
'mailgun' => [ | |
'domain' => env('MAILGUN_DOMAIN'), | |
'secret' => env('MAILGUN_SECRET'), | |
'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'), | |
], | |
//usage | |
$data = [ | |
'something'=>'something data', | |
'somethingElse'=>'another data' | |
]; | |
$this->sendMail($user->first_name, $user->email, 'Welcome to the team', $user, $data, 'emails.welcome_admin'); | |
//update your .env file | |
MAIL_DRIVER=mailgun | |
MAILGUN_DOMAIN=mg.example.com | |
MAILGUN_SECRET=<some secrete> | |
// note: ensure you have 'guzzlehttp/guzzle' installed in your composer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment