How to send mail without including html in laravel?

by mallory_cormier , in category: PHP Frameworks , 4 months ago

How to send mail without including html in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 4 months ago

@mallory_cormier 

To send an email without including HTML in Laravel, you can use the from() and text() methods provided by the Laravel Mail facade. Here's an example of how you can send a plain text email in Laravel:

1
2
3
4
5
6
7
8
9
use IlluminateSupportFacadesMail;
use AppMailPlainEmail;

$data = [
    'name' => 'John Doe',
    'message' => 'This is a plain text email message.'
];

Mail::to('[email protected]')->send(new PlainEmail($data));


In this example, we are sending a plain text email using a Mailable class named PlainEmail. Here is how the PlainEmail Mailable class may look like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace AppMail;

use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateQueueSerializesModels;

class PlainEmail extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function build()
    {
        return $this->from('[email protected]')
                    ->text('emails.plain') // Blade file for plain text content
                    ->with(['data' => $this->data]);
    }
}


In the PlainEmail class, we specify that the email should be sent as plain text using the text() method. You can provide the content for the plain text email in a blade file specified in the text() method. In this example, we used 'emails.plain' as the blade file for the plain text content.


Make sure to create a blade file named plain.blade.php in the resources/views/emails directory with the content of the plain text email message:

1
2
Name: {{ $data['name'] }}
Message: {{ $data['message'] }}


By following these steps, you can send plain text emails in Laravel without including HTML.