How to send email after registration using laravel 8?

Viewed 347

I want to send an email notification after registration when the user will register they will get an email notification please help me how can send an email?

UserController

public function store(Request $request)
{     
    $data = [
        'first_name' => $request->first_name,
        'last_name'         => $request->last_name,
        'name'              => $request->first_name . ' ' . $request->last_name,
        'email'             => $request->email,
        'password'          => bcrypt($request->password),
        'country_id'        => $request->country,
        'user_type'         => 'customer',
        'active_status'         => '1',
        'activation_code'   => str_random(30)
    ];
    
    $user_create = User::create($data);        
    Mail::to($request->email)->send(new UserSignUpMail);              
}

UserSignUpMail

 <?php
    class UserSignUpMail extends Mailable
    {
        use Queueable, SerializesModels;
    
        /**
         * Create a new message instance.
         *
         * @return void
         */
        public function __construct()
        {
            //
        }
    
        /**
         * Build the message.
         *
         * @return $this
         */
        public function build()
        {
            return $this->markdown('emails.userSignUp');
        }
    }
4 Answers

Laravel 8 Send email example; In this tutorial, you will learn how to send email in laravel 8 using SMTP drivers like Mailgun, Postmark, Amazon SES, and sendmail.

after the registration you can call your function

This link will help you Send Email

what email service are you using? add credentials to that service in config/mail.php or if its sendgrid we should use library provided by them

<?php

namespace App\Traits;

trait SendMail
{

protected function SendEmail($from_name, $to_email, $to_name, $subject, $body, $template)

{

try {


$from_email = 'sender@gmail.com';
$email = new \SendGrid\Mail\Mail();

$email->setFrom($from_email, $from_name);

$email->setSubject($subject);

$email->addTo($to_email, $to_name);

$email->addContent("text/plain", $body);

$email->addContent("text/html", $template);

$sendgrid = new \SendGrid('SENDGRID_API_KEY');

$response = $sendgrid->send($email);

return $response->statusCode();

} catch (Exception $e) {

return 'Error: ' . $e->getMessage();

}

}



//example

//$this->SendEmailWithAttachment("from_name", "xyz@xyz.com", "name", "Subject", "Body", "Template string", "URL, "file.mp3");

protected function SendEmailWithAttachment($from_name, $to_email, $to_name, $subject, $body, $template, $attachment_file_url, $file_name)

{

try {

$file_encoded = base64_encode(file_get_contents($attachment_file_url));



$from_email = 'qwertyvate@gmail.com';

$email = new \SendGrid\Mail\Mail();

$email->setFrom($from_email, $from_name);

$email->setSubject($subject);

$email->addTo($to_email, $to_name);

$email->addContent("text/plain", $body);

$email->addContent("text/html", $template);

$email->addAttachment(

$file_encoded,

null,

$file_name,

null

);



$sendgrid = new \SendGrid(getenv("SENDGRID_API_KEY"));



$response = $sendgrid->send($email);

return $response->statusCode();

} catch (Exception $e) {

return 'Error: ' . $e->getMessage();

}

}

}

First of all you have set the right Mailsettings in your .env file.

MAIL_DRIVER=smtp
MAIL_HOST=your.server.com
MAIL_PORT=25
MAIL_USERNAME=1234
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=hi@mail.com

You can send Mail from Controller like that:

return \Mail::to("your@mail.com")        
->send(new UserSignUpMail());

or via route:

Route::get(“/send-email”, function() {
    return new UserSignUpMail();
});

In both cases you need the Laravel Mail Facade. use Illuminate\Support\Facades\Mail;

In your build() function in you userSignUp class then replace the:

return $this->markdown('emails.userSignUp');

with :

$this->view('emails.userSignUp');

And you sending "real" mails.

Related