I am trying to send emails with mailgun but they won't send and I have no idea why because i don't get any errors at all.
This is my code:
mail.php:
'driver' => env('MAIL_DRIVER', 'mailgun'),
services.php:
'mailgun' => [
'domain' => env('sandbox1e...60.mailgun.org'),
'secret' => env('key-146...419'),
],
EmailController.php:
public function send($email, $uuid = null)
{
if($uuid == null){
$uuid = User::get()->where('customer_email' , $email)->first()->email_confirmed;
}
return Mail::to($email)->send(new ConfirmEmail($uuid));
}
ConfirmEmail.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class ConfirmEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public $uuid;
public function __construct($uuid)
{
$this->uuid = $uuid;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('mailgun@sandbox1e17506823f2490ba9cc78cbbc2adb60.mailgun.org')
->view('emails.confirm');
}
}
I have added the emailadress I want to send to in mailgun, but it's not working. Am I doing something wrong or is there any way I can debug this?
Your configuration is wrong:
'mailgun' => [
'domain' => env('sandbox1e...60.mailgun.org'),
'secret' => env('key-146...419'),
],
The env function looks for an environment variable with the name you provide and returns the value. You should change it to the name of an environment variable and define it in your .env or don't use the env function, but that's not recommended.
Whereas Esteban Garcia 's answer is correct, I to wish improve it with code snippets showing how exactly the configuration should look like:
In your config/services.php
, leave the configuration as shown below:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
In your .env
file, that is where you define the actual mailgun credentials:
MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandbox1e...60.mailgun.org
MAILGUN_SECRET=key-146...419
In your laravel .env configure these things
MAIL_DRIVER=mailgun
MAIL_USERNAME=postmaster@sandboxXXXXXX.mailgun.org
MAILGUN_DOMAIN=sandboxXXXXX.mailgun.org
MAILGUN_SECRET=XXXXXXX
After that goto vendor->guzzlehttp->src->client.php find configureDefaults() method have array name called defaults that array have verify => true change to false
'verify' => false,
then configure mail.php
'from' => ['address' => 'youremail@gmail.com', 'name' => 'yourname'],
go to route.php and test email is working or not send simple email. plz make sure you install laravel mail class or install using this command composer require guzzlehttp/guzzle
use Illuminate\Support\Facades\Mail;
Route::get('/', function () {
$data = [
'title'=>'title here',
'Content'=>'simple content'
];
Mail::send('email.test',$data, function ($message){
$message->to('youremail@gmail.com', 'John Smith')->subject('Welcome!');
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.