Sending e-mail in Laravel with custom HTML

后端 未结 5 918
死守一世寂寞
死守一世寂寞 2021-01-06 02: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

相关标签:
5条回答
  • 2021-01-06 02:14
    $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!' );
     });
    
    0 讨论(0)
  • 2021-01-06 02:15

    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.

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

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

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

    Let me know how you get on.

    0 讨论(0)
  • 2021-01-06 02:21

    In your controller:

    Mail::to("xyz.gmail.com")->send(new contactMailAdmin($userData));
    
    0 讨论(0)
  • 2021-01-06 02:30

    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.

    Mail::send([], [], function($message) use($to, $title, $email_body)
    {
        $message->setBody($email_body)->to($to)->subject($title);
    });
    
    0 讨论(0)
  • 2021-01-06 02:31

    The body is not accessible from the closure.

    You can create the swift message by hand:

        $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);
    
    0 讨论(0)
提交回复
热议问题