How to format the content received in an email in Laravel 5.4?

1.9k Views Asked by At

I am working on a website in which I want to format content of an email received.

At this moment it is coming up in one line like this with only value showing up.

Mike [email protected] 9870000000 New York Hello World

The controller which I have used for the email in Laravel is:

Class PostingMessageController extends Controller
{
    public function create() 
    {
        return view('posting');
    }

   public function store(Request $request)
   {

/*
    dd($request->all());

  */   
      $this->validate($request, [
      'name' => 'required',
      'email' => 'required|email',
      'number' => 'required',
      'city' => 'required',
      'post' => 'required'
      ]);

       Mail::send('emails.posting-message', [
        'msg'=> $request->name . "\r\n"
        . $request->email . "\r\n"
        . $request->number . "\r\n"
        . $request->city . "\r\n"
        . $request->post . "\r\n"


       ], function($mail) use($request) {

          $mail->from($request->email, $request->name);

           $mail->to('[email protected]')->subject('Contact Message');
       });

       return redirect()->back()->with('flash_message', 'thank you, your posting info has been sent to our team. we will reach out as soon as we can to provide next steps!');

   } 
}   


Problem Statement:

I am wondering what changes I need to make in the controller so that everything shows up in different lines like below with field names at the left and values at the right. At this moment, I am seeing only values as mentioned above.

Name        Mike

Email       [email protected]

Number      9870000000

City        New York

Post        Hello World 
3

There are 3 best solutions below

6
On
Mail::send('emails.posting-message', [
    'msg'=> "Name\t" . $request->name . "\r\n"
    . "Email\t" . $request->email . "\r\n"
    . "Number\t" . $request->number . "\r\n"
    . "City\t" . $request->city . "\r\n"
    . "Post\t" . $request->post . "\r\n"


   ]
9
On

If you wanted to modify you're output of your email, you're probably looking for something you can get into blade and design it and then send it, here is how i do it

In My Routes : routes/Web.php

Route::get('contact-us', 'Main@showContactUs')->name('route_name');
Route::post('contact-us', 'Main@doContactUs')->name('route_name');

In My Controller : app/Http/Controllers/Main.php

use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;

class Main extends Controller
{
    public function showContactUs()
    {
        $data = [];
        return View::make('pages.contactUs',$data);
    }
    public function doContactUs(Request $r)
    {
     $fullname = $r->get('fullName');
     $phone    = $r->get('phone');
     $email    = $r->get('email');
     $description = $r->get('message');
     Mail::to('RECEIVER_EMAIL_ADDRESS')->send(new SendMailable($fullname, $phone, $email, $description));
      if (Mail::failures())
      {
        $message1 = " Something Went Wrong.";
      }
      else
      {
        $message2 = " Message Sent Successfully.";
      }
    return redirect()->route('route_name'])->with([
            'warning' => $message1,
            'success' => $message2
        ]);
    }
}

then Create this: app\Mail\SendMailable.php

<?php
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendMailable extends Mailable
{
    use Queueable, SerializesModels;
    public $fullname,$phone,$email,$description;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($fullname, $phone, $email,$description)
    {
        $this->fullname = $fullname;
        $this->phone = $phone;
        $this->email = $email;
        $this->description = $description;
    }
    /**
     * Build the message. THIS WILL FORMAT YOUR OUTPUT
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('email.layoutOne')->subject('Contact Us Subject');
    }
}

Finally in Blade : resources/email/layoutOne.blade.php you can do it like this

<div>
    <p>Fullname  : {{ $fullname }}</p>
    <p>Phone No. : {{ $phone }}</p>
    <p>Email Address : {{ $email }}</p>
    <p>Description : {{ $description }}</p>
    <hr>
    <p>Thank you for your Query. We'll get back to you within 24 Hours. </p>
</div>

Hope this helps.

10
On

Why not pass the data as an array and format at the view?

$data = array(
    'name'=> $request->name,
    'email'=> $request->email,
    'number'=> $request->number,
    'city'=> $request->city,
    'post'=> $request->post
);

Mail::send('emails.posting-message', $data, function($mail) use($request) {
    $mail->from($request->email, $request->name);
    $mail->to('[email protected]', 'Name')->subject('Contact Message');
});

You can access the variables as $name or $email and implement some table in your blade view like the one in the answer by @ViperTecPro

This is an example of how you can do it... the specific html is up to you.

<div>
    <p>Name  : {{ $name }}</p>
    <p>Email Address :    {{ $email }}</p>
</div>