'Connection refused when trying to send mail using postfix

I want to use the mail() function in PHP. In order to do that, I've installed postfix. I am running on Ubuntu 18.04.

My problem is that I can't send mail as I have a "connection refused" output.

In the terminal, I try :


    echo "This is the body of the email" | mail -s "This is the subject line" [email protected]

And I get this output in mail.log


    Jan 11 01:14:39 AiliganonPC postfix/pickup[9411]: 207FE16016CD: uid=0 from=<root@AiliganonPC>
    Jan 11 01:14:39 AiliganonPC postfix/cleanup[12552]: 207FE16016CD: message-id=<[email protected]>
    Jan 11 01:14:39 AiliganonPC postfix/qmgr[9412]: 207FE16016CD: from=<root@AiliganonPC>, size=370, nrcpt=1 (queue active)
    Jan 11 01:14:39 AiliganonPC postfix/smtp[12554]: connect to gmail-smtp-in.l.google.com[2a00:1450:4010:c0e::1a]:25: Network is unreachable
    Jan 11 01:14:39 AiliganonPC postfix/smtp[12554]: connect to gmail-smtp-in.l.google.com[74.125.131.27]:25: Connection refused
    Jan 11 01:14:39 AiliganonPC postfix/smtp[12554]: connect to alt1.gmail-smtp-in.l.google.com[2404:6800:4008:c13::1a]:25: Network is unreachable
    Jan 11 01:14:39 AiliganonPC postfix/smtp[12554]: connect to alt1.gmail-smtp-in.l.google.com[142.250.157.27]:25: Connection refused
    Jan 11 01:14:39 AiliganonPC postfix/smtp[12554]: connect to alt2.gmail-smtp-in.l.google.com[2607:f8b0:400e:c06::1b]:25: Network is unreachable
    Jan 11 01:14:39 AiliganonPC postfix/smtp[12554]: 207FE16016CD: to=<[email protected]>, relay=none, delay=0.28, delays=0.02/0.01/0.24/0, dsn=4.4.1, status=deferred (connect to alt2.gmail-smtp-in.l.google.com[2607:f8b0:400e:c06::1b]:25: Network is unreachable)

Also, the output of the following command:


    telnet alt2.gmail-smtp-in.l.google.com 25

is


    Trying 108.177.98.26...
    Trying 2607:f8b0:400e:c06::1b...
    telnet: Unable to connect to remote host: Network is unreachable

What I've tried :

Verify that my port 25 was opened and not blocked by firewall :


    sudo lsof -i:25


    COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    master  12899 root   13u  IPv4 4027269      0t0  TCP *:smtp (LISTEN)
    master  12899 root   14u  IPv6 4027270      0t0  TCP *:smtp (LISTEN)

Disable my firewall

I've also tried with a different mail address (not on gmail) and get same output.

In /etc/postfix/main.cf, I've changed inet_protocols from all to ipv4 and then I've pt it back to "all".

Here's the configuration of my file :


    smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
    myhostname = example.com
    alias_maps = hash:/etc/aliases
    alias_database = hash:/etc/aliases
    mydestination = $myhostname, ailiganonpc, AiliganonPC, localhost.localdomain, localhost
    relayhost =
    mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
    mailbox_size_limit = 0
    recipient_delimiter = +
    inet_interfaces = all
    inet_protocols = all



Solution 1:[1]

Not sure why you're getting the error but here's a couple of troubleshooting tips that might be helpful to solve this.

Your DNS seems to be working but something in your network is causing the problem, can you ping the Google server and is your ISP maybe blocking port 25 for any reason?

What's the output of netstat -tuplen ?

Your postfix seems to be running, have you tried telneting to yourself?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wingman