-1

I know this question is common and I've read 100s of articles on it but being an amateur, I didn't find the perfect solution which I'm looking for.

I have a form on my website where the customers can book a service. I simply just want to know that where in the below codes I put the SMTP codes so that it runs fine.

I'm using GSuite for my business emails, so request you to provide the solution with the Google SMTP.

Thanks in Advance if anyone can help me out.

Your Beer will be on me if the solution is as desired.

HTML Form Code:

 <div class="col-sm-8 col-sm-offset-2">
                        <form id="test_form" action="va_contact.php" method="post">
      
      
      
                            <div class="row form-group">
       <div class="col-sm-12 g1">Your Information</div>
                                        
        <div class="col-sm-4"><label class="g2" for="first-name-1">First Name <font color="red">*</font></label>
        <input class="form-control" id="first-name-1" name="pt_first" value="" type="text" maxlength="50" required></div>
        
                                <div class="col-sm-4"><label class="g2" for="initial-1">Middle Initial</label>
        <input class="form-control" id="initial-1" name="pt_initial" value="" type="text" maxlength="5"></div>
        
        <div class="col-sm-4"><label class="g2" for="last-name-1">Last Name <font color="red">*</font></label>
        <input class="form-control" id="last-name-1" name="pt_last" value="" type="text" maxlength="50" required></div>
        
                                <div class="col-sm-12 g1">Address</div>
        
        <div class="col-sm-8"><label class="g2" for="street-address">Street Address <font color="red">*</font></label>
        <input class="form-control" id="street-address" name="pt_address" value="" type="text" maxlength="200"></div>
        <div class="col-sm-4"><label class="g2" for="city">City <font color="red">*</font></label>
        <input class="form-control" id="city" name="pt_city" value="" type="text" maxlength="50"></div>
        
        <div class="col-sm-4"><label class="g2" for="state">State/Province <font color="red">*</font></label>
        <input class="form-control" id="state" name="pt_state" value="" type="text" maxlength="50" required></div>
                                <div class="col-sm-4"><label class="g2" for="zip">Zip/Postal Code <font color="red">*</font></label>
        <input class="form-control" id="zip" name="pt_zip" value="" type="text" maxlength="10" required></div>
        <div class="col-sm-4"><label class="g2" for="country">Country <font color="red">*</font></label>
        <input class="form-control" id="country" name="pt_country" value="" type="text" maxlength="50" required></div>
        
        <div class="col-sm-12 g1">Contact Details</div>
        
        <div class="col-sm-4"><label class="g2" for="phone-type">Phone Type <font color="red">*</font></label>
        <select class="form-control" id="phone-type" name="pt_phone_type" required>
             <option selected="selected" value="">Please Select One</option>
          <option value="Cell">Mobile</option>
          <option value="Home">Home / Landline</option>
          <option value="Work">Work / Office</option>
          </select>
        </div>
        <div class="col-sm-4"><label class="g2" for="phone">Preferred Phone <font color="red">*</font></label>
        <input class="form-control" id="phone" name="pt_phone" value="" type="tel" maxlength="25" required></div>
        <div class="col-sm-4"><label class="g2" for="first-name-1">Alternate Phone</label>
        <input class="form-control" id="phone-alt" name="pt_phone_alt" value="" type="tel" maxlength="25"></div>
        
        <div class="col-sm-6"><label class="g2" for="email">Email Address <font color="red">*</font></label>
        <input class="form-control agileits-w3layouts" id="email" name="email" value="" type="email" maxlength="50" required></div>
        <div class="col-sm-6"><label class="g2" for="conf-email">Alternate Email Address</label>
        <input class="form-control" id="conf-email" name="alt_email" value="" type="email" maxlength="50"></div>
        
        <div class="col-sm-12 g1">Payment Information</div>
      
        <div class="col-sm-4"><label class="g2" for="card-type">Credit Card Type <font color="red">*</font></label>
        <select class="form-control" id="card-type" name="pt_card_type" required>
          <option selected="selected" value="">Please Select One</option>
          <option value="Cell">Visa</option>
          <option value="Home">Master Card</option>
          <option value="Work">American Express</option>
          <option value="Work">Maestro Card</option>
          <option value="Work">Other</option>
         </select></div>
        <div class="col-sm-4"><label class="g2" for="cc">Credit Card Number <font color="red">*</font></label>
        <input class="form-control" id="cc" name="pt_cc" value="" type="text" maxlength="20" required></div>
        <div class="col-sm-4"><label class="g2" for="cvv">CVV <font color="red">*</font></label>
        <input class="form-control" id="cvv" name="cvv" value="" type="password" maxlength="5" required></div>
        
        <div class="col-sm-8"><label class="g2" for="ccname">Name on Credit Card <font color="red">*</font></label>
        <input class="form-control" id="ccname" name="ccname" value="" type="text" maxlength="50" required></div>
        <div class="col-sm-4"><label class="g2" for="ed">Expiry Date <font color="red">*</font></label>
        <input class="form-control" id="ed" type="text" name="ed" placeholder="MM/YYYY" maxlength="7" required></div>
        
        <div class="col-sm-12 g1">Subscription Plans</div>
        <div class="col-sm-12 g2">Select the Virtual Assistant Service Plan</div>
        <div class="cc-selector col-sm-4">
        <label for="va-25" class="drinkcard-cc va-25">
         <input type="radio" id="va-25" name="testRadio" required value="va-25"> 
        </label>
        <label class="g3" for="first-name-1"><strong>Plan:</strong> $25 / Month</label>
        </div>
        <div class="cc-selector col-sm-4">
        <label for="va-109" class="drinkcard-cc va-109">
         <input type="radio" id="va-109" name="testRadio" value="va-109">
        </label>
        <label class="g3" for="first-name-1"><strong>Plan:</strong> $109 / Month</label>
        </div>
        <div class="cc-selector col-sm-4">
        <label for="va-179" class="drinkcard-cc va-179">
         <input type="radio" id="va-179" name="testRadio" value="va-179">
        </label>
        <label class="g3" for="first-name-1"><strong>Plan:</strong> $179 / Month</label>
        </div>
        <div class="cc-selector col-sm-4">
        <label for="va-349" class="drinkcard-cc va-349">
         <input checked="checked" type="radio" id="va-349" name="testRadio" value="va-349">
        </label>
        <label class="g3" for="first-name-1"><strong>Plan:</strong> $349 / Month</label>
        </div>
        <div class="cc-selector col-sm-4">
        <label for="va-599" class="drinkcard-cc va-599">
         <input type="radio" id="va-599" name="testRadio" value="va-599">
        </label>
        <label class="g3" for="first-name-1"><strong>Plan:</strong> $599 / Month</label>
        </div>
        <div class="cc-selector col-sm-4">
        <label for="va-999" class="drinkcard-cc va-999">
         <input type="radio" id="va-999" name="testRadio" value="va-999">
        </label>
        <label class="g3" for="first-name-1"><strong>Price:</strong> $999 / Month</label>
        </div>
        
        
        
        <div class="form-group col-sm-12">
        <label class="g2"><font color="#ffffff">.</font></label>
        
         <div class="form-group">
             
             <font size="2px" color="#000000">On submit, I assured that I have read and agree with the</font>
         
         <a href="javascript:void(0);" onclick="javascript:void window.open('https://www.xpertbaba.com/user-agreement','1496619568177','width=auto,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0');return false;"><font size="2px" color="#1880B4"> User Agreement (click here)</font></a>
         </div>
        </div>
        
        
        
        
        
                                <div class="col-sm-12 text-left">
                                    
         <button type="submit" value="Submit" class="btn">Submit</button>
                                </div>
                                
                            </div>
                        </form>
                    </div>

