【问题标题】:Laravel displaying base64 imageLaravel 显示 base64 图像
【发布时间】:2016-08-29 14:12:29
【问题描述】:

如果我收到 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 标签被忽略

【问题讨论】:

标签: php laravel gmail base64


【解决方案1】:

附件

要向电子邮件添加附件,请使用附件中的附加方法 可邮寄类的构建方法。 attach 方法接受完整路径 将文件作为其第一个参数:

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

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

【讨论】:

  • 嗯,根据您发布的链接,gmail 根本不支持嵌入图片 =/ 顺便说一句,您发布的这个解决方案给了我相同的结果
【解决方案2】:

我想出的解决方案是先保存图像,以便按照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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    • 2021-04-15
    • 2019-06-25
    • 2013-01-31
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多