Laravel 显示 base64 图片

Laravel displaying base64 image

如果我收到 base64 格式的数据,我如何发送带有附件图像的电子邮件?

这是邮件模板:

<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('xyz@app.com', 'XYZ');

        $m->to('xyz@gmail.com', $user->name)->subject('Subject');
    });
}

从这里我只收到带有完整 base64 字符串的邮件...img 标签被忽略

Attachments

To add attachments to an email, use the attach method within the mailable class' build method. The attach method accepts the full path to the file as its first argument:

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

更多信息here (for Laravel 5.3)。 希望对您有所帮助。

我想到的解决方案是先保存图像,以便按照 Viktor 的建议附加它,尽管我没有 Laravel 5.3。所以方法有点不同。

用户可以发也可以不发,所以方法如下:

$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('xyz@app.com', 'XYZ');

    $m->to('xyz@gmail.com', $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>