View Issue Details

IDProjectCategoryView StatusLast Update
15811Bug reportsSurvey editingpublic2021-01-13 07:55
ReporterWaterlijn Assigned To 
PrioritynoneSeverityminor 
Status newResolutionreopened 
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)4.1.0+200128
I will donate to the project if issue is resolvedNo
Sync to Zoho Project
BrowserAny
Database & DB-Versionnot important
Server OS (if known)not important
Webserver software & version (if known)not important
PHP Versionnot important

Relationships

duplicate of 14343 new Included image are not send with email if subdirectory is set 

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. :)

cdorin

cdorin

2020-12-22 15:58

manager   ~61026

Shall I have this ticket closed? :)

Waterlijn

Waterlijn

2020-12-22 16:07

reporter   ~61034

Sure

cdorin

cdorin

2020-12-22 16:10

manager   ~61037

Thanks for quick reply!

DenisChenu

DenisChenu

2020-12-22 18:17

developer   ~61054

No …
The issue persist in LimeSurvey core

Waterlijn cp,nstruct a plugin but this plugin are not public or in LimeSurvey core plugins .

Waterlijn

Waterlijn

2020-12-22 18:37

reporter   ~61056

Yeah, but the idea is that it isn't a core problem. This would rather be solved in a plugin, so I did.
You can find it here. The source is as is and have fun with it. :-)
https://github.com/Xites/LS_mailSendMessage

DenisChenu

DenisChenu

2020-12-22 21:03

developer   ~61058

Thanks for the public plugin :)

There is an old bug (14343) that causes problems when there is a subdirectory and relative images in the text.

You seems to don't use body of this event https://manual.limesurvey.org/BeforeTokenEmail , redo whole system (with HTML editor and so)
Not just the email body part.

Waterlijn

Waterlijn

2020-12-22 21:15

reporter   ~61059

Actually I didn't use the whole system for about 8 months now due to corona. But at the time it worked for me. I also wrote a plugin for DKIM, you might be interested.
https://github.com/Xites/LS_mailDKIM

DenisChenu

DenisChenu

2020-12-23 09:28

developer   ~61066

LS_mailDKIM is really great !

About LS_mailSendMessage : there are other purpose than «just» include image ? Right ?

Waterlijn

Waterlijn

2020-12-23 11:52

reporter   ~61078

Sorry my bad, wrong source. This mailSendMessage was an experiment, not for publishing.
Her is the right one: https://github.com/Xites/LS_mailEmbedImages

DenisChenu

DenisChenu

2020-12-24 10:10

developer   ~61157

Great plugin too !

Not same purpose than the original issue (include image with subdirectoy for URL)
But : maybe a better system

DenisChenu

DenisChenu

2020-12-26 14:34

developer   ~61168

@Waterlijn : you're OK if i add your plugins in our wiki ?

Waterlijn

Waterlijn

2020-12-26 14:36

reporter   ~61169

Yes sure, no problem.

DenisChenu

DenisChenu

2021-01-13 07:55

developer   ~61512

Don't find a good way …
Plugin seems the best solution

duplicate 14343: Included image are not send with email if subdirectory is set (3.X version)

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
2020-12-22 15:58 cdorin Status new => feedback
2020-12-22 15:58 cdorin Note Added: 61026
2020-12-22 16:07 Waterlijn Note Added: 61034
2020-12-22 16:07 Waterlijn Status feedback => new
2020-12-22 16:10 cdorin Assigned To => cdorin
2020-12-22 16:10 cdorin Status new => closed
2020-12-22 16:10 cdorin Resolution open => no change required
2020-12-22 16:10 cdorin Note Added: 61037
2020-12-22 18:17 DenisChenu Status closed => feedback
2020-12-22 18:17 DenisChenu Resolution no change required => reopened
2020-12-22 18:17 DenisChenu Note Added: 61054
2020-12-22 18:37 Waterlijn Note Added: 61056
2020-12-22 18:37 Waterlijn Status feedback => assigned
2020-12-22 21:03 DenisChenu Note Added: 61058
2020-12-22 21:04 DenisChenu Assigned To cdorin => DenisChenu
2020-12-22 21:15 Waterlijn Note Added: 61059
2020-12-23 09:28 DenisChenu Note Added: 61066
2020-12-23 11:52 Waterlijn Note Added: 61078
2020-12-24 10:10 DenisChenu Note Added: 61157
2020-12-26 14:34 DenisChenu Note Added: 61168
2020-12-26 14:36 Waterlijn Note Added: 61169
2021-01-13 07:54 DenisChenu Relationship added duplicate of 14343
2021-01-13 07:55 DenisChenu Assigned To DenisChenu =>
2021-01-13 07:55 DenisChenu Status assigned => new
2021-01-13 07:55 DenisChenu Complete LimeSurvey version number (& build) Versie 4.1.0+200128 => 4.1.0+200128
2021-01-13 07:55 DenisChenu Note Added: 61512