0

I do not have any knowledge about cakephp mail so explain the solution briefly I mean what to do and how to do from the beginning.

From the cakephp official side I just used this "use Cake\Mailer\Email;" and then the mail function but a error message shows like as shown below

Could not send email: mail(): Failed to connect to mailserver at quot;server.com" port 25, verify your "SMTP" and quot;smtp_port" setting in php.ini or use ini_set()

MY users controller login function

public function login() {
        $this->viewBuilder()->setLayout('');

        if ($this->request->is('post')) {
            $data = $this->request->getData();

            $query = $this->Users->find()->where(['email' => $data['email'], 'password' => md5($data['password'])]);
            if ($query->count()) {
                $user = $query->first()->toArray();
                $this->Auth->setUser($user);  
//FOR MAIL START                  
                ini_set('SMTP', "server.com");
                ini_set('smtp_port', "25");
                ini_set('sendmail_from', "restrange5@gmail.com");

                $email = new Email('default');
                $email->setFrom(['restrange5@gmail.com' => 'My Site'])
                        ->setTo('ramakantasahoo835@gmail.com')
                        ->setSubject('About')
                        ->send('My message');
        //FOR MAIL END

                $this->Flash->success(_('Login Successfull'));
                $this->redirect(['action' => 'dashboard']);
            } else {
                $this->Flash->error(__('Username/Password not found!!'));
                return $this->redirect($this->referer());
            }
        }
    }

How much I know as I have just changed in users controller only. What else I have do please suggest.

Suraj Kumar
  • 5,547
  • 8
  • 20
  • 42
  • 2
    Is this really exactly the code your are trying to execute? If so, I am not surprised that `server.com` does not work for you. You have to user the information from your email provider. I'd guess it would be `smtp.gmail.com` with port `465` or `587`. See https://stackoverflow.com/a/2748837/4944034 for an example. – Carsten Franke Jan 18 '19 at 11:15
  • Thank you sir but i want help in cakephp – gladiatorOO Jan 18 '19 at 14:31
  • The line `ini_set('SMTP', "server.com");` should contain your SMTP server (as I said, most likely `smtp.gmail.com`) and the correct port one line below. This has nothing to do with CakePHP, that's simply a configuration of the outgoing mail server. – Carsten Franke Jan 18 '19 at 14:57
  • Those `ini_set` calls shouldn't be required at all, with CakePHP. Rather, configure your email transport correctly (that is, with the real server name, login and password) in `config/app.php`. – Greg Schmidt Jan 18 '19 at 16:30

1 Answers1

0
**Your cofiguration in app.php file is something like this **'
EmailTransport' => [
    'default' => [
        'className' => 'Smtp',
        // The following keys are used in SMTP transports
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'timeout' => 30,
        'username' => 'email here',
        'password' => 'password here',
        'client' => null,
        'tls' => null,
        'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
    ],
]
jags
  • 39
  • 4