Abdus Sattar Bhuiyan
Abdus Sattar Bhuiyan

Reputation: 3074

File attachment in CakePHP

I have logo.png image in webroot/img/ as follows: enter image description here

I want to import it in my email body as follows:

 $Email->template('welcomeReseller')
                ->emailFormat('html')
                ->from(array($from => $cus_name))
                ->attachments(array(
                    array(
                        'file' => $this->webroot . 'img/logo.png',
                        'mimetype' => 'image',
                        'contentId' => '12345'
                    ),
                    array(
                        'file' => $this->webroot . 'img/logo.png',
                        'mimetype' => 'image',
                        'contentId' => '123456'
                    ),
                    array(
                        'file' => $this->webroot . 'img/logo.png',
                        'mimetype' => 'image',
                        'contentId' => '1234567'
                    ),
                ))
                ->viewVars($email_data)
                ->to($tests)
                ->subject($subject);

        $Email->send($body);

However, I am getting File not found: "/demo-home25_/img/logo.png" error. This simple. file path is not correct. But whats wrong in my code?

Upvotes: 0

Views: 938

Answers (1)

ndm
ndm

Reputation: 60483

Request::$webroot is neither necessarily a path to your app/webroot folder, nor is it a local filesystem path, it's a URL fragment that points to your applications root entry point, which usually is the servers document root, or the folder that contains the app folder, only with URL rewriting disabled it might point to the webroot folder, but still it would only be a URL fragment.

Use the path constants instead, they contain actual filesystem paths, like WWW_ROOT . 'img' . DS (which also works in Cake 3.x), or IMAGES (which is deprecated though).

array(
    'file' => WWW_ROOT . 'img' . DS . 'logo.png',
    'mimetype' => 'image',
    'contentId' => '12345'
)

See also

Upvotes: 3

Related Questions