PHP Code:

<?php
/* Set e-mail recipient */
$myemail  = "email@example.com";
$subject = "New Sign Up";

/* Check all form inputs using check_input function */
$firstname = check_input($_POST['pt_first'], "Enter your First Name");
$lastname = check_input($_POST['pt_last'], "Enter your Last Name");
$middle_ini = check_input($_POST['pt_initial'], "Enter your Middle Name Initial");
$sadress  = check_input($_POST['pt_address'], "Enter your street address");
$city = check_input($_POST['pt_city'], "Enter your city");
$state = check_input($_POST['pt_state'], "Enter your state");
$zip   = check_input($_POST['pt_zip'], "Enter your zip code");
$country   = check_input($_POST['pt_country'], "Enter your country");
$phone_type    = check_input($_POST['pt_phone_type']);
$phone    = check_input($_POST['pt_phone']);
$alt_phone    = check_input($_POST['pt_phone_alt']);
$email    = check_input($_POST['email']);
$alt_email = check_input($_POST['alt_email']);
$cc_type  = check_input($_POST['pt_card_type']);
$cc_no   = check_input($_POST['pt_cc']);
$cc_cvv = check_input($_POST['cvv']);
$cc_ed = check_input($_POST['ed']);
$cc_name = check_input($_POST['ccname']);
$va_plan = check_input($_POST['testRadio']);


