Simple PHP contact form not working

Hi guys,

I have a PHP contact form on my site that I was using for a long time, but since I switched to nginx it's not working. I added the and it displays the info (so PHP is working) but the mail doesn't arrive in my inbox. I also disabled any form of caching; still no luck!

Test it here:

http://test.uniteaccommodation.co.uk/

****Anyone has any ideas?

Thanks for your time!****

I am attaching the PHP code and nginx code below

PHP contact form

============

NGINX.CONF

============

user  nginx;
worker_processes 4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
#Automatic subdomains
server {
    listen 80;
    server_name ~^(.*)\.uniteaccommodation\.co.uk$;
    #if directory doesn't exist
    if (!-d /srv/www/sicanstudios.com/$1) {
        rewrite . http://uniteaccommodation.co.uk/ redirect;
    }

    # Sets the correct root
    root /srv/www/sicanstudios.com/$1;
}

#Block access to SSH but my IP
#Remove the www from websites
#Only allow certain methods
#Block bad referer
#Deny access to .hidden files
#Cache certain files
server {
    location 178.79.173.155:22 {
        allow XXX.XXX.XXX.XXX;
        deny all;
    }

    listen 80 default;

    if ($host ~* www\.(.*)) {
        set $host_without_www $1;
        rewrite ^(.*)$ $scheme://$host_without_www$1 permanent; #1
        #rewrite ^ $scheme://$host_without_www$1request_uri permanent; #2
    }

    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
        return 444;
    }

    if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudity|organic|poker|porn|sex|teen) )
    {
        # return 404;
        return 403;
    }

    location ~ /\. {
        deny  all;
        access_log off;
        log_not_found off;
    }

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml|ttf|eot)$ {
        access_log off;
        expires 30d;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
}

    client_max_body_size 20M;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile on;
    tcp_nopush off;
    tcp_nodelay on;

    #Improved security (allow only 5 connections, timeout faster, limit buffer size)
    limit_zone slimits $binary_remote_addr 5m;
    limit_conn slimits 5;
    client_body_timeout 20;
    client_header_timeout 20;
    keepalive_timeout 60;
    send_timeout 20;
    client_body_buffer_size 3K;
    client_header_buffer_size 3k;
    large_client_header_buffers 2 1k;

    gzip on;
    gzip_buffers 32 8k;
    gzip_vary on;
    gzip_proxied any;
    gzip_disable "MSIE [1-6].(?!.*SV1)";
    gzip_comp_level 2;
    gzip_types text/plain text/css application/json application/rtf  application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    include /etc/nginx/blockips.conf;
}

HOST.CONF

==========

server {
    listen 80;
    server_name test.uniteaccommodation.co.uk;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    root /srv/www/uniteaccommodation.co.uk/test;

    location / {
        index index.html index.htm index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;

    }
}

11 Replies

My first though is if you possibly removed any php modules when you switched to nginx. Some distributions don't include the php mail module directly in the php package, which means you need to install it from the package manager separately.

This probably won't fix it (it's more nginx-specific), but in your virtual host where your php location is define, you should only need the "include /etc/nginx/fastcgiparams" line. As long as the other php settings below the include are already in your fastcgiparams file that's being included, having those there is a bit redundant. If you need special fastcgi settings that aren't in fastcgiparams, it's better to remove the include and define them directly; for most php sites, though, the defaults in fastcgiparams should work, so including that and removing the stuff below should work.

Does anything show up in your mail server's log file? (postfix, exim, etc…)

@Piki:

As long as the other php settings below the include are already in your fastcgiparams file that's being included, having those there is a bit redundant. The code I have there is not found in the fastcgiparams file.

I think the mail function is installed. Here's what I found in my php.ini:

[mail function]
; For Win32 only.
; http://www.php.net/manual/en/mail.configuration.php#ini.smtp
SMTP = localhost
; http://www.php.net/manual/en/mail.configuration.php#ini.smtp-port
smtp_port = 25

; For Win32 only.
; http://www.php.net/manual/en/mail.configuration.php#ini.sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://www.php.net/manual/en/mail.configuration.php#ini.sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =

I also added to the script

$mail_sent = mail($to,$email_subject,$email_body,$headers);
echo $mail_sent ? "Mail sent" : "Mail failed";

to see if it sends the mail or not, and it says "Mail sent"…

These are the php modules I have installed:
> php-5.3.8-5.el6.remi.i686

php-mysql-5.3.8-5.el6.remi.i686

php-pear-1.9.0-2.el6.noarch

php-pdo-5.3.8-5.el6.remi.i686

php-fpm-5.3.8-5.el6.remi.i686

php-pecl-apc-3.1.3p1-1.2.el6.1.i686

php-common-5.3.8-5.el6.remi.i686

php-cli-5.3.8-5.el6.remi.i686

php-devel-5.3.8-5.el6.remi.i686

Also in the maillog file something useful (hoopycat):
> Nov 5 17:32:39 li310-155 postfix/postdrop[14513]: warning: unable to look up public/pickup: No such file or directory

No idea what that means! :p

@sican:

@Piki:

As long as the other php settings below the include are already in your fastcgiparams file that's being included, having those there is a bit redundant. The code I have there is not found in the fastcgiparams file.

It's good to keep a set of defaults in your fastcgi_params and only specify them in your virtual host file if you need special settings. Here's a set of defaults that should work (make a backup copy of yours and compare it to this, adjust as needed, and restore from the backup if needed):

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

fastcgi_pass    127.0.0.1:9000;
fastcgi_index    index.php;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200;

