View Issue Details

IDProjectCategoryView StatusLast Update
15811Bug reportsSurvey editingpublic2020-04-03 08:27
ReporterWaterlijn Assigned To 
PrioritynoneSeverityminor 
Status newResolutionopen 
Product Version4.1.0 
Summary15811: Embedding images not working well
Description

There is an old bug (14343) that causes problems when there is a subdirectory and relative images in the text.
This is hard to solve because there is no variable that has the path separately. But the text editor gives the image in two formats. When it is a general image, it's a full url like 'https://domain/path/image'. When it is an image from the survey itself, the relative path is given.

So this difference in picking the image should be solved first (or add the domain manually). Ones it is a full url, the following function can be added to: '/application/core/LimeMailer.php'
This function will replace the url with base64 data and this will be converted to an embedded image by the parent function (in phpMailer).

function msgHTML($message, $basedir = '', $advanced = false) {
    preg_match_all('/(?<!-)(src|background)=["\']http(.*)["\']/Ui', $message, $images);
    if (isset($images[2])) {
        $replacements = [];
        foreach ($images[2] as $imgindex => $url) {
            $url = "http".$url;
            preg_match('/(?P<ext>gif|jpe?g|png)/i', $url, $match);
            if (isset($match["ext"])) {
                switch ($match["ext"]) {
                    case "jpeg":
                    case "jpg":
                        $mime = "image/jpeg";
                        break;
                    default:
                        $mime = "image/".$match["ext"];
                        break;
                }
                if ($im = file_get_contents($url)) {
                    $replacements["/".preg_quote($url, "/")."/"] = "data:{$mime};base64,".base64_encode($im);
                }
            }
        }
        if (count($replacements)) {
            $message = preg_replace(array_keys($replacements), $replacements, $message);
        }
    }
    return parent::msgHTML($message, $basedir, $advanced);
}
Steps To Reproduce

Add image to the description text in a survey and add the text to a email template.

TagsNo tags attached.
Complete LimeSurvey version number (& build)Versie 4.1.0+200128
I will donate to the project if issue is resolvedNo
BrowserAny
Database & DB-Versionnot important
Server OS (if known)not important
Webserver software & version (if known)not important
PHP Versionnot important

Activities

Waterlijn

Waterlijn

2020-02-01 16:30

reporter   ~55612

Update: The relative url will be inserted when using 'Insert image' (upload) instead of 'Select image from server'.

DenisChenu

DenisChenu

2020-02-01 18:12

developer   ~55614

Best is to make the pull request ? No ?

DenisChenu

DenisChenu

2020-02-01 18:15

developer   ~55615

Here, you import external image too ?

Right ? Then : it can take a really more long time to send email …

Waterlijn

Waterlijn

2020-02-01 18:34

reporter   ~55617

Well, this is a solution that works for me. I personally don't want to have any external image in my mails. But it might rather be an option.
It shouldn't be slow when bug 15808 has been fixed in a proper way. The image will be added once, converted to base64 and sent to all addresses.

Waterlijn

Waterlijn

2020-02-01 20:58

reporter   ~55620

In order to prevent slowness, the images might be cached like this.

private $imageReplacements = [];
function msgHTML($message, $basedir = '', $advanced = false) {
    preg_match_all('/(?<!-)(src|background)=["\']http(.*)["\']/Ui', $message, $images);
    if (isset($images[2])) {
        foreach ($images[2] as $imgindex => $url) {
            $url = "http".$url;
            $key = "/".preg_quote($url, "/")."/";
            if (!isset($this->imageReplacements[$key])) {
                preg_match('/(?P<ext>gif|jpe?g|png)/i', $url, $match);
                if (isset($match["ext"])) {
                    switch ($match["ext"]) {
                        case "jpeg":
                        case "jpg":
                            $mime = "image/jpeg";
                            break;
                        default:
                            $mime = "image/".$match["ext"];
                            break;
                    }
                    if ($im = file_get_contents($url)) {
                        $this->imageReplacements[$key] = "data:{$mime};base64,".base64_encode($im);
                    }
                }
            }
        }
        if (count($this->imageReplacements)) {
            $message = preg_replace(array_keys($this->imageReplacements), $this->imageReplacements, $message);
        }
    }
    return parent::msgHTML($message, $basedir, $advanced);
}
DenisChenu

DenisChenu

2020-02-02 10:35

developer   ~55622

My opinion are : Best in a core plugin :)

You can check https://github.com/LimeSurvey/LimeSurvey/tree/master/application/core/plugins/mailSenderToFrom for sample

  1. Attach to beforeTokenEmail event only (in my opinion)
  2. Find image etc in body, replace it
  3. Update the email body here by
    • $this->getEvent()->set('mailer)->body = $yournewbody
    • or $this->getEvent()->set('body',$yournewbody)
DenisChenu

DenisChenu

2020-02-02 10:35

developer   ~55623

Last edited: 2020-02-02 10:36

View 2 revisions

PS : happy to help you to construct the core plugin :)

Waterlijn

Waterlijn

2020-02-02 12:28

reporter   ~55627

Created a plugin, thanks for info. :)

Issue History

Date Modified Username Field Change
2020-02-01 15:12 Waterlijn New Issue
2020-02-01 16:30 Waterlijn Note Added: 55612
2020-02-01 18:12 DenisChenu Note Added: 55614
2020-02-01 18:15 DenisChenu Note Added: 55615
2020-02-01 18:34 Waterlijn Note Added: 55617
2020-02-01 20:58 Waterlijn Note Added: 55620
2020-02-02 10:35 DenisChenu Note Added: 55622
2020-02-02 10:35 DenisChenu Note Added: 55623
2020-02-02 10:36 DenisChenu Note Edited: 55623 View Revisions
2020-02-02 12:28 Waterlijn Note Added: 55627
2020-02-05 08:52 cdorin Assigned To => cdorin
2020-02-05 08:52 cdorin Status new => assigned
2020-04-02 20:25 cdorin Assigned To cdorin =>
2020-04-03 08:27 DenisChenu Status assigned => new