DEV Community

Debajyoti Das
Debajyoti Das

Posted on

Laravel 10 Mailing

php artisan make:mail GeneralMail


use Illuminate\Support\Facades\Mail;
use App\Http\Traits\GeneralTrait;

public function forgot_password(Request $request)
{
$this->rules = [
            'email' => 'required|email',
        ];

        $validator = Validator::make($request->all(), $this->rules);

        if($validator->fails())
            return response()->json(['code'=>400, 'message'=>$validator->errors()], 200);
        try {
            $user = User::where('email', $request->email)->first();
            if($user == null)
                throw new Exception('User does not exists');

            $otp = $this->otp_get();

            $user->update([
                'otp' => $otp,
            ]);

            $this->data['to'] = $request->email;
            $this->data['name'] = $user->name;
            $this->data['subject'] = env('APP_NAME').' - Forgot Password';
            $this->data['mailFromId'] = (config()->get('mail.from.address') != '') ? config()->get('mail.from.address') : env('MAIL_FROM_ADDRESS');

            $txt = '';
            $txt .= '<p>OTP: '.$otp.'</p>';  
            $this->data['txt'] = $txt;

            Mail::to($this->data['to'])->send(new GeneralMail($this->data));                                
        } catch (Exception $e) {
            return response()->json(['code'=>400, 'message'=>$e->getMessage()], 200);
        }   

        return response()->json(['code'=>200, 'message' => 'OTP sent successfully to your mail', 'data'=>[]], 200);
}
Enter fullscreen mode Exit fullscreen mode

app/Http/Mail/GeneralMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

use Illuminate\Mail\Mailables\Address;
use Illuminate\Mail\Mailables\Attachment;

class GeneralMail extends Mailable
{
    use Queueable, SerializesModels;

    public $mail_arr = [];

    /**
     * Create a new message instance.
     */
    public function __construct($data = [])
    {
        //
        $this->mail_arr['name'] = $data['name'];
        $this->mail_arr['subject'] = $data['subject'];
        $this->mail_arr['mailFromId'] = $data['mailFromId'];
        $this->mail_arr['txt'] = $data['txt'];
    }

    /**
     * Get the message envelope.
     */
    public function envelope(): Envelope
    {
        return new Envelope(
            from: new Address($this->mail_arr['mailFromId'], config('setting.siteName')),
            subject: $this->mail_arr['subject'],
        );
    }

    /**
     * Get the message content definition.
     */
    public function content(): Content
    {
        return new Content(
            // view: 'mails.only_text_mail_layout.blade',
            // html: 'emails.orders.shipped',
            // text: 'emails.orders.shipped-text'
            markdown: 'mails.only_text_mail_layout',
            with: [
                'mail_arr' => $this->mail_arr,
            ],
        );
    }

    /**
     * Get the attachments for the message.
     *
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
     */
    public function attachments(): array
    {
        return [];
        // Attachment::fromPath(asset('uploads/profile_images/1670928231_4.chrlws.jpg'))
            //     ->as('admin_propic.jpg')
                // ->withMime('application/pdf'),
                // ->withMime('image/jpeg'),
    }
}

Enter fullscreen mode Exit fullscreen mode

app/resources/views/mails/general_mail_template.blade.php

<body>

@component('mail::message')
{{-- <center><img src="{{asset('uploads/'.config()->get('setting.logo'))}}" style="width: 200px; height: 50px;" type="image/x-icon"></center> --}}

# Hi {{$mail_arr['name']}},<br>
<hr>
{!! $mail_arr['txt'] !!}

<hr>

Thanks,<br>
# {{ config('app.name') }}
@endcomponent

</body>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)