Answer the question
In order to leave comments, you need to log in
Laravel mail inline img?
Good afternoon!
I am sending emails via laravel. But my template is set through the admin panel, and not through the view. Further in the code, I check the pictures inside the message, replace them with inline and send. I use this code for this:
$this->withSwiftMessage(function ($message) use ($msg) {
$message->setBody (preg_replace_callback("|<img.*?data-id=[\'\"]{1}(\d+)[\'\"]{1}.*?>|", function ($matches) use ($message) {
$img = $matches[0];
$imgId = $matches[1];
if (($imgObj = \Backend\Root\Upload\Models\MediaFile::find($imgId))) {
$sizes = 'orig';
if (preg_match('/width=[\'\"]{1}(\d+)[\'\"]{1}/', $img, $width) && preg_match('/height=[\'\"]{1}(\d+)[\'\"]{1}/', $img, $height)) {
$sizes = \Backend\Root\Upload\Services\Uploads::sizesToStr([ $width[1], $height[1] ]);
}
if (isset($imgObj['sizes'][$sizes])) {
$fName = $message->embed(new \Swift_Image(
Storage::disk($imgObj->disk)->get($imgObj->path.$imgObj['sizes'][$sizes]['path'].$imgObj['sizes'][$sizes]['file']), $imgObj['orig_name'])
);
$img = preg_replace("/src=[\'\"]{1}.*?[\'\"]{1}/", "src=\"".$fName."\"", $img);
}
}
return $img;
}, $msg ), 'text/html' );
});
call_user_func($callback, $message);
$this->addContent($message, $view, $plain, $raw, $data);
Answer the question
In order to leave comments, you need to log in
The solution is still found on the Internet :). You need to call the code $this->view([], []); at the end. Then the $message->setBody() function will start working. I don’t know how much this is generally documented somewhere and whether the developers will break this code in the future, but so far it works :)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question