/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

Personal Information:

First Name: $firstname
Middle Initial: $middle_ini
Last Name: $lastname
Street Address: $sadress
City: $city
State: $state
Zip Code: $zip
Country: $country

Contact Information:

Phone Type: $phone_type
Phone Number: $phone
Alternate Phone No.:
E-mail: $email
Alternate Email: $alt_email

Payment Information:

Credit Card Type: $cc_type
Credit Card Number: $cc_no
CVV Number: $cc_cvv
Credit Card Expiry Date: $cc_ed
Name on Credit Card: $cc_name
Subscription Plan: $va_plan



End of message
";

/* Send the message using mail() function */
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($myemail, $subject, $message, $headers);

/* Redirect visitor to the thank you page */
header('Location: confirmation.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>
Miguel Almeida
  • 177
  • 3
  • 13
Jay T.
  • 1
  • So, basically you want to send mails with your PHP app containing the info your user had entered. I think your code flows just fine. But you probably need a mailer library like PHPMailer to replace the default `mail()`, so you can use an SMTP server. Take a look at a similar problem here: https://stackoverflow.com/questions/14456673/sending-email-with-php-from-an-smtp-server – ionizer Mar 20 '18 at 18:25
  • Thanks for your Comment. But I didn't get it. Can you let me know if any codes need to be written in the PHP file to get it done? – Jay T. Mar 20 '18 at 18:37
  • If there's any code you'll need to write, it's probably the settings and/or initialization for the PHP library you're using. Try looking into the documentation of your mailing library of preference, for example, PHPMailer. – ionizer Mar 20 '18 at 18:43

1 Answers1

0

Replace this code in your php file

/* Send the message using mail() function */
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($myemail, $subject, $message, $headers);

/* Redirect visitor to the thank you page */
header('Location: confirmation.html');
exit();

To

// Pear Mail Library
require_once "Mail.php";

$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
   header('Location: confirmation.html');
   exit();
}

Here is a link to pear mail library.

http://pear.php.net/package/Mail/docs

Smit Raval
  • 3,385
  • 1
  • 7
  • 23
  • Thanks for the Solution Brother. Let me try. Just one question, what is this file "Mail.php"? Do I need to create it or is it ok just to write it in the PHP and the code will work? – Jay T. Mar 20 '18 at 18:41
  • You need to follow the link I have added in my answer. Its a php mail library you have to download it and include it in your file. You will be good to go. Also if it has answered your primary question please mark it as approved. :) Thanks @JayT. – Smit Raval Mar 20 '18 at 18:56
  • I've gone through the link and followed the steps as well but on submitting the form, below error appears: www.example.com is currently unable to handle this request. HTTP ERROR 500 – Jay T. Mar 20 '18 at 19:07
  • You need to change $myemail to a valid email in your script so you can receive an email. – Smit Raval Mar 20 '18 at 19:12