0

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 abc@gmail.com 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('abc@hellworld.com')->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       abc@gmail.com

Number      9870000000

City        New York

Post        Hello World 
john
  • 11,311
  • 40
  • 131
  • 251

3 Answers3

2

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.

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0
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"


   ]
Chris Cousins
  • 1,862
  • 8
  • 15
0

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('letsruckify@ruckify.com', '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>
Erubiel
  • 2,934
  • 14
  • 32
  • I used this code. I am wondering what I should be using inside `email.posting-message`. Because at this moment inside email.posting-message I am using `{{ $msg }}` this. – john Aug 27 '18 at 15:52
  • just use a table exactly like @ViperTecPro answered... edit: it wasn't a table, but still, thats how you would access – Erubiel Aug 27 '18 at 15:52
  • I think, you didn't get me. emails.posting-message is not a view. – john Aug 27 '18 at 15:58
  • Check my controller in the question. `public function create() { return view('posting'); }` – john Aug 27 '18 at 15:59
  • Here is `posting` is the view. – john Aug 27 '18 at 15:59
  • so, whats `emails.posting-message` – Erubiel Aug 27 '18 at 16:01
  • https://laravel.com/docs/5.4/mail ... add `->view()`so you can format in a view... i don't know if this way of doing things was dropped, this was right on laravel 5.2 https://laravel.com/docs/5.2/mail and maybe on laravel 5.3... – Erubiel Aug 27 '18 at 16:10
  • Where do you want me to add view ? – john Aug 27 '18 at 16:13
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178877/discussion-between-john-and-erubiel). – john Aug 27 '18 at 16:36
  • when I am copying @ViperTecPro code from the controller, it is telling me `Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file` – john Aug 27 '18 at 16:37