Jump to content

Sending Form Response to myself via email - Autentication Required


LCNewbie

Recommended Posts

Hi Everyone. I am a little unsure if i am on the right thread so here goes!!

 

I am new to PHP but have good knowledge of HTML. I have put together a form and am using some PHP from a .NET magazine  article to validate the form etc. My is sending the email. Once i click send the form updates with a successful message but the email never arrives. I am told its because the server needs authentication. I am using 123 reg for my hosting ad have the details i need but evertime i try to put the code in i get an error from the server. I would appreciate any help anyone can give me on getting this to work.

 

The code is below.... Feel free to contact me if you need more information.  I look forward to hearing from you!! (The website is thiconstruction.co.uk

 

<?php

 

 

// Configure our email variables

$email_to = 'info@thi-construction.co.uk';

$email_subject = 'THI Form submission';

 

// Configure our required fields

$required_fields = array('fullname','email','comment');

 

// What are our error messages?

$error_messages = array(

'fullname' => 'Please enter your Name to continue.',

'email' => 'Please enter a valid Email Address to continue.',

'comment' => 'Please enter your Comment to continue.'

);

 

// Set our default form status

$form_complete = FALSE;

 

// Prepare our validation array

$validation = array();

 

// Let's check that the form was submitted

if(!empty($_POST)) {

// Sanitise our POST array

foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

 

// Loop through our requirements and make sure they are acceptable

foreach($required_fields as $field) {

// Firstly, has the field been submitted?

if(!array_key_exists($field, $_POST)) array_push($validation, $field);

 

// Secondly, is there actually any data in the field?

if($_POST[$field] == '') array_push($validation, $field);

 

// Lastly, let's validate the submitted email address

if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);

}

 

// Now we want to check the outcome of our basic validation

if(count($validation) == 0) {

// Prepare our content string

$email_content = 'New Website Comment: ' . "\n\n";

 

// Let's build our simple email content

foreach($_POST as $key => $value) {

if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";

}

 

// Identify the mail server, username, password, and port

  $server  = "smtp.123-reg.co.uk";

  $username = "info@thi-construction.co.uk";

  $password = "ry54lln330";

  $port    = "25";

 

 

// The validation passed, so send the email

$mail = $smtp->send($email_to, $email_subject, $email_content);

 

  // Configure the mailer mechanism

  $smtp = Mail::factory("smtp",

      array(

        "host"    => $server,

        "username" => $username,

        "password" => $password,

        "auth"    => true,

        "port"    => $port

      )

  );

 

// Update our form switch

$form_complete = TRUE;

}

}

 

function validate_email_address($email = FALSE) {

return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;

}

 

function remove_email_injection($field = FALSE) {

  return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));

}

 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>THI Construction Newbury </title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" type="text/css" href="thi.css" />

 

<script type="text/javascript" src="mootools-1.2.4-core-yc.js"></script>

<script type="text/javascript" src="validation.js"></script>

<script type="text/javascript">

var nameError = '<?php echo $error_messages['fullname']; ?>';

var emailError = '<?php echo $error_messages['email']; ?>';

var phoneError = '<?php echo $error_messages['phone']; ?>';

var commentError = '<?php echo $error_messages['comment']; ?>';

</script>

</head>

<body>

<div class='main' >

<table width='1024px' height='768px' align='center' class='main' border='0' cellpadding='15' cellspacing='none'>

<tr height='90px' >

<td colspan='2' ><img src='web-logo.png' width='300px' height='75px' align='left' valign='top' border='0' >

</td>

</tr>

<tr  class=navigation >

<td class='white' colspan="10">

  <a href='index.html' class=navigation target='_parent'>Home</a>       <a href='index.php' class=navigation target='_parent'>Enquiries</a>

</td>

 

</tr>

<tr >

<td  width='100%' valign='top'>

 <p>

 

<h1>THI Construction is a local Building and Home Improvement company based in Newbury, West Berkshire.</h1>

<h2> Although based in Newbury we also serve it's surrounding area's including but not exclusive to Thatcham, Cold Ash, Hermitage and Kintbury.

If you are not within one of these areas, please contact us, as you may still be within our area.  </h2>

 

<p>We are having technical issues at the moment, please email your enquiry to info@thi-construction.co.uk until we are able to solve the issue with the form submission</p>

 

 

 

 

 

</td>

</tr>

<tr>

<td>

 

<?php if($form_complete === FALSE): ?>

<form action="index.php" method="post" id="comments_form">

<fieldset>

<div>

<label for="fullname">Full Name: <strong>*</strong></label>

<input type="text" name="fullname" value="<?php echo isset($_POST['fullname'])? $_POST['fullname'] : ''; ?>" id="fullname" />

<?php if(in_array('fullname', $validation)): ?><span class="error"><?php echo $error_messages['fullname']; ?></span><?php endif; ?>

</div>

<div>

<label for="email">Email Address: <strong>*</strong></label>

<input type="text" name="email" value="<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>" id="email" />

<?php if(in_array('email', $validation)): ?><span class="error"><?php echo $error_messages['email']; ?></span><?php endif; ?>

</div>

<div>

<label for="phone">Phone Number: <strong>*</strong></label>

<input type="text" name="phone" value="<?php echo isset($_POST['phone'])? $_POST['phone'] : ''; ?>" id="phone" />

<?php if(in_array('phone', $validation)): ?><span class="error"><?php echo $error_messages['phone']; ?></span><?php endif; ?>

</div>

 

<div>

<label for="comment">Brief description: <strong>*</strong></label>

<textarea name="comment" id="comment"><?php echo isset($_POST['comment'])? $_POST['comment'] : ''; ?></textarea>

<?php if(in_array('comment', $validation)): ?><span class="error"><?php echo $error_messages['comment']; ?></span><?php endif; ?>

</div>

<p class="mand">* Mandatory fields</p>

<div class="submit">

<input type="submit" name="submit" value="Send" id="submit" />

</div>

</fieldset>

</form>

<?php else: ?>

<p>Thank you for your feedback! We are having issues at the moment, please email your enquiry to info@thi-construction.co.uk until we are able to solve the issue </p>

<?php endif; ?>

 

 

</td>

</tr>

<tr >

<td colspan=10 class=footer valign=bottom >

Registered Address: 3 Abbey Close, Newbury, Berkshire, RG14 7QX              Contact us:<a href='form.php' target='_parent'>  Enquiries</a><p>

</td>

</tr>

 

</table>

</div >

</body>

</html>

 

 

Kind regards Lisa

 

Link to comment
Share on other sites

This line attempts to send an email using the $smtp object.....

 

$mail = $smtp->send($email_to, $email_subject, $email_content);

 

These lines create the $smtp object.

     

$smtp = Mail::factory("smtp",
  array(
    "host"     => $server,
    "username" => $username,
    "password" => $password,
    "auth"     => true,
    "port"     => $port
  )
);

 

The problem is, in your code, the line that uses the $smtp object comes before the lines that actually create this object. Meaning the object doesn't even exist when you attempt to use it. So, the first line needs to go after the second series of lines.

Link to comment
Share on other sites

I understand. I have swapped the lines over as suggested thank you. That stops the horrible error coming up. I still dont recieve the email, and dont see the confirmation message displayed as i should. In firefox i get a blank page and ie i get 'this website cannot display the page'.  Can you tell if missing something? 

 

I appreciate your help! I am starting a digital media course at Uni as a mature student in a  few weeks and im sure once we do touch on php i can solve most of this myself but in the time inbetween i am very grateful!

 

:)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.