Problem with showing images in gmail with sendgrid and laravel?
I have laravel job that sends mail with order details in it with sendgrid. Now I need to insert image of product in that mail, but not to attach it in mail, but to show image of product next to product. My images are stored in storage/app/documents folder, not in public folder and I need it to remain like that. Currently when I dump this dd($productsData); in my job I get response like this.
array:1 [ // app\Jobs\PendingOrderJob.php:61
0 => array:7 [
"category_id" => 3
"group_id" => 1
"type" => "simple"
"sku" => "New sku_0_0"
"name" => "New Product / 0"
"visible" => true
"images" => array:1 [
0 => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAMgCAIAAABwAouTAAAACXBIWXMAAA7zAAAO8wEcU5k6AAAAEXRFWHRUaXRsZQBQREYgQ3JlYXRvckFevCgAAAATdEVYdEF1dGhvcgBQREYgVG9vbHMgQUcbz3cwAAAALXpUWHREZXNjcmlwdGlvbgAACJnLKCkpsNLXLy8v1ytISdMtyc/PKdZLzs8FAG6fCPGXryy4AASJGUlEQVR42uS925Lsto4tCoBSVk33+pX9Tfv1/H13dNierkwROA8gQfAqqaqmvbp3hmNGuS6ZEkXiMjAwgP/n//5/kF8iwszMHEHiIfodAEBE/RcROQT7Wr9vvyCcviaiEEL6hbCJCOaPsD9kZiQCgAiCiFGYiN7e3p4BQgjbtokIIr6/v2/b9nw+t21DRBZhZiLato2IRGQLQX8TAJgZAF6v1x9//EFERHQcR4yRiPTTiUiA7Br83b29vR3HEUJ4PB76nRACEUUkGL3sz+0ddOlsTRjE1lNEJH9fMC0CEemb6KWmN0Gwd5D8Sh+HAAAUy5/r78QYD2H/sOzRiIgA2TXYh+YvgtR3BHoF+a0EQUQiiIjEfGuM6cL0BkGImWOMuviM5R71i3zxebVFmk+0+yVAIkIBACDpFjxfVVre/DUjlO1FqJvQvxjd3RHCtdfBDJdfMn9Xf82zl63A4vt2oGzR9FHqlvMn0Z8F/SLGaGeEAPU7/pf1p81l5N+vF9Pdl/4I8/31j+zKvUeQ8rd2oGS6tuv3xO6HaZey+NMRoJx9/ZPhI7ADiO6k25E0kzL83PILMn6s+oCIKOg72BOsfx8D9YfFHhYRxRhDCCLy1AAAABJRU5ErkJggg=="
]
]
]
and on my sendgrid template, not blade view, but template on sendgrid I have like this
{{#each products}}
<p>Product Name: {{this.name}}</p>
<p>Product Sku: {{this.sku}}</p>
<p>Product Price: {{this.price}}</p>
<p>Product Points: {{this.points}}</p>
{{#each this.images}}
<img src="{{this}}" alt="{{../name}}">
{{/each}}
{{/each}}
and when my mail arrives I get this on it
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAMgCAIAAABwAouTAAAACXBIWXMAAA7zAAAO8wEcU5k6AAAAEXRFWHRUaXRsZQBQREYgQ3JlYXRvckFevCgAAAATdEVYdEF1dGhvcgBQREYgVG9vbHMgQUcbz3cwAAAALXpUWHREZXNjcmlwdGlvbgAACJnLKCkpsNLXLy8v1ytISdMtyc/PKdZLzs8FAG6fCPGXryy4AASJGUlEQVR42uS925Lsto4tCoBSVk33+pX9Tfv1/H13dNierkwROA8gQfAqqaqmvbp3hmNGuS6ZEkXiMjAwgP/n//5/kF8iwszMHEHiIfodAEBE/RcROQT7Wr9vvyCcviaiEEL6hbCJCOaPsD9kZiQCgAiCiFGYiN7e3p4BQgjbtokIIr6/v2/b9nw+t21DRBZhZiLato2IRGQLQX8TAJgZAF6v1x9//EFERHQcR4yRiPTTiUiA7Br83b29vR3HEUJ4PB76nRACEUUkGL3sz+0ddOlsTRjE1lNEJH9fMC0CEemb6KWmN0Gwd5D8Sh+HAAAUy5/r78QYD2H/sOzRiIgA2TXYh+YvgtR3BHoF+a0EQUQiiIjEfGuM6cL0BkGImWOMuviM5R71i3zxebVFmk+0+yVAIkIBACDpFjxfVVre/
instead of image. What is wrong, how do I convert this encoded data to show image? Any help is appreciated. Here is my code.
OrderController.php
public function store(Request $request)
{
$cart = Cart::with('products.category')->findOrFail($request->input('cart_token'));
$user = Auth::user();
$order = Order::create([
'user_id' => $user->id,
'note' => $request->input('note'),
'status' => 'pending',
]);
$rentPeriod = CartManager::rentPeriod();
$factor = (is_null($rentPeriod))
? CartManager::duration()
: $rentPeriod->price_factor;
$order
->products()
->sync(
$cart->products
->mapWithKeys(function ($product) use ($factor) {
return [
$product->id => [
'quantity' => $product->pivot->quantity,
'price' => $product->price + ($factor * $product->category->price)
]
];
})
->toArray()
);
dispatch(new PendingOrderJob($user, $user->email, $order, $cart));
return $request->input();
}
PendingOrderJob.php
<?php
namespace App\Jobs;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use SendGrid;
use SendGrid\Mail\Mail;
use Illuminate\Support\Facades\Response;
class PendingOrderJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $user;
public $email;
public $order;
public $cart;
public function __construct($user, $email, $order, $cart)
{
$this->user = $user;
$this->email = $email;
$this->order = $order;
$this->cart = $cart;
}
public function handle()
{
$user = $this->user;
$email_template = config('sendgrid.templates.events.pending_order');
$productsData = [];
foreach ($this->order->products as $product) {
$images = [];
foreach ($product->images as $image) {
// Get the absolute path of the image
$imagePath = Storage::path($image->path);
$imageData = base64_encode(file_get_contents($imagePath)); // Convert image to base64
$imageSrc = 'data:image/' . pathinfo($imagePath, PATHINFO_EXTENSION) . ';base64,' . $imageData; // Create data URL
$images[] = $imageSrc;
}
$productData = [
'category_id' => $product->category_id,
'group_id' => $product->group_id,
'type' => $product->type,
'sku' => $product->sku,
'name' => $product->name,
'visible' => $product->visible,
'images' => $images,
];
$productsData[] = $productData;
}
//dd($productsData);
$price = [];
$quantity = [];
foreach ($this->order->products as $product) {
$price[] = $product->pivot->price;
$quantity[] = $product->pivot->quantity;
}
$cartData = [
'user_id' => $this->cart->user_id,
'start_date' => $this->cart->start_date,
'end_date' => $this->cart->end_date,
'points_required' => $this->cart->points_required,
'price' => $this->cart->price
];
$email = new Mail();
$email->setFrom(config('sendgrid.credentials.email', ''), config('sendgrid.credentials.name', ''));
$email->setSubject("Pending Order");
$email->addTo($user->email, 'toolbox');
$email->addDynamicTemplateDatas([
'user_email' => $user->email,
'order_id' => $this->order->id,
'status' => $this->order->status,
'note' => $this->order->note,
'price' => $price,
'quantity' => $quantity,
'cart' => $cartData,
'products' => $productsData,
]);
$email->setTemplateId($email_template);
//dd($email);
$sendgrid = new SendGrid(config('sendgrid.credentials.secret_key'));
try {
$response = $sendgrid->send($email);
} catch (Exception $e) {
Log::error('Pending Order Job Failed: ' . $e->getMessage());
}
}
}
Please or to participate in this conversation.