Search code examples
cakephpemail-attachments

Cakephp not sending attachment, only send plain html


I am trying to send an attachment with cakephp email, but only plain html sent and not a attachment.

The following code I am using.

$nmessage ="Hello Test\r\n\r\n";
$email = new CakeEmail();
$email->from(array('abc@example.com' => 'Test'));       
$email->filePaths  = array('/screenshots/');
$email->attachments =array('Google-Maps-9.22.2.jpg');
$email->to('user4@gmail.com');
$email->subject('Register a visit ');
$email->emailFormat('html');
$email->send($nmessage); // or use a template etc   

Solution

  • To send attachments you can do it the following ways, first a string with the full path (notice there is no equals symbol, it's a function of the CakeEmail class).

    $email->attachments('/full/file/path/file.jpg');
    

    Secondly is the same but wrapped in an array

    $email->attachments(array('/full/file/path/file.png'));
    

    Thirdly an array with keys to rename the file

    $Email->attachments(array('photo.png' => '/full/some_hash.png'))
    

    And finally you can use nested arrays

    $email->attachments(array(
        'photo.png' => array(
            'file' => '/full/some_hash.png',
            'mimetype' => 'image/png',
            'contentId' => 'my-unique-id'
        )
    ));
    

    So in summary, don't use $email->attachments = and make sure to provide the full path.

    http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments