How To Change Address & Name in Laravel 8 Email

Reading Time: 7 minutes
6,285 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 how to change address & name in laravel 8 email.

Inside this article we will Send an Email in Laravel 8 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 8.

To set from address and name for emails, we use from() method.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

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=587
MAIL_USERNAME="owthubteam@gmail.com"
MAIL_PASSWORD="mypassword"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="owthubteam@gmail.com"
MAIL_FROM_NAME="Online Web Tutor Support"
  
//...
  

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

MAIL_FROM_ADDRESS="owthubteam@gmail.com"
MAIL_FROM_NAME="Online Web Tutor Support"

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.


Add Route

Open web.php file from /routes folder. Add following into that file.

//...
use Illuminate\Support\Facades\Mail;

//...

Route::get('send-mail', function () {

    $details = [
        'title' => 'Add Address & Name to E-Mail',
        'body' => 'This is a test mail in laravel 8. Email shows the address and name added in email properly.'
    ];

    Mail::to('send_to_email@gmail.com')->send(new \App\Mail\MyTestMail($details));

    dd("Email is Sent, please check your inbox.");
});

//...

Create Mail Class

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

$ php artisan make:mail MyTestMail

When you run this command via terminal, 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 MyTestMail.php and following 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('Mail from Online Web Tutor')
            ->view('emails.sample-mail')
            ->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 Email Template

Create a folder emails inside /resources/views folder. Inside this folder create a file sample-mail.blade.php

Open sample-mail.blade.php and write this code into it.

<!DOCTYPE html>
<html>

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

<body>
    <h1>{{ $details['title'] }}</h1>
    <p>{{ $details['body'] }}</p>
    <p>Thank you <br />Online Web Tutor</p>
</body>

</html>

Application Testing

Run this command into project terminal to start development server,

php artisan serve

Open up the URL – http://127.0.0.1/send-mail

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

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more