Laravel 9 How To Change Address & Name in Email

Share this Article
Reading Time: 7 minutes
342 Views

Sending an email in web application is very common. Laravel provides the predefined Mail class used to send mail. We only need to use & configure it. The content of this article is very useful to understand the things that laravel 9 how to change address & name in email.

Inside this article we will Send an Email in Laravel 9 by Gmail SMTP Server, also we will set it’s from address and name in email settings. We will use Gmail and it’s SMTP with Laravel 9.

To set from address and name for emails, we use from() method. To get the complete concept of this article you need SMTP details.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

SMTP Configuration

To configure SMTP details, open up the file .env from application root.

We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

//..

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME="xxxyyyzzz@gmail.com"
MAIL_PASSWORD="your_password"
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="xxxyyyzzz@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"

//...

You should see inside (.env) above configuration we have these settings as well –

MAIL_FROM_ADDRESS="xxxyyyzzz@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"

Internally laravel uses these configured values while sending mails.

Open mail.php from /config folder.

You will find these lines into it.

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

Keys address and name of from array taking values from .env file. If you have no values set then it uses default values as passed here.

Make sure you have enabled google security setting form your gmail. go to Google account and click on “Account”. Once you are on the “Account” page, click on “Security“. Scroll down to the bottom and you will find “Less secure app access” settings. Set as ON.

Have a look into these images.

Create Mail Class

Inside this we will create mail class via artisan command which will be responsible for sending emails.

$ php artisan make:mail MyTestMail

Application will creates a folder with the name of Mail inside /app folder. You should see a file MyTestMail.php inside /app/Mail folder.

Open file MyTestMail.php and write this code into it.

<?php
  
namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyTestMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $details;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($details)
    {
        $this->details = $details;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $address = config("mail.from.address");
        $name = config("mail.from.name");

        $this->subject('New Article Published in Laravel 9')
            ->view('emails.custom_template')
            ->from($address, $name);
      
        return $this;
    }
}

$address = config(“mail.from.address”); It returns the value from mail.php [mail is file, from is an array and address is the key]

$name = config(“mail.from.name”); It returns the name value from mail.php

Concept to add from address and name in email

$this->subject("...")->view("...")->from($address, $name);

Create Mail Template

Let’s create a folder emails inside /resources/views folder. Inside this folder we will create a simple blade template file with following content.

Create a fie custom_template.blade.php inside /resources/views/emails folder. Open file and write this code into it.

<!DOCTYPE html>
<html>

<head>
    <title>Mail From Online Web Tutor</title>
</head>

<body>
    <p>Hi Users,</p> 
    <p>Greetings!!!</p>
    <p>Hope you are doing good.</p>
    <p>{{ $details['title'] }}</p>
    <p>{{ $details['body'] }}</p>

    <p>Thank you</p>
</body>

</html>

Add Route

Open web.php file from /routes folder. Add this route and code into it.

//...

use Illuminate\Support\Facades\Mail;

Route::get('send-mail', function () {
   
  $details = [
      'title' => 'Online Web Tutor Support Mail',
      'body' => 'This is a notification mail from support. We have published a new article in laravel 9 category.'
  ];
 
  Mail::to('send_to_email@gmail.com')->send(new \App\Mail\MyTestMail($details));
 
  dd("Email is Sent, please check your inbox.");
});

//...

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL – http://127.0.0.1:8000/send-mail

We hope this article helped you to learn about Laravel 9 How To Change Address & Name in Email in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5