PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Could not send email: unknown Cake\Network\Exception\SocketException in cakephp3

 March 09, 2022     cakephp-3.0     No comments   

Issue

Okay I have been to almost all threads but I couldn't find the answer what I want. Controller: RegistersController

namespace App\Controller;

use App\Controller\AppController;
use Cake\Auth\DefaultPasswordHasher;
use Cake\Event\Event;
use Cake\Mailer\Email;
use Cake\Routing\Router;

class RegistersController extends AppController
{
  public function add()
    {
        $this->loadModel('Tbusers');
        $tbusers = $this->Tbusers->newEntity();
        if ($this->request->is('post')) 
        {
            $tbusers = $this->Tbusers->patchEntity($tbusers, $this->request->data);
            if ($this->Tbusers->save($tbusers)) {
                $this->Flash->success(__('Yayie! You have been registered.'));
                $baseurl = Router::url('/', true);
                $email = base64_encode($this->request->data['email']);
                $first_name = $this->request->data['fname'];
                $last_name = $this->request->data['lname'];
                $username = $first_name.' '.$last_name;
                //$confirmation_link = $baseurl.'users/confirm/'.$email;
                $email = new Email();
                $email->template('email')
                        ->subject('Student Portal - Signup')
                        ->emailFormat('html')
                        ->to($this->request->data['email'])
                        //->viewVars(['confirmation_link' => $confirmation_link,'username'=>$username])
                        ->viewVars(['username'=>$username])
                        ->from('dotnetdev555@gmail.com')
                        ->send();
                if ($email->send()) 
                {
                    // Success
                    echo "mail sent";
                } 
                else 
                {
                    echo "Mail not sent";
                    // Failure
                }
                return $this->redirect(['action' => 'add']);
            }
}

Inside Template->Email->html I have file called email.ctp

Hello <?php echo $username; ?>,<br/>


Thanks,<br/>
Jaymin Sejpal Founder at Student Portal

app.php

 'EmailTransport' => [
        'default' => [
            'className' => 'Mail',
            // The following keys are used in SMTP transports
            'host'      =>  'smtp.elasticemail.com',
            'port'      =>  2525,
            'timeout'   =>  30,
            'username'  => 'dotnetdev555@gmail.com',
            'password'  => 'secret',
            'client'    => null,
            'tls'       => null,
            'url'       => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
        ],

    ],

I don't know what I am missing to it. I am refferring this http://book.cakephp.org/3.0/en/core-libraries/email.html for now.


Solution

The email host that you are using might be using smtp protocol , so in your app.php . Try editing you className parameter to 'Smtp'

'EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        // The following keys are used in SMTP transports
        'host'      =>  'smtp.elasticemail.com',
        'port'      =>  2525,
        'timeout'   =>  30,
        'username'  => 'dotnetdev555@gmail.com',
        'password'  => 'secret',
        'client'    => null,
        'tls'       => null,
        'url'       => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
   ],

],


Answered By - Ankush Tanwar
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing