Contact Form Problems

6 posts by 2 authors in: Forums > CMS Builder
Last Post: April 2, 2013   (RSS)

By Mikey - March 21, 2013

I'm having issues with a contact form that typically works great, but is giving me trouble on a Godaddy VPS CentOS 6.3 server.

Here's the code I'm using for my form:

<?php
  // contact form - process form
  if (@$_REQUEST['submitForm']) {

    // error checking
    $errorsAlerts = "";
    if (!@$_REQUEST['firstname'])                { $errorsAlerts .= "Please enter your first name!<br/>\n"; }
    if (!@$_REQUEST['lastname'])                { $errorsAlerts .= "Please enter your last name!<br/>\n"; }
    //if (!@$_REQUEST['title'])                { $errorsAlerts .= "Please enter your title!<br/>\n"; }
    //if (!@$_REQUEST['company'])                { $errorsAlerts .= "Please enter your company name!<br/>\n"; }
    if (!@$_REQUEST['address'])                { $errorsAlerts .= "Please enter your address!<br/>\n"; }
    //if (!@$_REQUEST['address2'])                { $errorsAlerts .= "Please enter your address 2!<br/>\n"; }
    if (!@$_REQUEST['city'])                    { $errorsAlerts .= "Please enter your city!<br/>\n"; }
    if (!@$_REQUEST['state'])                    { $errorsAlerts .= "Please select your state!<br/>\n"; }
    if (!@$_REQUEST['zipcode'])                    { $errorsAlerts .= "Please select your zipcode!<br/>\n"; }
    if (!@$_REQUEST['phone'])                    { $errorsAlerts .= "Please enter your phone number!<br/>\n"; }
    if (!@$_REQUEST['email'])                    { $errorsAlerts .= "Please enter your email!<br/>\n"; }
    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAlerts .= "Please enter a valid email (example: user@example.com)<br/>\n"; }
    if (!@$_REQUEST['message'])                    { $errorsAlerts .= "Please enter a message!<br/>\n"; }

    // send email user
    if (!$errorsAlerts) {
        $from        = $_REQUEST['email'];
        $headers    = 'MIME-Version: 1.0' . "\r\n"; 
        //$headers    .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
        $headers    .= 'Content-type: text/html; charset=utf-8' . "\r\n";
        //$headers     .= "Content-type: text/html\r\n"; 
        $headers    .="From: $from \r\n";
        $to            = "webdev@domain.net";
        $subject    = "{$_REQUEST['firstname']} {$_REQUEST['lastname']} submitted a message at {$_SERVER['HTTP_HOST']}";
        $message    = <<<__TEXT__
<div style='background-color:#006BAF; padding:24px; font-size:22px; font-family:Arial; color:#FFF; margin-bottom:12px;'> Please contact {$_REQUEST['firstname']} {$_REQUEST['lastname']}
</div>

<div style='padding:24px; font-size:14px; font-family:Arial; color:#003A46; margin-bottom:0px;'>
<strong>First Name:</strong> {$_REQUEST['firstname']}<br />
<strong>Last Name:</strong> {$_REQUEST['lastname']}<br />
<strong>Title:</strong> {$_REQUEST['title']}<br />
<strong>Company:</strong> {$_REQUEST['company']}<br />
<strong>Address:</strong> {$_REQUEST['address']}<br />
{$_REQUEST['address2']}<br />
<strong>City:</strong> {$_REQUEST['city']}<br />
<strong>State:</strong> {$_REQUEST['state']}<br />
<strong>Zipcode:</strong> {$_REQUEST['zipcode']}<br />
<strong>Phone Number:</strong> {$_REQUEST['phone']}<br />
<strong>Email:</strong> {$_REQUEST['email']}<br /><br />
<strong>Message:</strong> {$_REQUEST['message']}
</div>

<div style='background-color:#CFE2A7; padding:24px; font-size:14px; font-family:Arial; color:#FFF; margin-bottom:12px;'>
The user who sent this message had the IP address {$_SERVER['REMOTE_ADDR']}.</div>
__TEXT__;
      // Note: The above line must be flush left or you'll get an error
      // This is a PHP heredoc.  See: http://ca2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

      // send message
      // $mailResult = @mail($to, $subject, $message, "From: $from");
      // if (!$mailResult) { die("Mail Error: $php_errormsg"); }
      
      // send message 
      $mailResult = @mail($to, $subject, $message, $headers); 
      if (!$mailResult) { die("Mail Error: $php_errormsg"); }

      //
      $errorsAlerts = "Thank you for taking the time to contact us. A representative will be in contact.";
      $_REQUEST = array(); // clear form values
    }
  }   // contact form - end process form
?>

