2017-02-28 128 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功能