Problems trying to send email with Laravel 5.5 - laravel

I made an API with Laravel and it is registering the data correctly
Now I'm trying to get this to trigger registration confirmation emails, however these are not going
I'm using Mailtrap for the tests
In .env I put like this:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=291ac7fdcf52bb
MAIL_PASSWORD=610b2e5e9782d3
No Http/Mail/DataManifestMail.php tenho:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class DataManifestMail extends Mailable
{
use Queueable, SerializesModels;
protected $inputs;
public function __construct($inputs)
{
$this->inputs = $inputs;
}
public function build()
{
return $this->view('mails.data');
}
}
In views/mails/data.blade.php I only have one warning message:
<h1>Test e-mail</h1>
Then in my Http/Controller/ManifestationController.php I put it like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Manifestation;
use App\Mail\DataManifestationMail;
use Mail;
class ManifestationController extends Controller
{
private $manifestation;
public function __construct(Manifestation $manifestation)
{
$this->manifestation = $manifestation;
}
public function store(Request $request)
{
$nr = Manifestation::max('nrmanifestation');
$this->manifestation->nrmanifestation = $nr + 1;
$this->manifestation->dspass = $request->dspass;
$this->manifestation->eeemail = $request->eeemail;
$this->manifestation->address = $request->address;
$this->manifestation->name = $request->name;
$this->manifestation->latitude = $request->latitude;
$this->manifestation->longitude = $request->longitude;
$this->manifestation->save();
Mail::to('vazag#c1oramn.com')->send(new DataManifestationMail());
return response()->json([
'result' =>
$this->manifestation->nrmanifestation
]);
}
}
I have reread the documentation and my code several times and have not found any errors
What can it be?

There might be multiple reasons:
configuration is cached (use php artisan config:cache)
you used invalid mailtrap data (you should have log in laravel.log file)
your server has somehow blocked 2525 port
Also looking at your code it seems you miss passing $input parameter to constructor. You have:
public function __construct($inputs)
but you run:
->send(new DataManifestationMail());
without passing any value

Related

Laravel Mail: Markdown Email [ Class "Illuminate\Mail\Mailables\Content" not found ]

