【问题标题】:How to Send email with html templates in laravel 5?如何在 laravel 5 中使用 html 模板发送电子邮件?
【发布时间】:2018-06-19 06:25:33
【问题描述】:

我想尝试像在 php 中发送的简单电子邮件,但我在 Laravel 5 中获得基于模板的电子邮件发送??

`$to      = Session::get('email');
 $subject = 'Order confirmation';
 $headers = "MIME-Version: 1.0\r\n";
 $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
 $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
 $headers .= "From: Test <rajdipvekriya1992@gmail.com>"; 
 $message = 'test body';
 mail($to, $subject, $message, $headers);`

但我想获得基于模板的 body html 传递 $message 之类的

 $body = $this->load-
 >view('admin/email_template/test_template',$data,TRUE);
 $this->email->message($body);
 $this->email->send();

【问题讨论】:

    标签: laravel email templates


    【解决方案1】:

    看看这段代码,这一定会对你有所帮助。

    <?php
       $to = "somebody@example.com";
       $subject = "My subject";
       $txt = "Hello world!";
       $headers = "From: webmaster@example.com" . "\r\n" .
       "CC: somebodyelse@example.com";
       mail($to,$subject,$txt,$headers);
    ?> 
    

    【讨论】:

    • 不鼓励在 StackOverflow 上使用纯代码的答案。请发布简短说明,说明您尝试使用此 sn-p 完成的工作以及它究竟有什么帮助。谢谢。
    【解决方案2】:

    使用 make 命令创建一个 Mailable 类

    php artisan make:mail
    

    然后在handle方法中编写你的代码

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
      return $this->view('emails.complaint-reply')
            ->subject('Cotint Group')
            ->with(['complaint'=>$this->complaint]);
    }
    

    emails.complaint-reply.blade.php 文件中写下你的HTML 模板

    【讨论】:

      【解决方案3】:

      我认为 Mail::raw 是你需要的:

      Mail::raw('Text to e-mail', function($message)
      {
          $message->from('us@example.com', 'Laravel');
          $message->to('foo@example.com')->cc('bar@example.com');
      });
      

      https://laravel.com/docs/5.0/mail

      【讨论】:

        【解决方案4】:

        像这样使用 Mail::send

        \Mail::send('view', $data, function ($message)
        {
            $message->subject('Email Subject');
            $message->from('acb@example.com');
            $message->to('xyz@example.com');
        });
        

        并创建view.blade.php,写入laravel Blade。

        【讨论】:

          猜你喜欢
          • 2016-11-03
          • 1970-01-01
          • 2017-03-06
          • 2015-10-01
          • 1970-01-01
          • 2012-05-17
          • 2014-05-24
          • 2019-06-11
          • 2016-01-27
          相关资源
          最近更新 更多