Php 无法在Laravel 5.8中发送HTML电子邮件
我很难通过我的Laravel应用程序发送HTML电子邮件。我曾尝试返回视图,它返回得很好,但当我将电子邮件发送到实际电子邮件时,它以纯文本电子邮件的形式到达,而没有进行所有HTML解析 我的电子邮件视图布局如下所示:Php 无法在Laravel 5.8中发送HTML电子邮件,php,email,html-email,laravel-5.8,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
<!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>
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>");
}
}
我得到的视图正好代表了我想要的内容,但当我发送电子邮件时,它以纯文本电子邮件的形式到达
这就是我通过mailable类调用视图的方式
<?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]);
}
}
在电子邮件版面的标题部分内联添加样式。邮件客户端倾向于忽略外部引用的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>
...
你的定制风格在这里
...
您可以使用
定制CSS
导出组件后,resources/views/vendor/mail/html/themes目录将包含一个default.css文件。您可以自定义此文件中的CSS,并且您的样式将自动在标记邮件消息的HTML表示中对齐
你到底是如何发送电子邮件的?@ADyson,我已经更新了我的问题来说明这一点。谢谢你,好的。那么,当电子邮件到达时,您实际看到了什么?你能看到屏幕上打印的所有HTML标记而不是用来控制布局吗?@ADyson,我看不到打印的标记。它看起来就像一封普通的文本电子邮件。您查看过邮件的来源吗?里面有HTML标签吗?什么是mime类型?您测试过哪些邮件客户端?它们在呈现结果的方式上可能存在很大差异。也许它是HTML,但只是没有使用CSS,因为它的链接是外部的。很多邮件客户不喜欢这样。如果你阅读了HTML电子邮件,它总是建议你使用内联样式,或者至少在邮件正文中添加一个
块。但是@Adam,在Laravel中,这是发送HTML电子邮件的正常做法way@Josh拉威尔之路:
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>");
}
}
<!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>