Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Sending Mail using SMTP in Cakephp 3.0

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 2.51k
    Comment on it

    Hello Reader's ,

    Today in my blog, I am going to explain about the mail function in CakePHP3 and how you can use it for sending a mail. It is simple to implement in your project.

    First, you need to configure email transports and email delivery profiles in your config/app.php. Here i am using gmail ssl for sending a mail.

    Add gmail SSl setting in Email Transport array in app.php file

     'EmailTransport' => [
            'default' => [
                'className' => 'Mail',
                // The following keys are used in SMTP transports
                'host' => 'localhost',
                'port' => 25,
                'timeout' => 30,
                'username' => 'user',
                'password' => 'secret',
                'client' => null,
                'tls' => null,
                 'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
            ],
          'gmail'=> [
              'host' => 'ssl://smtp.gmail.com',
              'port' => 465,
              'username' => 'YourEmailID@gmail.com',  //your gmail address
              'password' => 'YourMailPassword',        //your gmail password
              'className' => 'Smtp',
              'log' => true,
              'context' => [
                'ssl' => [
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                ]
              ]
            ],
        ],
    

    After completing this then add below Function in the controller.

    public function send_mail()
    	{		
    		
    		$msg='Hellow User';
    		$subject='CakePHP3 Mail';
    		$to=yourEmailId@domain.com;
    		$true=$this->sendUserEmail($to,$subject,$msg);
    		 if($true=='1')
    		 {
    			$this->Flash->success(__('Mail Send'));
    			return $this->redirect(['action' => 'index']);
    		 }
    		 else
    		 {
    			$this->Flash->success(__('Error'));
    			return $this->redirect(['action' => 'index']);
    		 }
        
    	}

    In the above function we are passing param to another function which placed in config/appController.php.

    Add Mail Library in your AppController.php file.

    use Cake\Network\Email\Email;

    Now add sendUserEmail () Function in appController.php

     

    public function sendUserEmail($to,$subject,$msg)
        {
          $email = new Email();    
           $email
                ->transport('gmail')
                ->from(['yourEmailID@domain.com' => 'yourEmailID@domain.com'])
                ->to($to)
                ->subject($subject)
                 ->emailFormat('html')
                ->viewVars(array('msg' => $msg))
                ->send($msg);         
                return 1;
        }

     

 1 Comment(s)

  • Interesting! Thank you for sharing this informative content. Check out https://betapage.co/ it is a startup directory where you can discover, hunt and upvote on various innovative startups as per your choice.

    Thank you.

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: