Php Laravel邮件:传递字符串而不是查看
我想用laravel发送一封确认电子邮件。 laravel Mail::send()函数似乎只接受系统上文件的路径。 问题是我的邮件模板存储在数据库中,而不是系统上的文件中 如何将普通内容传递到电子邮件 例如:Php Laravel邮件:传递字符串而不是查看,php,email,laravel,laravel-4,swiftmailer,Php,Email,Laravel,Laravel 4,Swiftmailer,我想用laravel发送一封确认电子邮件。 laravel Mail::send()函数似乎只接受系统上文件的路径。 问题是我的邮件模板存储在数据库中,而不是系统上的文件中 如何将普通内容传递到电子邮件 例如: $content = "Hi,welcome user!"; Mail::send($content,$data,function(){}); Mailer类将字符串传递给addContent,该字符串通过各种其他方法调用views->make()。因此,直接传递内容字符串将不起作用
$content = "Hi,welcome user!";
Mail::send($content,$data,function(){});
Mailer类将字符串传递给
addContent
,该字符串通过各种其他方法调用views->make()
。因此,直接传递内容字符串将不起作用,因为它将尝试以该名称加载视图
您需要做的是创建一个视图,该视图只响应$content
// mail-template.php
<?php echo $content; ?>
更新:在Laravel 5中,您可以使用
raw
:
Mail::raw('Hi, welcome user!', function ($message) {
$message->to(..)
->subject(..);
});
这就是你如何做到的:
Mail::send([], [], function ($message) {
$message->to(..)
->subject(..)
// here comes what you want
->setBody('Hi, welcome user!'); // assuming text/plain
// or:
->setBody('<h1>Hi, welcome user!</h1>', 'text/html'); // for HTML rich messages
});
Mail::send([],[],函数($message){
$message->to(..)
->主题(……)
//这是你想要的
->setBody('Hi,welcome user!');//假设为text/plain
//或:
->setBody('Hi,welcome user!','text/html');//用于html丰富的消息
});
用于Html电子邮件
Mail::send(array(), array(), function ($message) use ($html) {
$message->to(..)
->subject(..)
->from(..)
->setBody($html, 'text/html');
});
这与问题没有直接关系,但对于那些在保留自定义HTML版本的同时搜索设置电子邮件的纯文本版本的问题,您可以使用以下示例:
Mail::raw([], function($message) {
$message->from('contact@company.com', 'Company name');
$message->to('johndoe@gmail.com');
$message->subject('5% off all our website');
$message->setBody( '<html><h1>5% off its awesome</h1><p>Go get it now !</p></html>', 'text/html' );
$message->addPart("5% off its awesome\n\nGo get it now!", 'text/plain');
});
希望它能帮助您。如果您正在使用邮件。您可以在构建方法中执行以下操作:
public function build()
{
return $this->view('email')
->with(['html'=>'This is the message']);
}
您只需在资源文件夹中创建blade视图email.blade.php
然后在blade中,可以使用laravel blade语法引用字符串
{{$html}}
或
{!!$html!!}
如果原始文本包含HTML标记
我希望这对那些将模板存储在数据库中并希望利用Laravel中的Mailables类的人有效。要使用Laravel Mailables发送原始html、文本等,您可以 在邮件中重写Mailable->send(),并在其中使用前面响应中的方法:
send([], [], function($message){ $message->setBody() } )
根本不需要在构建函数中调用$this->view()
只有邮件才能排队
也就是说,如果使用ShouldQueue
接口
1) 首先,你应该一直这样做
php artisan queue:restart
2) 其次,在邮件中,您可以使用html
方法(在laravel 5.8中测试)
公共函数构建():self
{
退还$this
->html('
转发电子邮件
')
->主题(配置('app.name')、'email forwarded')
->attachData($this->content'email.eml'[
'mime'=>'应用程序/eml',
]);
}
试试看
我有一个类似的问题,我的电子邮件的HTML和/或纯文本不是由视图生成的,我不想为它们创建一个虚拟视图(如@Matthew Odedoyin所建议的) 正如其他人所评论的,您可以使用
$this->html()
设置邮件的html内容,但是如果您希望您的电子邮件同时包含html和纯文本内容,该怎么办
不幸的是,$this->text()
只提供了一个视图,但我通过使用:
$this->text(new HtmlString('Here is the plain text content'));
它呈现的是
HTMLString的内容,而不是视图。这可以在一个可邮寄的实现中完成,包括纯文本和html内容部分:
public function build() {
// Text and html content sections we wish to use in place of view output
$bodyHtml = ...
$bodyText = ...
// Internally, Mailer::renderView($view) interprets $view as the name of a blade template
// unless, instead of string, it is set to an object implementing Htmlable,
// in which case it returns the result $view->toHtml()
$htmlViewAlternative = new class($bodyHtml) implements Htmlable {
protected string $html;
public function __construct($html) {
$this->html = $html;
}
public function toHtml(): string {
return $this->html;
}
};
// We can now set both the html and text content sections without
// involving blade templates. One minor hitch is the Mailable::view($view)
// documents $view as being a string, which is incorrect if you follow
// the convoluted downstream logic.
/** @noinspection PhpParamsInspection */
return $this
->to(...)
->from(...)
->subject(...)
->view([
'html' => $htmlViewAlternative,
'raw' => $bodyText
]);
}
不是很埃尔甘特,我更喜欢雅瑞克·特卡奇克的方式:)这个答案很容易误导,不知道op为什么接受这个答案。Jarek Tkaczyk和Sajjad Ashraf已经正确地回答了这个问题。因此,您可以设置具有不同内容类型的多个主体,也可以设置包含text/html
和text/plain
部分的多部分电子邮件。@Jason函数()对普通部分使用addPart()
,而不是setBody()
当HTML部分也被设置好时,你能帮我解决这个问题吗?$HTML=“Hello{{{username}}”,我如何动态传递username?有关HTML电子邮件(需要额外参数),请参阅@sajjad ashraf的回答。合并可能会很好?是的,正如@Sajjad的回答中所添加的那样,在这种情况下是否可以使用onConnection?以及如何在laravel 5中设置text/html??不幸的是,如果您想邮件::将邮件排入队列,则其将不起作用,从而引发InvalidArgumentException异常“只有邮件才能进入队列。”,这与问题无关。这是唯一对我有效的答案,因为我正在提前构建邮件,所以我不能使用send()方法。令人失望的是,当HTML已经可以作为字符串添加时,仅仅制作一封多部分电子邮件就需要这种解决方法。这正是我所需要的。Mailtrap有一个“文本”选项卡,显示电子邮件的纯文本版本,供任何使用Mailtrap进行测试的人使用。谢谢,这很有帮助!这在版本8中有效。好主意!
public function build(): self
{
return $this
->html('
<html>
<body>
ForwardEmail
</body>
</html>
')
->subject(config('app.name') . ' ' . 'email forwarded')
->attachData($this->content, 'email.eml', [
'mime' => 'application/eml',
]);
}
public function build()
{
$message = 'Hi,welcome user!'
return $this->html($message)->subject($message);
}
$this->text(new HtmlString('Here is the plain text content'));
public function build() {
// Text and html content sections we wish to use in place of view output
$bodyHtml = ...
$bodyText = ...
// Internally, Mailer::renderView($view) interprets $view as the name of a blade template
// unless, instead of string, it is set to an object implementing Htmlable,
// in which case it returns the result $view->toHtml()
$htmlViewAlternative = new class($bodyHtml) implements Htmlable {
protected string $html;
public function __construct($html) {
$this->html = $html;
}
public function toHtml(): string {
return $this->html;
}
};
// We can now set both the html and text content sections without
// involving blade templates. One minor hitch is the Mailable::view($view)
// documents $view as being a string, which is incorrect if you follow
// the convoluted downstream logic.
/** @noinspection PhpParamsInspection */
return $this
->to(...)
->from(...)
->subject(...)
->view([
'html' => $htmlViewAlternative,
'raw' => $bodyText
]);
}