Home > Mobile >  How to send email after registration using laravel 8?
How to send email after registration using laravel 8?

Time:10-19

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');
        }
    }

CodePudding user response:

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

CodePudding user response:

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 = '[email protected]';
$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", "[email protected]", "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 = '[email protected]';

$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();

}

}

}

CodePudding user response:

customize sending email in app/mail.php or .env file. You can use https://mailtrap.io/

  • Related