无法在Laravel 5.8中发送HTML电子邮件

时间:2019-07-13 16:44:55

标签: php email html-email laravel-5.8

我很难通过Laravel应用程序发送HTML电子邮件。我试图返回视图,但返回效果很好,但是当我将电子邮件发送到实际电子邮件时,它作为纯文本电子邮件到达,而没有所有HTML解析。

电子邮件的“我的视图”布局如下:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">

<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Montserrat&display=swap" 
rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" 
  rel="stylesheet">
<!-- Bootstrap CSS File -->
<link href="{{ url('design-assets/lib/bootstrap/css/bootstrap.min.css')}}" 
 rel="stylesheet">

 <!-- Main Stylesheet File -->
 <link href="{{ url('design-assets/css/style.css" rel="stylesheet')}}">
 <link href="{{ url('design-assets/css/prof-custom.css')}}" 
 rel="stylesheet">
 @yield('styles')
</head>

<body id="body">
@yield('content')
<!-- JavaScript Libraries -->
<script src="{{ url('design-assets/lib/jquery/jquery.min.js')}}"></script>
<script src="{{ url('design-assets/lib/jquery/jquery-migrate.min.js')}}"> 
</script>
 <script src="{{ url('design-assets/lib/bootstrap/js/bootstrap.bundle.min.js') }}/"></script>

<!-- Contact Form JavaScript File -->
<!-- Template Main Javascript File -->
<script src="{{ url('design-assets/js/main.js') }}"></script>
@yield('scripts')
</body>

The actual email view is as follows:

@extends('layouts.mail')
@section('content')
<div class="card montserrat-font">
<div class="card-header">
    <div class="card-title">Contact Message from the Website</div>
</div>
<div class="card-body">
    <div class="card-text">
        Dear Sir,<br /><br />
    We have a contact from our events website with the following details<br/><br />
    </div>
    <div class="table-responsive">
        <table class="table table-striped">
            <tr>
                <th>Name of Sender:</th><td>{{$mail_info['name']}}</td>
            </tr>
            <tr>
                <th>Email of Sender:</th><td>{{$mail_info['email']}}</td>
            </tr>
            <tr>
                <th>Phone number of Sender:</th><td>{{$mail_info['phone']}}</td>
            </tr>
            <tr>
                <th>Subject of Message:</th><td>{{$mail_info['subject']}}</td>
            </tr>                       
            {{--  <tr>
                <th>Message:</th><td>{{$mail_info['message']}}</td>
            </tr>  --}}
        </table>
    </div>
    <div class="card-title">Message body</div>
    <div class="card-text">
        {!! $mail_info['message'] !!}
    </div>
</div>
</div>
@endsection

问题是,当我通过按以下方式返回视图来检查从电子邮件视图返回的格式时:

 return new \App\Mail\ContactMail(['email' => 'testemail@gmail.com', 'name' => 'Testing Name','phone'=>'08033776502', 'subject' => 'Just a test', 'message' => "This is  a message returned from testing the view email template"]);

我得到的视图恰好代表了我想要的内容,但是当我发送电子邮件时,它以纯文本电子邮件的形式到达

这就是我通过可邮寄类调用视图的方式

<?php

namespace App\Mail;

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

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public $mail_info;

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

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    // return $this->view('view.name');
    return $this->view('mails.web-contact', ['mail_info'=>$this->mail_info]);
}
}

,然后通过控制器进行如下操作:

 public function post_contact(Request $request)
{
    try
    {
        $data = $request->all();
        $this->validate_recaptcher($data['g-recaptcha-response']);
        $this->validator($request->all())->validate();
        \Mail::to('uchendukwe@yahoo.com')->send(new \App\Mail\ContactMail(['email' => $request->email, 'name' => $request->name,'phone'=>$request->phone, 'subject' => $request->subject, 'message' => $request->message]));
        return redirect()->route('ContactForm')->with('msg',"<div class='alert alert-success'><span class='fa fa-check'></span> Message successfully sent. I will get back to you soon if necessary</div>");
        // return new \App\Mail\ContactMail(['email' => $request->email, 'name'=> $request->name,'phone'=>$request->phone, 'subject' => $request->subject, 'message' => $request->message]);
    }
        catch(Exception $e)
    {
        return back()->withInputs()->with('msg',"<div class='alert alert-danger'><span class='fa fa-warning'></span> ".$e->getMessage()."</div>");
    }        
}

我正在使用smtp电子邮件驱动程序,并且其他所有功能都按预期工作。

我将感谢任何解决此问题的指南

谢谢

1 个答案:

答案 0 :(得分:1)

在电子邮件布局的head部分内联添加样式。邮件客户端倾向于忽略外部引用的CSS。示例:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">

...

<style type="text/css">
    your custom styles here
</style>

...

</head>
</html>

您可以使用Markdown完成此操作。

  

自定义CSS   导出组件后,resources / views / vendor / mail / html / themes目录将包含default.css文件。您可以在此文件中自定义CSS,样式会自动内嵌在Markdown邮件的HTML表示中。