Parangan
Parangan

Reputation: 148

Cakephp email with attachment falid

I am trying to send email with attachment in Cakephp 2.6 via Mailgun. It normally working fine unless I add 'attachments($attachments)'. My code is below.

$attachments = SITE_URL . '/img/red-hyphen.png';
                            $data = array (
                                    'unique_secret_key' => $unique_secret_key,
                                    'WAYDSiteUrl' => $siteUrl 
                            );
                            $Email = new CakeEmail ( 'mailgun' );

                            $Email->template ( 'forgotpassword', 'default' )
                                    ->emailFormat ( 'html' )
                                    ->viewVars ( $data )
                                    ->subject ( 'Forgot Password' )
                                    ->to ( $chek_exists_record ['Member'] ['email'] )
                                    ->attachments($attachments)
                                    ->send ();

I am getting error

Error: An Internal Error Has Occurred. Stack Trace

APP\Controller\MembersController.php line 255 → CakeEmail->attachments(string)

'http://localhost/GYB_develop/gyb_source/img/red-hyphen.png'

[internal function] → MembersController->forgotpassword()

CORE\Cake\Controller\Controller.php line 490 → ReflectionMethod->invokeArgs(MembersController, array)
CORE\Cake\Routing\Dispatcher.php line 193 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 167 → Dispatcher->_invoke(MembersController, CakeRequest)
APP\webroot\index.php line 118 → Dispatcher->dispatch(CakeRequest, CakeResponse)

Upvotes: 0

Views: 768

Answers (1)

Sharma Vikram
Sharma Vikram

Reputation: 2470

You should try this Cakephp send email with attachment

email.php file

public $gmail = array(
    'host' => 'ssl://smtp.gmail.com',
    'port' => 465,
    'username' => '[email protected]',
    'password' => 'xxxxxxxx',
    'transport' => 'Smtp'
    );

In your Controller At the top import the cakeemail function

App::uses('CakeEmail', 'Network/Email');

After that your function/Action of your controller

          $client_email='[email protected]' 
          $Email = new CakeEmail();
          $Email->template('doperesult');
          $Email->viewVars(array( 'unique_secret_key' => $unique_secret_key,'WAYDSiteUrl' => $siteUrl ));
          $Email->emailFormat('html');
          $Email->to($client_email);    
          $Email->from('[email protected]');
          $Email->attachments(array(
          'red-hyphen.png' => array(
            'file' => WWW_ROOT.'img/red-hyphen.png',
            'mimetype' => 'image/png',
            'contentId' => 'my-unique-id'
                 )
            ));
          $Email->subject('Dummy test');
          if($Email->send('Send Mail')){ 
            $this->Session->setFlash('Your Result is successfully send','default',array('class'=>'alert alert-success'));
            return $this->redirect('/');  
          }          

you can visit the cakephp email link Cakephp email

Upvotes: 0

Related Questions