php Laravel 邮件:传递字符串而不是视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26139931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:10:39  来源:igfitidea点击:

Laravel mail: pass string instead of view

phpemaillaravellaravel-4swiftmailer

提问by nexana

I want to send a confirmation e-mail using laravel. The laravel Mail::send() function only seems to accept a path to a file on the system. The problem is that my mailtemplates are stored in the database and not in a file on the system.

我想使用 laravel 发送确认电子邮件。laravel Mail::send() 函数似乎只接受系统上文件的路径。问题是我的邮件模板存储在数据库中,而不是系统上的文件中。

How can I pass plain content to the email?

如何将纯内容传递给电子邮件?

Example:

例子:

$content = "Hi,welcome user!";

Mail::send($content,$data,function(){});

回答by Jarek Tkaczyk

update: In Laravel 5you can use rawinstead:

更新:在Laravel 5 中,您可以使用raw

Mail::raw('Hi, welcome user!', function ($message) {
  $message->to(..)
    ->subject(..);
});


This is how you do it:

这是你如何做到的:

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
});

回答by Sajjad Ashraf

For Html emails

对于 HTML 电子邮件

Mail::send(array(), array(), function ($message) use ($html) {
  $message->to(..)
    ->subject(..)
    ->from(..)
    ->setBody($html, 'text/html');
});

回答by Ben Swinburne

The Mailer class passes a string to addContentwhich via various other methods calls views->make(). As a result passing a string of content directly won't work as it'll try and load a view by that name.

Mailer 类传递一个字符串addContent,通过各种其他方法调用该字符串views->make()。因此,直接传递内容字符串将不起作用,因为它会尝试按该名称加载视图。

What you'll need to do is create a view which simply echos $content

你需要做的是创建一个简单的回声视图 $content

// mail-template.php
<?php echo $content; ?>

And then insert your string into that view at runtime.

然后在运行时将您的字符串插入到该视图中。

$content = "Hi,welcome user!";

$data = [
    'content' => $content
];

Mail::send('mail-template', $data, function() { });

回答by Anwar

It is not directly related to the question, but for the ones that search for setting the plain text version of your email while keeping the custom HTML version, you can use this example :

它与问题没有直接关系,但对于那些在保留自定义 HTML 版本的同时搜索设置电子邮件的纯文本版本的问题,您可以使用以下示例:

Mail::raw([], function($message) {
    $message->from('[email protected]', 'Company name');
    $message->to('[email protected]');
    $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');
});

If you would ask "but why not set first argument as plain text ?", I made a test and it only takes the html part, ignoring the raw part.

如果你问“但为什么不将第一个参数设置为纯文本?”,我做了一个测试,它只需要 html 部分,忽略原始部分。

If you need to use additional variable, the anonymous function will need you to use use()statement as following :

如果您需要使用附加变量,匿名函数将需要您使用use()如下语句:

Mail::raw([], function($message) use($html, $plain, $to, $subject, $formEmail, $formName){
    $message->from($fromEmail, $fromName);
    $message->to($to);
    $message->subject($subject);
    $message->setBody($html, 'text/html' ); // dont miss the '<html></html>' or your spam score will increase !
    $message->addPart($plain, 'text/plain');
});

Hope it helps you folks.

希望对各位大侠有所帮助。

回答by jeidison farias

try

public function build()
{
    $message = 'Hi,welcome user!'
    return $this->html($message)->subject($message);
}

回答by Matthew Odedoyin

If you were using mailables. You can do something like this in the build method :

如果您使用的是可邮寄的。你可以在 build 方法中做这样的事情:

public function build()
{

    return $this->view('email')
        ->with(['html'=>'This is the message']);
}

And you just go ahead and create the blade view email.blade.phpin your resource folder.

您只需继续email.blade.php在您的资源文件夹中创建刀片视图。

Then in the blade you can reference your string using laravel blade syntax

然后在刀片中,您可以使用 laravel 刀片语法引用您的字符串

 <html>
    <body>
      {{$html}}
    </body>
  </html>

or

或者

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

If your raw text contains html mark up I hope this works for those who have templates stored in the database and wants to take advantage of the Mailables class in Laravel.

如果您的原始文本包含 html 标记,我希望这适用于那些将模板存储在数据库中并希望利用 Laravel 中的 Mailables 类的人。

回答by Yevgeniy Afanasyev

as you know

如你所知

Only mailables may be queued.

只有可邮寄物品可以排队。

meaning, if you use ShouldQueueinterface

意思是,如果你使用ShouldQueue接口

1) first, you should always do

1)首先,你应该总是这样做

php artisan queue:restart

2) second, in your mailable you can use htmlmethod (tested in laravel 5.8)

2)第二,在你的邮件中你可以使用html方法(在laravel 5.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',
    ]);
}

回答by patrox

To send raw html, text etc using Laravel Mailables you can

要使用 Laravel Mailables 发送原始 html、文本等,您可以

override Mailable->send() in your Mailable and in there, use the method in previous responses:

在您的 Mailable 中覆盖 Mailable->send() 并在那里使用先前响应中的方法:

send([], [], function($message){ $message->setBody() } )

No need to call $this->view() at your build function at all.

根本不需要在你的构建函数中调用 $this->view() 。