Home > Software engineering >  Why does sending multiline emails fail in this Laravel 8 application?
Why does sending multiline emails fail in this Laravel 8 application?

Time:08-12

I am working on a blogging application in Laravel 8.

I have a simple contact form (contact.blade.php):

@if (session('success'))
  @include('themes/' .$theme_directory . '/partials/success')
@endif

@if (session('error'))
  @include('themes/' .$theme_directory . '/partials/errors')  
@endif

<form method="POST" action="{{ route('contact.submit') }}" name="sentMessage" id="contactForm" novalidate>
  @csrf
  <div >
    <div >
      <label>Name</label>
      <input type="text"  placeholder="Name" name="name" id="name" value="{{ old('name') }}" required data-validation-required-message="Please enter your name.">
      @error('name')
        <p >{{ $message }}</p>
      @enderror
    </div>
  </div>
  <div >
    <div >
      <label>Email Address</label>
      <input type="email"  placeholder="Email Address" name="email" id="email" value="{{ old('email') }}" required data-validation-required-message="Please enter your email address.">
      @error('email')
        <p >{{ $message }}</p>
      @enderror
    </div>
  </div>
  <div >
    <div >
      <label>Phone Number</label>
      <input type="tel"  placeholder="Phone Number" name="phone" id="phone" value="{{ old('phone') }}" required data-validation-required-message="Please enter your phone number.">
      @error('phone')
        <p >{{ $message }}</p>
      @enderror
    </div>
  </div>
  <div >
    <div >
      <label>Message</label>
      <textarea rows="5"  placeholder="Message" name="message" id="message" required data-validation-required-message="Please enter a message.">
        {{ old('message') }}
      </textarea>
      @error('message')
      <p >{{ $message }}</p>
      @enderror
    </div>
  </div>
  <div id="success"></div>
  <button type="submit"  id="sendMessageButton">Send</button>
</form>

The ContactController controller:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;

class ContactController extends FrontendController
{
  public function index() {
            return view('themes/' . $this->theme_directory . '/templates/contact', 
            array_merge($this->data, [
                'tagline' => 'Contact us',
            ])
        );
    }


    public function submit(Request $request){
        // Server-side form validation
        $rules = [
            'name' => 'required',
            'email' => 'required|email',
            'phone' => 'required|numeric',
            'message' => 'required'
        ];
    
        $messages = [
            'name.required' => 'Please enter your name',
            'email.required' => 'Please enter your email address',
            'email.email' => 'Not a valid email address',
            'phone.required' => 'Please enter your phone number',
            'message.required' => 'Please enter a message'
        ];

        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput()->with('error', 'There are invalid fields in the form.');
        } else {
            Mail::send('themes/' . $this->theme_directory . '/emails/contact-message', [
                'site_name' => $this->data['site_name'],
                'name' => $request->name,
                'email' => $request->email,
                'phone' => $request->phone,
                'msg' => $request->message,
            ],
            function($mail) use($request){
                $mail->from($request->email, $request->name);
                $mail->to($this->data['owner_email'])->subject('A message from ' . $this->data['site_name']);
            });

            return redirect()->back()->with('success', 'Your message was sent. We will get back to you as soon as possible!');
        }
        
    }
}

In the email template I use {!! $msg !!} to display the message.

The problem

If the textarea in the form is filled with multiline text, the email (I use Mailtrap for testing) receives it as a single line.

Questions

  1. What causes this bug?
  2. What is the easiest fix?

CodePudding user response:

I think it is not a bug. If you don't use WYSIWYG Editor, line breaks don't replace with <br> tag. Using the following code can fix your problem nl2br($request->message)

  • Related