Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-42007

Embed images as attachments in Moodle forum post emails

XMLWordPrintable

    • Icon: Improvement Improvement
    • Resolution: Duplicate
    • Icon: Minor Minor
    • None
    • 2.4.3, 2.4.4, 2.4.5, 2.4.6
    • Forum
    • MOODLE_24_STABLE

      If a image (jpeg,gif,png) is inserted into the RTE in a forum post, the image is not visible in the generated mail.

      Solution:

      /lib/moodlelib:
      function email_to_user:

      +add (row ~5389):

      <code>
      if($messagehtml) { //HTML Message
      $messagehtml=rawurldecode($messagehtml);
      //CHG sbrandstaetter: Push image directly in mails
      $pattern = "/src=\"([^(\"|\')]

      {1,}

      (png|jpg|jpeg|pjpeg|gif))\"/i";
      preg_match_all($pattern, $messagehtml, $images);

      foreach($images[1] as $image){

      $dataString = explode('pluginfile.php/',$image);
      $data=explode('/',$dataString[1]);

      $fs = get_file_storage();

      // Prepare file record object
      $fileinfo = array(
      'component' => $data[1], // usually = table name
      'filearea' => $data[2], // usually = table name
      'itemid' => $data[3], // usually = ID of row in table
      'contextid' => $data[0], // ID of context
      'filepath' => '/', // any path beginning and ending in /
      'filename' => $data[4]); // any filename

      // Get file
      $file = $fs->get_file($fileinfo['contextid'], $fileinfo['component'], $fileinfo['filearea'],
      $fileinfo['itemid'], $fileinfo['filepath'], $fileinfo['filename']);

      $im=false;
      // Read contents
      if ($file) {
      $im = $file->get_content();
      $imInfo = $file->get_imageinfo();
      if($im)

      { $mail->AddStringEmbeddedImage($im,str_replace(' ','',rawurldecode($data[4])),str_replace(' ','',rawurldecode($data[4])),'base64', $imInfo['mimetype']); $newImg ="cid:".str_replace(' ','',rawurldecode($data[4])); $messagehtml=str_replace($image,$newImg,$messagehtml); }

      }
      }
      }

      </code>

      I know it is not the "beatiful way", but it works fine for me.

      kind regards
      Simon Brandstätter

        1. moodlelib.php
          401 kB
          Simon Brandstätter

            Unassigned Unassigned
            sbrandstaetter Simon Brandstätter
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                Error rendering 'clockify-timesheets-time-tracking-reports:timer-sidebar'. Please contact your Jira administrators.