How to force postfix to use smtp relay

When I execute, the following line postfix uses my sendgrid relay server

echo "Subject: sendmail test" | sendmail -v [email protected]

But when I execute the sendmail function via the PHP script it won't send it to the relay server. Look like it want to distribute it locally

Any thoughts on how to force postfix to use the smtp relay option

  • Update

It looks like postfix is timing out on STARTTLS, I have added the log below. Could this be due to my LetsEncrypt certificate?

postfix/smtpd[29268]: > localhost[::1]: 250-www.example.com
postfix/smtpd[29268]: > localhost[::1]: 250-PIPELINING
postfix/smtpd[29268]: > localhost[::1]: 250-SIZE 10240000
postfix/smtpd[29268]: > localhost[::1]: 250-VRFY
postfix/smtpd[29268]: > localhost[::1]: 250-ETRN
postfix/smtpd[29268]: > localhost[::1]: 250-STARTTLS
postfix/cleanup[29279]: 8E2B520B98: message-id=<[email protected]>
postfix/smtpd[29268]: smtp_get: EOF

postconf -n

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
header_size_limit = 4096000
inet_interfaces = all
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
mydestination = $myhostname, localhost.$mydomain, localhost
mydomain = example.com
myhostname = www.example.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
readme_directory = no
recipient_delimiter = +
relayhost = smtp.sendgrid.net:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = static:apikey:BLBAABLAABLAA
smtp_sasl_security_options = noanonymous
smtp_sasl_tls_security_options = noanonymous
smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt
smtp_tls_security_level = encrypt
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtpd_banner = $myhostname E
SMTP $mail_name (Debian/GNU)
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes

It seems to work perfectly when I write a python script but with the PHP swiftmailer it seems to fail and return an EOF

=============== UPDATE

The relay seems to work but now we are getting this error

Swift_IoException [ 0 ]: Connection to localhost:25 Timed Out

I am able to sendmails using sendmail, but when SwiftMailer is using sendmail its getting the earlier stated error


I managed to fix the issue. It turned out that the mail that the client send out was embedded with an unauthorised image. I removed the embedded image and everything works fine now!

I do find it odd that nothing would show in the verbose log of postfix