I am getting a super strange error when I am creating a markdown email with Laravel.
I have a simple Markdown email (code below), the issue is with line 43 at "return new Content("
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Queue\SerializesModels;
use Illuminate\Mail\Mailables\Envelope;
use App\Models\User;
class RegisteredUser extends Mailable
{
use Queueable, SerializesModels;
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function content()
{
return new Content(
view: 'users.register.new',
with: [
'name' => $this->name,
],
);
}
public function build()
{
return $this->content();
// return $this->markdown('users.register.new');
}
}
The error I get is:
local.ERROR: Class "Illuminate\Mail\Mailables\Content" not found {"exception":"[object] (Error(code: 0): Class \"Illuminate\\Mail\\Mailables\\Content\" not found at www/app/Mail/RegisteredUser.php:43)
How is that possible? Shouldn't that be included with Laravel 9? (9.19)
How do I fix this?
Turns out the syntax I am using was introduced in Laravel 9.3 so I had to update Laravel using:
composer update.
PS. I was able to solve this from the help of Laravel's official discord group. Thank you <3

Missing required parameter for [Route: verification.verify]

On a project I have I am using Fortify as my BE. I need a multilingual app, therefore I added the
'prefix' => {locale}' to config/fortify.php.
Login, registering, and 2FA, are working ok, but the problem arrives with the email verification process.
If I try to click on the link received by email, it goes to the /email/verify and returns a forbidden page error.
Then if I request to get another verification email it returns the error displayed on the title of the question.
Probably it has something to be with the locale parameter because when I run route::list, the verification.verify route is displayed with the endpoint of {locale}/email/verify/{id}/{hash}, so I assume that the link on the request another mail is causing the error since it is referenced as /email/verify/{id}/{hash}.
So does anyone know how to change it?
Or has anyone faced a similar problem regarding Fortify and these localization routes?
What I had to do was to customize some of the default Fortify functions, extending some classes in order to add the locale parameter to them.
When a new user is registered (event) it sends the verification email (listener), so I had to configure the files involved in this flow.
<?php
namespace App\Listeners;
use Illuminate\Auth\Events\Registered;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SendEmailVerificationNotification implements ShouldQueue
{
use Queueable;
public function handle(Registered $event)
{
if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
$event->user->sendCustomVerificationEmailNotification();
}
}
}
And create the function sendCustomVerificationEmailNotification on the user's model and the notification CustomVerificationNotification that will be sent.
public function sendCustomVerificationEmailNotification()
{
$this->notify(new CustomVerificationNotification);
}
<?php
namespace App\Notifications;
use Carbon\Carbon;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;
class CustomVerificationNotification extends VerifyEmail
{
protected function verificationUrl($notifiable)
{
if (static::$createUrlCallback) {
return call_user_func(static::$createUrlCallback, $notifiable);
}
return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
[
'locale' => app()->getLocale(),
'id' => $notifiable->getKey(),
'hash' => sha1($notifiable->getEmailForVerification()),
]
);
}
}
Then in case, the user wants an additional verification email notification, this is handled through a function on the EmailVerificationNotificationController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Http\Controllers\EmailVerificationNotificationController;
class CustomEmailVerificationController extends EmailVerificationNotificationController
{
/**
* Send a new email verification notification.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return $request->wantsJson()
? new JsonResponse('', 204)
: redirect()->intended(Fortify::redirects('email-verification'));
}
$request->user()->sendEmail();
return $request->wantsJson()
? new JsonResponse('', 202)
: back()->with('status', 'verification-link-sent');
}
}

Laravel mail error: Process could not be started [The system cannot find the path specified. ]

I'm developing a new Laravel application. When I'm using mail to send messages via contact form in my website, I'm getting the following error:
Process could not be started [The system cannot find the path specified. ]
I'm developing in my local environment but using my business mail to get messages.
My controller:
namespace App\Http\Controllers;
use App\SendMessage;
use App\Mail\SendEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;
use Session;
class SendMessageController extends Controller
{
public function store(Request $request) {
$this->validate($request, [
"email" => "required|email",
"message" => "min:10",
"subject" => "min:3"
]);
$name = $request->name;
$email = $request->email;
$company = $request->company;
$subject = $request->subject;
$message = $request->message;
Mail::to("audit#auditors.uz")->send(new SendEmail($subject, $message));
Session::flash("success", "Your email was sent");
return back();
}
}
?>
My mailing function:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public $sub;
public $mese;
public function __construct($subject, $message)
{
$this->sub = $subject;
$this->mes = $message;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$e_subject = $this->sub;
$e_message = $this->mes;
return $this->view('emails.contact', compact("e_message"))->subject($e_subject);
}
}
?>
My .env file:
MAIL_DRIVER=mail
MAIL_HOST=mail.auditors.uz
MAIL_PORT=465
MAIL_USERNAME=audit#auditors.uz
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
I googled it a lot, but did not find the appropriate answer. If anybody of you can help me, I'll be really happy. Because I've been looking for a solution for a long time.
Your MAIL_DRIVER is set to mail, which does not exist by default. If you are using an SMTP mail server, you should use smtp as the driver.
Please make sure that your email provider supports port 465 and TLS encryption. Most of the providers support these automatically though.
Use MAIL_DRIVER="smtp"
Do not forget to Re-Serve or clear cache of Laravel
[ php artisan optimize:clear / php artisan config:clear / php artisan cache:clear ]

Why mail laravel not working on the staging server?

I try on the my localhost, it works
But if I try on the staging server, it does not works
My controller like this :
<?php
use Illuminate\Support\Facades\Mail;
use App\Mail\OrderReceivedMail;
...
class PurchaseController
{
...
public function test() {
$order = $this->order_repository->find(416);
$user = $this->user_repository->find(1);
Mail::to($user)->send(new OrderReceivedMail($order, $order->store));
}
}
My mail like this :
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderReceivedMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $order;
public $store;
public function __construct($order, $store)
{
$this->order = $order;
$this->store = $store;
$this->subject('subject');
}
public function build()
{
$mail_company = explode(',',config('app.mail_company'));
// dd($mail_company, $this->order->number, $this->store->name, 'test');
return $this->view('vendor.notifications.mail.email-order',['number'=>$this->order->number, 'store_name' => $this->store->name])->bcc($mail_company);
}
}
I try add this :
dd($mail_company, $this->order->number, $this->store->name, 'test');
on the mail
If in my localhost, the result of dd show
But if in the staging server, the result of dd not show
Seems if the staging server, it does not run this statement :
Mail::to($user)->send(new OrderReceivedMail($order, $order->store));
How can I solve this problem?
open config/mail.php, .env files and set your email driver as mail as bellow,
'driver' => env('MAIL_DRIVER', 'mail'), //you must set it in env file too
then you can send emails like bellow, note that emails.admin.member, is the path to your email template, in the example code, laravel will look for a blade template in the path, resources\views\emails\admin\member.blade.php
Mail::queue('emails.admin.member', $data, function($message) {
$message->subject("A new Member has been Registered" );
$message->from('noreply#mydomain.net', 'Your application title');
$message->to('yourcustomer#yourdomain.com');
});
or
Mail::send('emails.admin.member', $data, function($message) {
$message->subject("A new Member has been Registered" );
$message->from('noreply#mydomain.net', 'Your application title');
$message->to('yourcustomer#yourdomain.com');
});

Call to undefined method Illuminate\Notifications\Notification::send()

I am trying to make a notification system in my project.
These are the steps i have done:
1-php artisan notifications:table
2-php artisan migrate
3-php artisan make:notification AddPost
In my AddPost.php file i wrote this code:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class AddPost extends Notification
{
use Queueable;
protected $post;
public function __construct(Post $post)
{
$this->post=$post;
}
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'data'=>'We have a new notification '.$this->post->title ."Added By" .auth()->user()->name
];
}
}
In my controller I am trying to save the data in a table and every thing was perfect.
This is my code in my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use App\User;
//use App\Notifications\Compose;
use Illuminate\Notifications\Notification;
use DB;
use Route;
class PostNot extends Controller
{
public function index(){
$posts =DB::table('_notification')->get();
$users =DB::table('users')->get();
return view('pages.chat',compact('posts','users'));
}
public function create(){
return view('pages.chat');
}
public function store(Request $request){
$post=new Post();
//dd($request->all());
$post->title=$request->title;
$post->description=$request->description;
$post->view=0;
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
return redirect()->route('chat');
}
}
Everything was good until I changed this code:
$post->save();
to this :
if ($post->save())
{
$user=User::all();
Notification::send($user,new AddPost($post));
}
It started to show an error which is:
FatalThrowableError in PostNot.php line 41: Call to undefined method
Illuminate\Notifications\Notification::send()
How can i fix this one please??
Thanks.
Instead of:
use Illuminate\Notifications\Notification;
you should use
use Notification;
Now you are using Illuminate\Notifications\Notification and it doesn't have send method and Notification facade uses Illuminate\Notifications\ChannelManager which has send method.
Using this
use Illuminate\Support\Facades\Notification;
instead of this
use Illuminate\Notifications\Notification;
solved the problem for me.
Hope this helps someone.
using this is better
use Notification
Instead of
use Illuminate\Support\Facades\Notification
this makes the send() not accessible [#Notification Databse]

Resources