简体   繁体   中英

Broken Email Template

I am using laravel 5 framework for a project and it has a features that a user can construct a email message. But when I test it on my email I got html tags.

<p> <small>Julius Cesar</small></p><p><small>Julius</small></p><p><small> Cesar<br></small></p><p><small>tester@mail.com </small> <br></p> 

Expected Output that the user receives from email should be:

Julius Cesar
Julius
Cesar
tester@mail.com

I have no idea why it goes like that. Here is my code in laravel. This is for the view of that displays the form: eblast-form.blade.php

<form action=" {{ url( 'app/esetting/emailblast' ) }} " id="eblast-form" method="post">
    {{ csrf_field() }}
    <div class="box-body">
        <div class="form-group {{ $errors->has( 'subject' ) ? 'has-error' : '' }}">
            <label for="subject" class="control-label col-sm-2">Subject</label>
            <div class="col-sm-10">
                <input type="text" name="subject" class="form-control" id="subject" placeholder="Subject" value="{{ old('subject') }}" required="required" />
            </div>
        </div>
        <br><br>
        <div class="form-group {{ $errors->has( 'message' ) ? 'has-error' : '' }}">
            <label for="message" class="control-label">Email Message Content</label>
            <div class="col-sm-12">
                <textarea class="form-control" name="message-cont" id="message" required>
                    {{ old('message') }}
                </textarea>
                <p><small> [fullname] [first_name] [last_name] [email]  </small></p>
                <input type="hidden" id="msg" name="message" value="">
            </div>
        </div>
    </div>
    <br>

    <br>
    <div class="box-footer">
        <div class=" pull-right">
         <button type="submit" class="btn btn-info " id="send-mail">Send</button>

        </div>
    </div>
</form>

This is on my controller's method that sends the email:

 public function postEmailblast(Request $request)
    {        
        ....
        $user = Auth::user();
        $message = $request->input('message');
        $m_user = User::find($egm->user_id);
        $rep_this = array(
            "[fullname]", 
            "[first_name]", 
            "[last_name]", 
            "[email]"
        );
        $rep_with = array(
            $m_user->common_name, 
            $m_user->first_name, 
            $m_user->last_name, 
            $m_user->email
        );
        $data = array(
            'email_content' => str_replace($rep_this, $rep_with, $message),
        );

        Mail::send('emails.e-blast', $data, function ($message) use ($request, $user, $m_user) {

            $message->from($user->email, $user->common_name)->to($m_user->email, $m_user->common_name)->subject($request->input('subject'));

        });
        ....
    }

This is the email template of emails.e-blast / emails.e-blast.blade.php

<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="utf-8">
</head>
<body>
{{$email_content}}
</body>
</html>

Does anyone have an idea about my case?

You can also use

<body>
{!!$email_content!!}
</body>

instead of

<body>
{{$email_content}}
</body>

For a simpler though less secure solution.

Not sure if you did this for a specific reason but in my opinion the problem lies here

<body>
{{$email_content}}
</body>

Instead of pasting email content ( btw. you escaped it ) you should use the blade template to build your mail and not only receive content. So instead use this as emails.e-blast.blade.php

...
<body>
<p><small>{{$common_name}}</small></p>
<p><small>{{$first_name}}</small></p>
<p><small> {{$last_name}}</small></p>
<p><small>{{$email}} </small></p>
</body>
...

And now you pass these variables in your $data when sending the mail

$data = array(
    'common_name' => "Julius Caesar"
    'first_name' => "Julius",
    'last_name' => "Caesar",
    'email' => "test@example.com"
);

Mail::send('emails.e-blast', $data, function ($message) use ($request, $data, $m_user) {

    $message->from($data->email, $data->common_name)->to($m_user->email, $m_user->common_name)->subject($request->input('subject'));

});

I didn't test the code but I hope you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM