I can only send email at localhost


#1

Hello I am using Cakephp 3.7.3.

The problem is that in localhost I can send emails without any problem but when I try in my VPS (with hostinger) it is imposible to send an email. I always get the error:
" Connection timed out Cake\Network\Exception\SocketException"

My server has ubuntu 18.04.1 with php 7.3.2.

I have already allow the email port with ufw
image

I have seen the topic “Can send an email on Cakephp3” and anything works for me. The code that I have in my app.php is

'gmail'=> [
        'host' => 'smtp.gmail.com',
        'port' => 587,
        'username' => 'no-reply@gruposcout50.es',  
        'password' => 'mypass',        
        'className' => 'Smtp',
        'log' => true,
        'tls'=>true
    ],

I tried with other configurations but I get always the same problem. I also tried with an account that is not GSuite (a normal gmail.com)

I hope anyone can help me.

Thanks in advance


#2

Open port 587 in firewall or use localhost with postfix as an smtp service


#3

I have already create the firewall rule. But it is still not working
image


#4

I just configured postfix and I am able to send an email with the command echo “Test mail from postfix” | mail -s “Test Postfix” myemail@gmail.com at the terminal.

The problem now is that I do not know how to configure cakephp to send an email through postfix


#6

I answer myself. With the default parameters it works perfectly
‘default’ => [
‘className’ => ‘Cake\Mailer\Transport\MailTransport’,
/*
* The following keys are used in SMTP transports:
*/
‘host’ => ‘localhost’,
‘port’ => 25,
‘timeout’ => 30,
‘username’ => null,
‘password’ => null,
‘client’ => null,
‘tls’ => null,
‘url’ => env(‘EMAIL_TRANSPORT_DEFAULT_URL’, null),
],
If anyone have doubts about how to configure postfix, I follow the next tutorial:


#7

via localhost? Postfix is installed on localhost as a proxy.


#8

Try connecting to the gmail server over telnet:

telnet smtp.gmail.com 587

It should reply with something like:

220 smtp.gmail.com ESMTP p5sm4719403wmh.16 - gsmtp

If that’s the case, your ports aren’t the issue.


#9

I have the following in app which is working for me without problems.

'EmailTransport' => [
        'default' => [
            'className' => 'Smtp',
            /*
             * The following keys are used in SMTP transports:
             */
            'host' => 'smtp.gmail.com',
            'port' => 587,
            'timeout' => 30,
            'username' => 'youremail@domain.com',
            'password' => 'XXXXXXXXXX',
            'tls' => true
        ],
    ],

    /**
     * Email delivery profiles
     *
     * Delivery profiles allow you to predefine various properties about email
     * messages from your application and give the settings a name. This saves
     * duplication across your application and makes maintenance and development
     * easier. Each profile accepts a number of keys. See `Cake\Mailer\Email`
     * for more information.
     */
    'Email' => [
        'default' => [
            'transport' => 'default',
            'from' => 'youremail@domain.com',
            //'charset' => 'utf-8',
            //'headerCharset' => 'utf-8',
        ],
    ],

Your email needs to be the one that is registered in gsuite. Also you may want to check your gmail settings that the smtp from authorised IP is allowed to send emails.