0

I just installed a fresh version of Ubuntu 14.04 Server on one of my computers. I installed everything to make it run like a typical web server (including PHP). My problem is, I have a contact form written in PHP that I'm running on my site and in it, I have a fail safe in case something goes wrong with submitting the form, which is what is happening. The script runs fine on any other server. But on mine, it goes to my message failed function every time I try to submit the form. I happen to think it's something wrong with the way PHP is configured on my server. Any ideas?

Here is the PHP script if anyone wants to test it:

<?php
$field_name = trim($_POST['cf_name']);
$field_email = trim($_POST['cf_email']);
$field_subject = trim($_POST['cf_subject']);
$field_message = trim($_POST['cf_message']);

if (empty($field_name) && empty($field_email) && empty($field_subject) && empty($field_message) && !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $field_email))
{
    ?>
    <script language="javascript" type="text/javascript">
        alert('One or more fields are invalid. Please fill out any empty fields and make sure your email address is valid.');
        window.location = 'contact.html';
    </script>
    <?php 
}

else if(empty($field_name))
{ 
    ?>
    <script language="javascript" type="text/javascript">
        alert('Please type in your name.');
        window.location = 'contact.html';
    </script>
    <?php
}

else if(empty($field_email))
{ 
    ?>
    <script language="javascript" type="text/javascript">
        alert('Please type an email address.');
        window.location = 'contact.html';
    </script>
    <?php
}

else if(empty($field_subject))
{
    ?>
    <script language="javascript" type="text/javascript">
        alert('Please type a subject.');
        window.location = 'contact.html';
    </script>
    <?php
}

else if(empty($field_message))
{ 
    ?>
    <script language="javascript" type="text/javascript">
        alert('Please type a message.');
        window.location = 'contact.html';
    </script>
    <?php
}

else if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $field_email))
{
    ?>
    <script language="javascript" type="text/javascript">
        alert('Please enter a valid email address.');
        window.location = 'contact.html';
    </script>
    <?php 
}

else
{
    $mail_to = '[email protected]';

    $subject = "New message from...";

    $body_message .= 'A message from a user of...'."\n\n";
    $body_message .= 'From: '.$field_name."\n";
    $body_message .= 'E-mail: '.$field_email."\n";
    $body_message .= 'Subject: '.$field_subject."\n";
    $body_message .= 'Message: '.$field_message;

    $headers = "From:";

    $mail_status = mail($mail_to, $subject, $body_message, $headers);

    if ($mail_status) { 
        ?>
        <script language="javascript" type="text/javascript">
            alert('Thanks for contacting me! I will respond as soon as possible.');
            window.location = 'contact.html';
        </script>
        <?php 
    } else { 
        ?>
        <!-- This is triggering every time I submit the form -->
        <script language="javascript" type="text/javascript">
            alert('Message failed. Please try again later.');
            window.location = 'contact.html';
        </script>
        <?php 
    }
} 
?>

Edit

The hosting side of things works fine.

3
  • What MTA are you using? Can you send other outgoing mail through it? Jul 18, 2014 at 0:32
  • Not sure. How do I find that out? When I went through the setup process, I chose to not configure the mail server. Is that my problem? Jul 18, 2014 at 1:16
  • Not necessarily a problem as you can use a mail server somewhere else. I'll add that to my answer. Jul 18, 2014 at 1:20

1 Answer 1

2

You can look up what a failed status of PHP mail() means here:

http://php.net/manual/en/function.mail.php

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

The false return value indicates the mail was not accepted for delivery by your MTA.

On a local Linux server, if you've done no other configuration changes, PHP will probably be trying to submit mail using the sendmail command. This command is provided by a number of different MTAs, such as postfix, or classic sendmail itself.

If you have a minimal Ubuntu installation you may not have an MTA installed at all. Otherwise, you may have one, but it may not be configured to be able to send outgoing mail.

Or, your computer may not have port 25 access to the internet. It is common for consumer-level connections to block outgoing connecitons on port 25 which effectively stops these computers sending outgoing email.


If you don't want to set up a full mail server (MTA) locally on your server or your ISP's policy prohibits you from doing so, you can relay mail through another server (such as your ISP's).

To do this you need to install a local MTA but configure it only to relay all outgoing mail through another server instead of trying to deliver it directly.

There are various lightweight MTAs which only exist to bridge the gap between a local sendmail function and a remote SMTP server, but it's probably better to install an MTA with proper queuing functionality like postfix, even though you will not be using most of postfix's features.

If you install postfix it's relatively easy to do this because Ubuntu gives you a menu when installing (if you missed the menu, you can use sudo dpkg-reconfigure postfix to do it again later, though that'll ask you even more questions). You probably want what Ubuntu calls a "satellite host", and you want to be final destination for no mail (eg nothing under "mydestination" or "relay_domains" in postfix's configuration, and use "relayhost" to refer to the external SMTP server).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .