0

I'm trying to send email via cakephp3.

This is my app email config:

'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,
    ],
],

And this is controller :

public function index() {
    $this->autoRender = false;
    $email = new Email('default');

    if (
    $email->from(['mymail@gmail.com' => 'My Site'])
        ->to('mymail@gmail.com')
        ->subject('About')
        ->send('My message')) {
        print 'ok';
    }
}

And now, if i run this function, result is printed 'ok' on monitor.

But no mail is on my testing email box, even span, nothing.

My question is, why cakephp tells me that sending was done, but no mail is present on my box ?

Thank You.

Marek Brzeziński
  • 325
  • 1
  • 3
  • 13

1 Answers1

0

Your configuration is incorrect. cakePHP attempts to send the e-mail via smtp to your localhost.

Most likely you do not have an MTA (ie. exim, dovecot) installed locally and the request gets dropped. This should be visible as an error in you logs (if enabled).

An easy solution is to change the configuration to a working email service for testing, for example Gmail.

Example:

Email::configTransport('gmail', [
  'host' => 'smtp.gmail.com',
  'port' => 587,
  'username' => 'my@gmail.com',
  'password' => 'secret',
  'className' => 'Smtp',
  'tls' => true
]);

Note that the host and port point to Gmail. This example is indicative and was taken from the official documentation.

More information on configuring email transports in cakePHP 3.0 here: http://book.cakephp.org/3.0/en/core-libraries/email.html#configuring-transports

Mark
  • 5,437
  • 2
  • 19
  • 29
  • I try it but now i get error: SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS. – Marek Brzeziński Sep 15 '15 at 11:20
  • i have changed port to 465, and now this error arises: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed stream_socket_client(): Failed to enable crypto stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) – Marek Brzeziński Sep 15 '15 at 11:31
  • The TLS configuration should work. The problem is probably caused by a firewall in the middle which blocks the port. Try running `telnet smtp.gmail.com 587` in your terminal (note: you should have telnet installed/enabled). If you can't get in with telnet, check your firewall. – Mark Sep 15 '15 at 12:04
  • Also make sure that the `username` and `password` are from a valid gmail account. The username should be an email address. – Mark Sep 15 '15 at 12:14
  • When i run telnet smtp.gmail.com 587 i get: 220 smtp.gmail.com ESMTP b11sm1236178362189laf.16 - gsmtp. Is it ok ?And if i use port 465 i see blank terminal. – Marek Brzeziński Sep 15 '15 at 12:26
  • And when i change host to 'smtp.gmail.com' i get this: SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS. – Marek Brzeziński Sep 15 '15 at 12:36
  • It seems gmail is a bit _picky_, try mailjet.com. See an example here: http://stackoverflow.com/a/26409292/2853903 – Mark Sep 15 '15 at 12:39
  • i have uploaded project to my hosting provider. On server there is an error:SMTP server did not accept the password. – Marek Brzeziński Sep 15 '15 at 12:55
  • 1
    There's a pretty good chance that this means that you have used either the wrong user name or wrong password. It's also possible that you have TLS off in your config but the server requires it. – Greg Schmidt Sep 16 '15 at 23:50