I want to send and receive emails from my webpage using mailgun.
Main problem : Laravel cannot find my send-email.blade file on resources\views\pages
I configure my .env file for mailgun.
What I am missing? Here is my codes;
web.php
Route::get('/send-email', [App\Http\Controllers\EmailController::class, 'sendEmail']);
app\http\controller\EmailController.php
namespace App\Http\Controllers;
use App\Mail\HelloEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class EmailController extends Controller
{
public function sendEmail()
{
$reveiverEmailAddress = "[email protected]";
Mail::to($reveiverEmailAddress)->send(new HelloEmail);
if (Mail::failures() != 0) {
return "Email has been sent successfully.";
}
return "Oops! There was some error sending the email.";
}
}
config\services.php
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
]
config\mail.php
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
],
],
'from' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'app name'),
],
'reply_to' => [
'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
'name' => env('MAIL_FROM_NAME', 'app name'),
],
app\mail\hellomail.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class HelloEmail extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
//
}
public function build()
{
return $this->from("[email protected]")->view('email-template');
}
}
email-template.blade.php
<div >
<p>Hello</p>
<p>
Demo of sending emails through the Mailgun email service.
</p>
</div>
CodePudding user response:
You're using ->view('email-template);
which looks for the file in resources/views/{filename}
You say your email-template
file is stored in resources\views\pages
Therefore you have to use ->view('pages.email-template');
which will look in resources/views/pages/{filename}
.