Jump to content

HTML Online Form - Post to PHP


Cybermax

Recommended Posts

I'm busy building an online html form that posts to a *.php file (Below). Were do I add in a line of code that sends the subscribe a copy of the completed form. On another note, the form successfully post when completed, but the client informed us that we hasn't received a copy - is there anything I'm missing?

<?php
if(isset($_POST['email'])) {
     
    $email_to = "EmailTo";
    $email_subject = "Online Booking Form";
     
     
    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['company_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['booking_date']) ||
        !isset($_POST['session_times']) ||
        !isset($_POST['number_players']) ||
        !isset($_POST['confirmation_code'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }
     
    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $company_name = $_POST['company_name']; // not required
    $email_from = $_POST['email']; // required
    $booking_date = $_POST['booking_date']; // required
    $session_times = $_POST['session_times']; // required
    $number_players = $_POST['number_players']; // required
    $confirmation_code = $_POST['confirmation_code']; // required

     
    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Online Booking Results.\r\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "First Name: ".clean_string($first_name)."\r\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\r\n";
    $email_message .= "Your Company: ".clean_string($company_name)."\r\n";
    $email_message .= "Email Address: ".clean_string($email_from)."\r\n";
    $email_message .= "Booking Date: ".clean_string($booking_date)."\r\n";
    $email_message .= "Session Times: ".clean_string($session_times)."\r\n";
    $email_message .= "Number of Players: ".clean_string($number_players)."\r\n";
    $email_message .= "Confirmation Code: ".clean_string($confirmation_code)."\r\n";
     
     
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion(). "\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n\r\n";

ini_set("SMTP","IPADDRESS");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "SendFrom");
ini_set('auth_username','Username');
ini_set('auth_password','Password');

@mail($email_from, $email_subject, $email_message, $headers); 
@mail($email_to, $email_subject, $email_message, $headers); 
header('Location: URL');
?>
 

<?php
}
?>

Link to comment
Share on other sites

First, do that:

 

Replace

@mail($email_from, $email_subject, $email_message, $headers); 
@mail($email_to, $email_subject, $email_message, $headers);

for this:

$result1 = mail($email_from, $email_subject, $email_message, $headers); 
$result2 = mail($email_to, $email_subject, $email_message, $headers); 
var_dump($result1);
var_dump($result2);

If both are true, that is OK. Else, we have a problem to send the e-mail.

 

If both are true, try send a e-mail to your email account.

If you recive the e-mail, may be your client have recive the e-mail in SPAM folder.

If you dont recive the e-mail, sooo... we can have 246524652465 possible erros.

 

But, first, test all that.

Edited by BrodaNoel
Link to comment
Share on other sites

Thank for the feedback. I made the changes and the following error is thrown;

 

Warning: mail() [function.mail]: Failed to connect to mailserver at "IPAddress" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Inetpub\vhosts\domain\httpdocs\FormMailAuth.php on line 78
SMTP Settings Verified and Tested
Warning: mail() [function.mail]: Failed to connect to mailserver at "IPAddress" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\Inetpub\vhosts\domain\httpdocs\FormMailAuth.php on line 79
SMTP Socket Verified and Tested
 bool(false) bool(false)
Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\domain\httpdocs\FormMailAuth.php:78) in C:\Inetpub\vhosts\domain\httpdocs\FormMailAuth.php on line 82

 

Kindly advise, thanks

Link to comment
Share on other sites

Ohhh...

I understand...

 

You must change this data for the correct data:

ini_set("SMTP","IPADDRESS");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "SendFrom");
ini_set('auth_username','Username');
ini_set('auth_password','Password');

You have all this information of your SMTP server?

You have a SMTP server? (your server should be enable this)

Link to comment
Share on other sites

I added those values in to protect my clients server. When running the online form the below error is encountered

 

mail() [function.mail]: SMTP server response: 503 This mail server requires authentication when attempting to send to a non-local e-mail address. Please check your mail client settings or contact your administrator to verify that the domain or address is defined for this server. in

Changes made to the *.php

$result1 = mail($email_from, $email_subject, $email_message, $headers); 
$result2 = mail($email_to, $email_subject, $email_message, $headers);
var_dump($result1); 
var_dump($result2);



 





Edited by Cybermax
Link to comment
Share on other sites

In order for the email to be sent the smtp server requires authentication. In other words before you can send the email, the smtp sever requires a username/password that is authorised to send the email. The standard mail() does not have this capability. There is no PHP setting for this either.

 

You will need to use the PHPMailer script I linked to. Look at the example code for sending an email using authentication here http://phpmailer.worxware.com/index.php?pg=examplebsmtp

Link to comment
Share on other sites

Wait wait wait...

This:

ini_set("SMTP","IPADDRESS");
ini_set("smtp_port", "25");
ini_set("sendmail_from", "SendFrom");
ini_set('auth_username','Username');
ini_set('auth_password','Password');

That isn't work... why that is not used by "mail()" function.

 

Try using this library: "PHPMailer".

Search about that in Google. This library use STMP server to sends emails and in a file configuration you can push the data to access at SMTP server.

 

I think that all the information is here:

https://github.com/PHPMailer/PHPMailer

 

But, if not, search about this library in Google. Is a single file, is very easy to use.

Link to comment
Share on other sites

Thanks for all the assistance guys. Got a couple of more questions regarding the script. I have tested all the changes and I cant seem to get a confirmation sent to the subscribing user, I also need to change the format so the client receives the information in HTML and not TXT. Example Below

First Name:  *********
Last Name: *********
Company: *********
Telephone Number: *********

 

I also need to add in a line that sends the subscribing user (once the form is submitted) product information and pricing, either in HTML or PDF format. Any assistance would greatly be appreciated.
 

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.