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

时间:2022-11-15 10:44:01

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,我该怎么办呢?

5 个解决方案

#1


13  

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

Mail :: send('emails.echo',$ data,function($ message){$ message-> to('foo@example.com','John Smith') - > subject('Welcome!');} );

Let me know how you get on.

让我知道你是怎么办的。

#2


1  

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

#3


1  

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

#4


0  

$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!' );
 });

#5


0  

In your controller:

在你的控制器中:

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

#1


13  

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

Mail :: send('emails.echo',$ data,function($ message){$ message-> to('foo@example.com','John Smith') - > subject('Welcome!');} );

Let me know how you get on.

让我知道你是怎么办的。

#2


1  

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

#3


1  

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

#4


0  

$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!' );
 });

#5


0  

In your controller:

在你的控制器中:

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