0

I am sending HTML emails containing images, the src attribute for those images is an URL to a yii2 controller action that outputs the image content.

Images are shown if I open my email on the browser, my problem is with outlook that can not download and show my images.

Here is my action to output the image:

public function actionImage($img_name) {
        $filepath = Yii::getAlias('@webroot')."/media/files/$img_name";
        if (file_exists($filepath))
        {
            $mime = \yii\helpers\BaseFileHelper::getMimeType($filepath);

            header('Content-Type: '.$mime);
            header('Content-Disposition: attachment; filename="' . $img_name. '"');
            header('Content-Length: ' . filesize($filepath));

            // Render the file
            readfile($filepath);

            exit(0);
        }
        else
        {
           die('Media Not Found!');
        }
    }
anwar
  • 103
  • 1
  • 10
  • first i would check my server access files and see if outlook is even bothering to make the request; if it is the issue is with what is being returned, so check the Apache headers; if its not the issue is with the url. –  May 29 '18 at 23:17
  • I checked, action is getting called when I try to download pictures on Outlook, Outlook is making a request per image. – anwar May 30 '18 at 01:30
  • then make sure request returns the 'right' headers. its likely to be fussier than a browser –  May 30 '18 at 01:33
  • You may want to try out [this](https://stackoverflow.com/a/27961248/57091) – robsch May 30 '18 at 05:33
  • unfortunately [this](https://stackoverflow.com/a/27961248/57091) did not work. – anwar May 30 '18 at 11:17

1 Answers1

0

The function sendFile from yii2 response helped to solve the problem.

This worked and Outlook downloaded the images.

public function actionImage($img_name) {
        $filepath = Yii::getAlias('@webroot')."/media/files/$img_name";
        if (file_exists($filepath))
        {
            $mime = \yii\helpers\BaseFileHelper::getMimeType($filepath);

            \Yii::$app->response->sendFile($filepath);
            \Yii::$app->response->send();

            exit(0);
        }
        else
        {
           die('Media Not Found!');
        }
    }
anwar
  • 103
  • 1
  • 10