When I submit the message, it comes across as raw source code as see below and there's no from email address produced:

Content-type: text/html; charset=utf-8

From: johndoe@domain.com

<div style='background-color:#006BAF; padding:24px; font-size:22px; font-family:Arial; color:#FFF; margin-bottom:12px;'> Please contact John Doe
</div>

<div style='padding:24px; font-size:14px; font-family:Arial; color:#003A46; margin-bottom:0px;'>
<strong>First Name:</strong> John<br />
<strong>Last Name:</strong> Doe<br />
<strong>Title:</strong> CEO<br />
<strong>Company:</strong> ABC Company, Inc.<br />
<strong>Address:</strong> 1234 Main Street<br />
Suite 100 #200<br />
<strong>City:</strong> Atlanta<br />
<strong>State:</strong> GA<br />
<strong>Zipcode:</strong> 00000<br />
<strong>Phone Number:</strong> 000-000-0000<br />
<strong>Email:</strong> johndoe@domain.com<br /><br />
<strong>Message:</strong> test message 3
</div>

<div style='background-color:#CFE2A7; padding:24px; font-size:14px; font-family:Arial; color:#FFF; margin-bottom:12px;'>
The user who sent this message had the IP address 00.000.000.000.</div>

The DNS settings for the domain are set to use Godaddy's email servers - not the VPS's email, so the VPS server is relaying the message to the Godaddy email servers. Does anyone have any suggestions to get this working?

By Mikey - March 21, 2013

Jason... as always many thanks for your help! Your solution solved the issue.

Contact Form if statement for $to

By Mikey - April 2, 2013

Anyone have any suggestion on how to add an if statement to the contact form for who receives the message? See the example in red below of what I'm trying to achieve.

// send email user
    if (!$errorsAlerts) {
        $from        = $_REQUEST['email'];
$to          = if $business_detailsRecord['email_to']; // If no Department email exist then Business Details email address is used.
$to          = else if $it_departmentRecord['email_to']; // Else if an email address exist for the Department, then the Department email address trumps Business Details.
$to          = else 'business@domain.net'; // Otherwise if no email address exist for either Business Details nor Department, then there's a fall back email address.
        $subject     = "{$_REQUEST['firstname']} {$_REQUEST['lastname']} submitted a message at {$_SERVER['HTTP_HOST']}";
        $message     = <<<__TEXT__
<div style='background-color:#006BAF; padding:24px; font-size:22px; font-family:Arial; color:#FFF; margin-bottom:12px;'> Please contact {$_REQUEST['firstname']} {$_REQUEST['lastname']}
</div>

<div style='padding:24px; font-size:14px; font-family:Arial; color:#003A46; margin-bottom:0px;'>
<strong>First Name:</strong> {$_REQUEST['firstname']}<br />
<strong>Last Name:</strong> {$_REQUEST['lastname']}<br />
<strong>Title:</strong> {$_REQUEST['title']}<br />
<strong>Company:</strong> {$_REQUEST['company']}<br />
<strong>Address:</strong> {$_REQUEST['address']}<br />
{$_REQUEST['address2']}<br />
<strong>City:</strong> {$_REQUEST['city']}<br />
<strong>State:</strong> {$_REQUEST['state']}<br />
<strong>Zipcode:</strong> {$_REQUEST['zipcode']}<br />
<strong>Phone Number:</strong> {$_REQUEST['phone']}<br />
<strong>Email:</strong> {$_REQUEST['email']}<br /><br />
<strong>Message:</strong> {$_REQUEST['message']}
</div>

<div style='background-color:#CFE2A7; padding:24px; font-size:14px; font-family:Arial; color:#FFF; margin-bottom:12px;'>
The user who sent this message had the IP address {$_SERVER['REMOTE_ADDR']}.</div>
__TEXT__;
 
     $mailErrors = sendMessage(array(
       'to'      =>  $to,
       'from'    =>  $from,
       'subject' =>  $subject,
       'html'    =>  $message,
       
     ));
     
     if ($mailErrors) { die("Error sending email: $mailErrors"); }

      //
      $errorsAlerts = "Thank you for taking the time to contact us. A representative will be in contact.";
      $_REQUEST = array(); // clear form values
  }

Contact Form if statement for $to

By Jason - April 2, 2013

Hi Zick,

You can set up your if statement like this:

if ($business_detailsRecord['email_to']) {
    $to = $business_detailsRecord['email_to'];
  }
  elseif ($it_departmentRecord['email_to']) {
    $to = $it_departmentRecord['email_to'];
  }
  else {
    $to = "business@domain.net";
  }

Hope this helps

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Contact Form if statement for $to

By Mikey - April 2, 2013

Thanks Jason,

Works like a charm!

You're the best.

Zick