2017-02-28 77 views
7

我刚刚将laravel的5.2安装升级到5.3,然后按照官方升级方法升级到5.4laravel 5.4在邮件中嵌入图片

我现在正尝试使用其中一项新功能来创建降价格式电子邮件。

根据文档中发现的:https://laravel.com/docs/5.4/mail#view-data

To embed an inline image, use the embed method on the $message variable within your email template. Laravel automatically makes the $message variable available to all of your email templates, so you don't need to worry about passing it in manually:

然而,这样的:

<img src="{{ $message->embed(public_path().'/img/official_logo.png') }}"> 

将产生以下错误:

Undefined variable: message

我缺少的东西?或者在升级指南中有没有文档记录?

后来编辑:

我打电话的电子邮件功能,具有:

\Mail::to($user)->send(new WelcomeCandidate($user, $request->input('password')));

而且WelcomeCandidate样子:

<?php 

namespace App\Mail; 

use Illuminate\Bus\Queueable; 
use Illuminate\Mail\Mailable; 
use Illuminate\Queue\SerializesModels; 
use Illuminate\Contracts\Queue\ShouldQueue; 

use App\Models\User; 

class WelcomeCandidate extends Mailable 
{ 

    use Queueable, SerializesModels; 

    public $user; 
    public $password; 

    /** 
    * Create a new message instance. 
    * 
    * @return void 
    */ 
    public function __construct(User $user, $password) 
    { 
     // 
     $this->user = $user; 
     $this->password = $password; 
    } 

    /** 
    * Build the message. 
    * 
    * @return $this 
    */ 
    public function build() 
    { 
     $this->subject('Welcome!'); 
     return $this->markdown('emails.welcome-candidate'); 
    } 
} 
+0

表现出一些更多的代码,但什么错误是说是相当清楚的。您没有在您的邮寄类或通知类中定义邮件变量,也没有在邮件/通知的构造函数中为其分配任何值。另外,如果你要使用降价,你不需要图像标签。 – Christophvh

+0

我按照说明操作,如引用文本中所示。我应该提供哪些代码? –

+0

你在哪里执行你的mailable类的代码:例如: '$ message =“hello”; Mail :: to($ request-> user()) - > send(new OrderShipped($ message));' – Christophvh

回答

9

看来,旧的$ MESSAGE->嵌入与Markdown电子邮件不能很好地配合使用。 Like you mentioned in the comments it seems broken since 5.4

但你可能只是试试你这样的降价电子邮件中:

This is your logo 
![Some option text][logo] 

[logo]: {{asset('/img/official_logo.png')}} "Logo" 

像如下所示: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#images

资产函数参考:https://laravel.com/docs/5.4/helpers#method-asset

+0

已经尝试过。我得到一个空的img'alt here' –

+0

您是否尝试过使用{{assets('img/official_log.png')}}?也许问题在路上? – Christophvh

+0

路径是问题。我正在尝试内联方法,这可能是另一个问题。 –

0

你可以尝试以下方法:

class WelcomeCandidate extends Mailable 
{ 
    use Queueable, SerializesModels; 

    public $message; 
    public function __construct(User $user) 
    { 
     $this->user = $user; 
     $this->message = (object) array('image' => '/path/to/file'); 
    } 
} 
2

试试这个:

<img src="data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}}" alt=""> 

![](data:image/png;base64,{{base64_encode(file_get_contents(resource_path('img/email/logo.png')))}}) 
+0

我喜欢这个解决方案,但不幸的是没有骰子。 – Benco

0

解决了我的问题!

<img src="{{ $message->embed(base_path() . '/img/logo.png') }}" /> 
Controller: 


\Mail::send(['html' =>'mail'], 
     array(
      'name' => $r->name, 
      'email' => $r->email, 
      'user_message' => $r->message, 
      // 'telephone'=>$r->telephone, 
      // 'subject'=>$r->subject 
     ), function($message) use ($r) { 

      $message->to('[email protected]')->subject('Contact Form || abc.com'); 
      $message->from($r->email); 
      // ->setBody($r->user_message); // assuming text/plain 

     }); 

如果你是在本地主机,您可以使用public_path代替BASE_PATH功能