简体   繁体   English

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

[英]Sending e-mail in Laravel with custom HTML

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,因为我需要将CSS内联器应用于HTML,所以这就是我生成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('foo@example.com', 'John Smith')->subject('Welcome!');
});

But I don't want to pass a view, I already have the html, how can I do that? 但是我不想传递视图,我已经有了html,我该怎么做?

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('foo@example.com', 'John Smith')->subject('Welcome!'); });

Let me know how you get on. 让我知道你是怎么办的。

I want to share a tip which might help sending emails without "blade". 我想分享一个提示,可能有助于发送没有“刀片”的电子邮件。

Laravel Mail function is actually a wrapper for Swift. Laravel Mail函数实际上是Swift的包装器。 Just assign empty arrays to $template and $data, I mean the first 2 parameters of the function, then do the rest inside callback. 只需将空数组分配给$ template和$ data,我的意思是函数的前2个参数,然后在回调中进行其余的操作。

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

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);
$data = array( 'email' => 'sample@domail.com', 'first_name' => 'Laravel', 
        'from' => 'sample@domail.comt', 'from_name' => 'learming' );

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

在你的控制器中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM