在 Laravel 中使用自定义 HTML 发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24905202/
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-09-14 09:51:08  来源:igfitidea点击:

Sending e-mail in Laravel with custom HTML

phpemaillaravel

提问by Carlos Goce

I need to send e-mails but I already have the HTML generated, I don't want to use laravel blade because I need to apply a CSS Inliner to the HTML, so this is how i generate the html:

我需要发送电子邮件,但我已经生成了 HTML,我不想使用 Laravel Blade,因为我需要对 HTML 应用 CSS Inliner,所以这就是我生成 html 的方式:

getRenderedView($viewData) {
    //code
    $html =  View::make('email.notification', $viewData)->render();
    $this->cssInliner->setCSS($this->getCssForNotificationEmail());
    $this->cssInliner->setHTML($html);
    return $this->cssInliner->convert();
}

So, to send the mail with Laravel you usually do something like this:

因此,要使用 Laravel 发送邮件,您通常会执行以下操作:

Mail::send('emails.welcome', $data, function($message)
{
    $message->to('[email protected]', 'John Smith')->subject('Welcome!');
});

But I don't want to pass a view, I already have the html, how can I do that?

但是我不想传递视图,我已经有了 html,我该怎么做?

回答by fossman83

If I'm right in what you want to achieve, to get round this I created a view called echo.php and inside that just echo $html.

如果我对你想要实现的目标是正确的,为了解决这个问题,我创建了一个名为 echo.php 的视图,在里面只是 echo $html。

Assign your html to something like $data['html'].

将您的 html 分配给类似 $data['html'] 的内容。

Then below pass your $data['html'] to the echo view.

然后在下面将您的 $data['html'] 传递给 echo 视图。

Mail::send('emails.echo', $data, function($message) { $message->to('[email protected]', 'John Smith')->subject('Welcome!'); });

Mail::send('emails.echo', $data, function($message) { $message->to('[email protected]', 'John Smith')->subject('Welcome!'); });

Let me know how you get on.

让我知道你是怎么办的。

回答by Kenan Tumer

I want to share a tip which might help sending emails without "blade".

我想分享一个技巧,它可能有助于在没有“刀片”的情况下发送电子邮件。

Laravel Mail function is actually a wrapper for Swift. Just assign empty arrays to $template and $data, I mean the first 2 parameters of the function, then do the rest inside callback.

Laravel Mail 函数实际上是 Swift 的包装器。只需将空数组分配给 $template 和 $data,我的意思是函数的前 2 个参数,然后在回调中执行其余操作。

Mail::send([], [], function($message) use($to, $title, $email_body)
{
    $message->setBody($email_body)->to($to)->subject($title);
});

回答by Christopher Pecoraro

The body is not accessible from the closure.

无法从封闭件中接触到主体。

You can create the swift message by hand:

您可以手动创建 swift 消息:

    $message = \Swift_Message::newInstance();
    $message->setFrom($messageToParse->template['fromEmail']);
    $message->setTo($messageToParse->to['email']);
    $message->setBody($messageToParse->body);
    $message->addPart($messageToParse->body, 'html contents');
    $message->setSubject('subject');

But then you to need to create the transport:

但是你需要创建传输:

    $mailer = self::setMailer( [your transport] );
    $response =  $mailer->send($message);

回答by Ketan Akbari

$data = array( 'email' => '[email protected]', 'first_name' => 'Laravel', 
        'from' => '[email protected]', 'from_name' => 'learming' );

Mail::send( 'email.welcome', $data, function( $message ) use ($data)
{
 $message->to( $data['email'] )->from( $data['from'], 
 $data['first_name'] )->subject( 'Welcome!' );
 });

回答by Sachin Aghera

In your controller:

在您的控制器中:

Mail::to("xyz.gmail.com")->send(new contactMailAdmin($userData));