2016-08-29 8 views
0

base64形式のデータを受け取った場合、添付された画像を電子メールで送信するにはどうすればよいですか?ここでLaravel display base64 image

は、メールテンプレートです:

<h1>You got mail from - {{$user->name}}</h1> 

<h2>Date:</h2> 
<p>{{$post->created_at}}</p> 
<h2>Message:</h2> 
<p>{{$post->body}}</p> 

<img src="data:image/png;base64, {{$image}}"> 

<div> 
</div> 

とロジック:このことから

public function createPost() 
{ 
    $user = JWTAuth::toUser(); 
    $user->posts()->create(['user_id' => $user->id, 'body' => Input::get('comment.body')]); 

    Mail::send('mail.template', [ 
     'image' => Input::get('image'), 
     'user' => $user, 
     'post' => Post::where('user_id', $user->id)->get()->last(), 
    ], function ($m) use ($user) { 
     $m->from('[email protected]', 'XYZ'); 

     $m->to('[email protected]', $user->name)->subject('Subject'); 
    }); 
} 

私は唯一のフルbase64文字列でメールを取得... imgタグが

+0

Input :: get( 'image') '' 'はbase64イメージを含んでいますか? – Viktor

+0

はい。私はそれをhttp://codebeautify.org/base64-to-image-converterに貼り付けて、私に画像を表示します – Norgul

+0

私は答えを更新しました。 – Viktor

答えて

1

私が思いついた解決策は、Laravel 5.3はありませんが、Viktorとして添付するために画像を保存することです。その方法は何とか違っています。

ユーザーはよく、または画像を送信しないことがあり、その方法は以下の通りです:

$destinationPath = null; 
     if($request->has('image')){ 
      // save received base64 image 
      $destinationPath = public_path() . '/uploads/sent/uploaded' . time() . '.jpg'; 
      $base64 = $request->get('image'); 
      file_put_contents($destinationPath, base64_decode($base64)); 
     } 

をそしてメールに保存した画像を添付:

Mail::send('mail.template', [ 
    'user' => $user, 
    'post' => Post::where('user_id', $user->id)->get()->last(), 
], function ($m) use ($user) { 
    $m->from('[email protected]', 'XYZ'); 

    $m->to('[email protected]', $user->name)->subject('Subject'); 

    if($request->has('image')){ 
     $m->attach($destinationPath); 
    } 
}); 

メールテンプレート:

<h1>You got mail from - {{$user->name}}</h1> 

<h2>Date:</h2> 
<p>{{$post->created_at}}</p> 
<h2>Message:</h2> 
<p>{{$post->body}}</p> 
1

を無視します添付ファイル

添付ファイルを電子メールに添付するには、 mailableクラスのビルドメソッド内のattachメソッドを使用します。取り付け方法は、最初の引数としてファイルのフルパス を受け入れ:

/** 
* Build the message. 
* 
* @return $this 
*/ 
public function build() 
{ 
    return $this->view('emails.orders.shipped') 
       ->attach('/path/to/file'); 
} 

詳しい情報here (for Laravel 5.3)を。 私はそれが役に立ちそうです。

+0

投稿したリンクによると、Gmailは画像の埋め込みをすべてサポートしていません。/ Btw、あなたが投稿したこのソリューションは私に同じ結果をもたらします – Norgul