@sican:

I think the mail function is installed. Here's what I found in my php.ini:

[mail function]
; For Win32 only.
; http://www.php.net/manual/en/mail.configuration.php#ini.smtp
SMTP = localhost
; http://www.php.net/manual/en/mail.configuration.php#ini.smtp-port
smtp_port = 25

; For Win32 only.
; http://www.php.net/manual/en/mail.configuration.php#ini.sendmail-from
;sendmail_from = me@example.com

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://www.php.net/manual/en/mail.configuration.php#ini.sendmail-path
sendmail_path = /usr/sbin/sendmail -t -i

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =

; Add X-PHP-Originaiting-Script: that will include uid of the script followed by the filename
mail.add_x_header = On

; Log all mail() calls including the full path of the script, line #, to address and headers
;mail.log =

Distros generally include a default php.ini that will give a set of distro-specific defaults for most of the modules offered by the distro, including the modules that aren't installed. Those settings are only used by those modules, so it doesn't matter that they're there unless you install them.

@sican:

These are the php modules I have installed:
> php-5.3.8-5.el6.remi.i686

php-mysql-5.3.8-5.el6.remi.i686

php-pear-1.9.0-2.el6.noarch

php-pdo-5.3.8-5.el6.remi.i686

php-fpm-5.3.8-5.el6.remi.i686

php-pecl-apc-3.1.3p1-1.2.el6.1.i686

php-common-5.3.8-5.el6.remi.i686

php-cli-5.3.8-5.el6.remi.i686

php-devel-5.3.8-5.el6.remi.i686

Judging by the fact that the package names include "el6", I'm guessing you're using CentOS? If so, I don't know what they call the package. You'll have to search yum for it. It might be something like "php-mail" or "php-pear-mail". You can do something like "yum search php | grep mail" (I don't remember if it's actually "yum search …", I haven't used yum in a long time). Doing the "| grep mail" will only show the php packages that include mail in the name (and maybe the description as well, if yum displays the description too).

Thanks Piki!

Will update the fastcgi parameters

Yes, I'm using CentOS 6. I did a yum search:
> php-PHPMailer.noarch : PHP email transport class with a lot of features

php-email-address-validation.noarch : A PHP class for validating email addresses

: emails

php-pecl-mailparse.i686 : PHP PECL package for parsing and working with email

squirrelmail.noarch : webmail client written in php

imp.noarch : The Internet Messaging Program: webmail access to IMAP/POP3

ingo.noarch : The Horde web-based Email Filter Rules Manager

libc-client.i686 : C-client mail access routines for IMAP and POP protocols

php-channel-swift.noarch : Adds swift mailer project channel to PEAR

: conforming to the mail standard

roundcubemail.noarch : Round Cube Webmail is a browser-based multilingual IMAP

I'll install the first one (PHPMailer)and see if it works! If not, I'll also try the pecl-mailparse

**Update: I tried installing them, but I'm getting an error (I have newer version installed):

Error: Package: php-mbstring-5.3.2-6.el6_0.1.i686 (updates)
           Requires: php-common = 5.3.2-6.el6_0.1
           Installed: php-common-5.3.8-5.el6.remi.i686 (@remi)
               php-common = 5.3.8-5.el6.remi
           Available: php-common-5.3.2-6.el6.i686 (base)
               php-common = 5.3.2-6.el6
           Available: php-common-5.3.2-6.el6_0.1.i686 (updates)
               php-common = 5.3.2-6.el6_0.1

Also, those packages (PHPMailer and mailparse) aren't listed on the http://pkgs.org/centos-6-rhel-6/remi-i386/

Any thoughts?

What's the remi thing? A 3rd party repository? If so, try using the version from the official repos. Another repos (one I used awhile back without any issue with CentOS 5.5) is called webtatic, though I don't have the URL available.

The mail() function is part of the PHP core. You do not need to install any extensions to be able to use it, unless the maintainer of your repository deliberately removed it from the core (very unlikely). Check that the sendmail path in php.ini is correct. If you installed PHP from a third-party repository, the path may be different from what your distro actually has. Also check whether there are any security restrictions on calling sendmail. Check what your MTA thinks its hostname is. If it's wrong, no mail will get through to any reputable e-mail service provider.

PHPMailer is just a third-party library that allows you to send e-mails with attachments and so on. It does not affect the behavior of mail(), and in fact relies on it. If mail() does not work, neither will PHPMailer – unless you use PHPMailer's SMTP function to connect directly to an external mail server, or manually configure it to use a different path to sendmail.

I also doubt that this your problem anything to do with nginx. If you just switched from Apache to nginx, you probably also switched from mod_php to php-fpm. In that case, whatever configuration that comes with php-fpm would be to blame. Again, this boils down to tweaking php.ini settings and restarting php-fpm until it works. (Restarting nginx will have no effects.)

@hybinet:

The mail() function is part of the PHP core.

Hmm, good to know. I always thought it was separate :-)

@Piki:

@hybinet:

The mail() function is part of the PHP core.
Hmm, good to know. I always thought it was separate :-)
It's a common misconception. I've seen it a few times in this forum. The misconception is probably fueled by the fact that mail() depends on an external program, sendmail.

@hybinet:

The misconception is probably fueled by the fact that mail() depends on an external program, sendmail.
Which doesn't have to be THE (horrible) Sendmail MTA, as every single MTA, being it Postfix, Exim, or whatever, will provide a "sendmail" command for compatibility.

That's to continue on the "correcting misconceptions" thread.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct