Hadeszeus
By:
Hadeszeus

Can't send email in PHP using Postfix

October 25, 2015 7.9k views
PHP Email Nginx

I already installed Postfix and it's working sending from terminal. But Send mail via PHP is not coz I do know what setting or how to create or get the usersname / password and other details needed

//EMAIL SENDING CONFIGURATION

define('MAILER', "smtp");

define('SMTP_HOST', "");

define('SMTP_PORT', 587);

define('SMTP_USERNAME', "");

define('SMTP_PASSWORD', "");

define('SMTP_ENCRYPTION', "");

2 Answers

Wordpress uses PHP mail() by default. If you disable/uninstall any extensions that try to manage email sending and delete the configuration lines above from wp-config.php, it should work fine.

You can check if PHP's mail() function is working properly by creating a file with the following content:

<?php
    ini_set('display_errors', 1);
    error_reporting(-1);
    mail ('you@example.com', 'Postfix Test', 'A test email') || print_r(error_get_last());
?>

Place it in your document root (e.g. /var/www/html) and browse to it. If it's blank, everything went well and you should receive an email shortly.

  • Hi kamaln7, thank you for your response. I tried your script and yes the email was sent to my test account after 2 to 3 minutes. But using Easy SMTP plugin for wordpress and setup gmail smtp account I got this after test mail.

    SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

    I'm using SSL 465 on my setting.

    I also tried nc -vz smtp.gmail.com 25 and the result is
    "Connection to smtp.gmail.com 25 port [tcp/smtp] succeded!

PHP uses an MTA to send it's mails trough mail(),
if you want to override it on system level you could edit /etc/postfix/main.cf and add

relayhost = [smtp.domain.tld]:587

if you are on an older system, you might need to configure sendmail instead

Have another answer? Share your knowledge.