Php – Postfix won’t send PHP mail() function from form

emailPHPphp-fpmpostfix

I got postfix to send me emails from my server via console using echo "body example" | mail -s "subject example" my@email, but when I try to send an email from my website via mail(), the php function, I don't get the email. I'm new to web servers and mail servers, and from what I understand I need postfix mts to be running in order for me to use the mail() function.

I have no idea why my form submissions aren't going through, and nothing appears in the /var/log/mail.log file to help me figure out what's going wrong. I've checked my contact form and the php itself for any errors (which I got from a bootstrap template but have since then tampered with), and cannot find any problems there.

I could use some help finding next steps I can take to figure out why the emails aren't sending, and if there are any logs elsewhere that my indicate potential errors.

For good measure, I've included my php functions and form:

<?php
    function send(){
        if(empty($_POST['name'])        ||
           empty($_POST['email'])       ||
           empty($_POST['phone'])       ||
           empty($_POST['message']) ||
           empty($_POST['company'])   ||
           !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
           {
            echo "No arguments Provided!";
            return false;
           }

        $name = $_POST['name'];
        $email_address = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $company = $_POST['company'];

        // Create the email and send the message
        $to = 'my@email';
        $email_subject = "Website Contact Form:  $name";
        $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nCompany: $company\n\nPhone: $phone\n\nMessage:\n$message";
        $headers = "From: noreply@nickborisenko.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
        $headers .= "Reply-To: $email_address"; 
        mail($to,$email_subject,$email_body,$headers);
        return true;
    }
    if(isset($_POST['send'])){
        send();
    }        
?>


<!-- Some html -->


<form name="sentMessage" method="post" id="contactForm" novalidate>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Name</label>
                            <input type="text" class="form-control" placeholder="Name" name="name" id="name" required data-validation-required-message="Please enter your name.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Email Address</label>
                            <input type="email" class="form-control" placeholder="Email Address" name="email" id="email" required data-validation-required-message="Please enter your email address.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Phone Number</label>
                            <input type="tel" class="form-control" placeholder="Phone Number" name="phone" id="phone" required data-validation-required-message="Please enter your phone number.">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Company</label>
                            <input type="text" class="form-control" placeholder="Company" name="company" id="company">
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <div class="row control-group">
                        <div class="form-group col-xs-12 floating-label-form-group controls">
                            <label>Message</label>
                            <textarea rows="5" class="form-control" placeholder="Message" name="message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                            <p class="help-block text-danger"></p>
                        </div>
                    </div>
                    <br>
                    <div id="success"></div>
                    <div class="row">
                        <div class="form-group col-xs-12">
                            <button type="submit" name="send" class="btn btn-default">Send</button>
                        </div>
                    </div>
                </form>

and my postfix config file in /etc/postfix/main.cf:

smtpd_banner = ESMTP $mail_name (Ubuntu)
biff = no

append_dot_mydomain = no

readme_directory = no

smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = nickborisenko.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = nickborisenko.com, ubuntu-512mb-nyc3-01, localhost.localdomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_command = procmail -a "$EXTENSION"
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
inet_protocols = all
mydomain = nickborisenko.com
smtp_generic_maps = hash:/etc/postfix/generic

Edit: These logs are from my apache2/access.log:

136.167.247.240 - - [28/Mar/2016:17:35:15 -0400] "GET /contact.php HTTP/1.1" 200 2592 "http://nickborisenko.com/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"
::1 - - [28/Mar/2016:17:35:23 -0400] "OPTIONS * HTTP/1.0" 200 125 "-" "Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.14 (internal dummy connection)"
136.167.247.240 - - [28/Mar/2016:17:35:28 -0400] "POST /mail/contact_me.php HTTP/1.1" 200 247 "http://nickborisenko.com/contact.php" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"

These are the only logs I can find that change when I try to send the contact form, and they seem to be involved, although I don't see any errors…

Best Answer

The problem was with the way I handled the php. All I had to fix was to separate the function above into a validation function to validate the input and another to actually send the form if validation returned true. This fix doesn't exactly give me what I want in terms of validation, but the email was sent and that's the only thing I was worried about.

<?php
    function validate(){
        if(empty($_POST['name'])        ||
           empty($_POST['email'])       ||
           empty($_POST['phone'])       ||
           empty($_POST['message']) ||
           empty($_POST['company'])   ||
           !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
           {
            echo "No arguments Provided!";
            return false;
           }else{
            return true;
           }
    }

    function send(){
        $name = $_POST['name'];
        $email_address = $_POST['email'];
        $phone = $_POST['phone'];
        $message = $_POST['message'];
        $company = $_POST['company'];

        // Create the email and send the message
        $to = 'my@email';
        $email_subject = "Website Contact Form:  $name";
        $email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nCompany: $company\n\nPhone: $phone\n\nMessage:\n$message";
        $headers = "From: noreply@nickborisenko.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
        $headers .= "Reply-To: $email_address"; 
        mail($to,$email_subject,$email_body,$headers);
        return true;
    }
    if(isset($_POST['send'])){
        send();
    }        
?>

<form name="sentMessage" onsubmit="return validate()" method="post" id="contactForm" novalidate>