1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Adding Send Email Section

Discussion in 'Web Design & Development' started by EspressoBean, Jul 8, 2018.

Thread Status:
Not open for further replies.
Advertisement
  1. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    So I have a contact form that works but how should I have ti set to send out an email with the inputted contents?

    PHP:
    <?php
    function validateRecaptcha($secret$clientResponse$clientIp)
    {
        
    $data http_build_query([
            
    "secret"   => $secret,
            
    "response" => $clientResponse,
            
    "remoteip" => $clientIp,
        ]);

        
    $options = [
            
    "http" => [
                
    "header" =>
                    
    "Content-Type: application/x-www-form-urlencoded\r\n".
                    
    "Content-Length: ".strlen($data)."\r\n",
                    
    "method"  => "POST",
                    
    "content" => $data,
            ],
        ];

        
    $response file_get_contents(
            
    "https://www.google.com/recaptcha/api/siteverify",
            
    false,
            
    stream_context_create($options)
        );

        if(
    $response === false)
        {
            return 
    false;
        }
        else if((
    $arr json_decode($responsetrue)) === null)
        {
            return 
    false;
        }
        else
        {
            return 
    $arr["success"];
        }
    }

    $errors         = array();      // array to hold validation errors
    $data             = array();         // array to pass back data

    // validate the variables ======================================================
        // if any of these variables don't exist, add an error to our $errors array

        
    if (empty($_POST['firstName']))
            
    $errors['firstName'] = 'First Name is required.';

        if (empty(
    $_POST['lastName']))
            
    $errors['lastName'] = 'Last Name is required.';

        if (empty(
    $_POST['companyName']))
            
    $errors['companyName'] = 'Company Name is required.';

        if (empty(
    $_POST['companyAddress']))
            
    $errors['companyAddress'] = 'Company Address is required.';

        if (empty(
    $_POST['city']))
            
    $errors['city'] = 'City is required.';

        if (empty(
    $_POST['state']))
            
    $errors['state'] = 'State is required.';

        if (empty(
    $_POST['emailAddress']))
            
    $errors['emailAddress'] = 'Email Address is required.';

        if (empty(
    $_POST['comment']))
            
    $errors['comment'] = 'Comment is required.';

        if (empty(
    $_POST['g-recaptcha-response']))
            
    $errors['captcha'] = 'Captcha is required.';


    // return a response ===========================================================

        // if there are any errors in our errors array, return a success boolean of false

        
    if(!validateRecaptcha($secret$_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"]))
        {
            
    $errors['recaptcha'] = 'Captcha is required.';
        }

        if ( ! empty(
    $errors)) {

            
    // if there are items in our errors array, return those errors
            
    $data['success'] = false;
            
    $data['errors']  = $errors;
        } else {

            
    // if there are no errors process our form, then return a message

            // DO ALL YOUR FORM PROCESSING HERE
            // THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)

            // show a message of success and provide a true success variable
            
    $data['success'] = true;
            
    $data['message'] = 'Success!';
        }

        
    // return all our data to an AJAX call
        
    echo json_encode($data);
     
  2. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    There are just 2 more steps to execute:
    • You need to set up your email.
    • You need to sent it.
    This tut explains how to do this quite well;)
     
  3. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    typo:mad: should be 'send it';)
    You may also find Howard's thread of use
     
  4. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    Last edited: Jul 13, 2018
  5. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    Please, I am 67 years old and my eyesight is not what it was and hastebin doesn't cater for my ilk:(
    I would appreciate your using the TSG insert code facility (4th icon on the right) when posting code in a thread/reply(y)
    That said, I guess that you have all your 'include, use and require' scripts set up and present?
    If not then you'd probably get a 500 error for missing scripts ...
     
  6. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    Sorry about that, here we go:

    PHP:
    <?php
    //display errors
    ini_set('display_errors'1);
    //write errors to log
    ini_set('log_errors'1);
    //error log file name
    ini_set('log_errors''error.log');

    include 
    'email.php';
    use 
    PHPMailer\PHPMailer\PHPMailer;
    use 
    PHPMailer\PHPMailer\Exception;

    require 
    'Exception.php';
    require 
    'PHPMailer.php';
    require 
    'SMTP.php';

    /* VALIDATE RECAPTCHA */
    function validateRecaptcha($secret$clientResponse$clientIp)
    {
        
    $data http_build_query([
            
    "secret_code"   => $secret,
            
    "response" => $clientResponse,
            
    "remoteip" => $clientIp,
        ]);

        
    $options = [
            
    "http" => [
                
    "header" =>
                    
    "Content-Type: application/x-www-form-urlencoded\r\n".
                    
    "Content-Length: ".strlen($data)."\r\n",
                
    "method"  => "POST",
                
    "content" => $data,
            ],
        ];

        
    $response file_get_contents(
            
    "https://www.google.com/recaptcha/api/siteverify",
            
    false,
            
    stream_context_create($options)
        );

        if(
    $response === false)
        {
            return 
    false;
        }
        else if((
    $arr json_decode($responsetrue)) === null)
        {
            return 
    false;
        }
        else
        {
            return 
    $arr["success"];
        }
    }


    $errorMSG = array();


    /* NAME */

    if (empty($_POST["firstName"])) {

        
    $errorMSG[] = "Name is required";

    } else {

        
    $name $_POST["firsrtName"];

    }


    /* EMAIL */

    if (empty($_POST["emailAddress"])) {

        
    $errorMSG[] = "Email is required";

    } else if(!
    filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {

        
    $errorMSG[] = "Invalid email format";

    }else {

        
    $email $_POST["emailAddress"];

    }


    /* MSG SUBJECT */

    if (empty($_POST["lastName"])) {

        
    $errorMSG[] = "Subject is required";

    } else {

        
    $subject $_POST["lastName"];

    }


    /* MESSAGE */

    if (empty($_POST["comment"])) {

        
    $errorMSG[] = "Message is required";

    } else {

        
    $message $_POST["comment"];

    }

    /* RECAPTCHA */

    if (empty($_POST["g-recaptcha-response"])) {

        
    $errorMSG[] = "ReCaptcha is required";

    }


    if(empty(
    $errorMSG)){

    //PHPMailer

    $mail = new PHPMailer;

    //Enable SMTP debugging.
    $mail->SMTPDebug 3;
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();
    //Set SMTP host name
    $mail->Host "smtp.secureserver.com";
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth true;
    //Provide username and password
    $mail->Username "[email protected]";
    $mail->Password "password";
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure "tls";
    //Set TCP port to connect to
    $mail->Port 465;

    $mail->From $emailAddress;

    $mail->addAddress("[email protected]""Recepient Name");

    $mail->isHTML(true);

    $mail->Subject "Subject Text";
    $mail->Body $emailBody;

    $response = array();

    if(!
    $mail->send())
        {
          
    $msg "Email Failure";
          echo 
    json_encode(['code'=>200'msg'=>$msg]);
        }
        else
        {
          
    $msg "Form submit successfully";
          echo 
    json_encode(['code'=>200'msg'=>$msg]);
        }

    }


    ?>
     
  7. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    ...mmm?
     
  8. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    Sorry, I missed this.

    Yes the required items are all there.
     
  9. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    I rather think it's the php variables $emailAddress and $emailBody which have not been set up ...
     
  10. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    Here is email.php:

    PHP:
    <?php

    function writeBody() {

      return 
    "
      <!DOCTYPE html PUBLIC "
    -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
    <html xmlns="http://www.w3.org/1999/xhtml">
        <
    head>
          <
    title>THANK YOU</title>
          <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <
    meta http-equiv="X-UA-Compatible" content="IE=edge" />
          <
    meta name="viewport" content="width=device-width, initial-scale=1.0 " />
          <
    meta name="format-detection" content="telephone=no" />
          <!--[if !
    mso]><!-->
          <
    link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
          <!--<![endif]-->
          <
    style type="text/css">
            
    body {
            -
    webkit-text-size-adjust100% !important;
            -
    ms-text-size-adjust100% !important;
            -
    webkit-font-smoothingantialiased !important;
            }
            
    img {
            
    border!important;
            
    outlinenone !important;
            }
            
    {
            
    Margin0px !important;
            
    Padding0px !important;
            }
            
    table {
            
    border-collapsecollapse;
            
    mso-table-lspace0px;
            
    mso-table-rspace0px;
            }
            
    tdaspan {
            
    border-collapsecollapse;
            
    mso-line-height-ruleexactly;
            }
            .
    ExternalClass * {
            
    line-height100%;
            }
            
    span.MsoHyperlink {
            
    mso-style-priority:99;
            
    color:inherit;}
            
    span.MsoHyperlinkFollowed {
            
    mso-style-priority:99;
            
    color:inherit;}
            </
    style>
            <
    style media="only screen and (min-width:481px) and (max-width:599px)" type="text/css">
            @
    media only screen and (min-width:481px) and (max-width:599px) {
            
    table[class=em_main_table] {
            
    width100% !important;
            }
            
    table[class=em_wrapper] {
            
    width100% !important;
            }
            
    td[class=em_hide], br[class=em_hide] {
            
    displaynone !important;
            }
            
    img[class=em_full_img] {
            
    width100% !important;
            
    heightauto !important;
            }
            
    td[class=em_align_cent] {
            
    text-aligncenter !important;
            }
            
    td[class=em_pad_top]{
            
    padding-top:20px !important;
            }
            
    td[class=em_aside]{
            
    padding-left:10px !important;
            
    padding-right:10px !important;
            }
            
    td[class=em_height]{
            
    height20px !important;
            }
            
    td[class=em_space]{
            
    width:10px !important;
            }
            
    td[class=em_width55] {
            
    width:80px !important;
            
    text-align:center !important;
            }
            
    td[class=em_width75] {
            
    width:100px !important;
            }
            
    td[class=em_font]{
            
    font-size:14px !important;
            }
            
    td[class=em_font2] {
            
    text-align:center !important;
            }
            
    td[class=em_align_cent1] {
            
    text-aligncenter !important;
            
    padding-bottom10px !important;
            }
            }
            </
    style>
            <
    style media="only screen and (max-width:480px)" type="text/css">
            @
    media only screen and (max-width:480px) {
            
    table[class=em_main_table] {
            
    width100% !important;
            }
            
    table[class=em_wrapper] {
            
    width100% !important;
            }
            
    td[class=em_hide], br[class=em_hide], span[class=em_hide] {
            
    displaynone !important;
            }
            
    img[class=em_full_img] {
            
    width100% !important;
            
    heightauto !important;
            }
            
    td[class=em_align_cent] {
            
    text-aligncenter !important;
            }
            
    td[class=em_pad_top]{
            
    padding-top:20px !important;
            }
            
    td[class=em_height]{
            
    height20px !important;
            }
            
    td[class=em_aside]{
            
    padding-left:10px !important;
            
    padding-right:10px !important;
            }
            
    td[class=em_font]{
            
    font-size:14px !important;
            
    line-height:28px !important;
            }
            
    td[class=em_font1]{
            
    font-size:14px !important;
            
    line-height:18px !important;
            }
            
    td[class=em_font2]{
            
    font-size:14px !important;
            
    line-height:18px !important;
            
    text-align:center !important;
            }
            
    td[class=em_space]{
            
    width:10px !important;
            }
            
    span[class=em_br]{
            
    display:block !important;
            }
            
    td[class=em_width55] {
            
    width:55px !important;
            
    font-size:15px !important;
            
    line-height:19px !important;
            
    text-align:center !important;
            }
            
    td[class=em_width75] {
            
    width:75px !important;
            
    font-size:15px !important;
            
    line-height:19px !important;
            }
            
    td[class=em_align_cent1] {
            
    text-aligncenter !important;
            
    padding-bottom10px !important;
            }
            }
          </
    style>
        </
    head>
        <
    body style="margin:0px; padding:0px;" bgcolor="#ffffff">
          <
    table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">

            <!-- 
    BODY  -->
            <
    tr>
              <
    td align="center" valign="top"  bgcolor="#ffffff">
                <
    table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
                  <!-- 
    LOGO SECTION -->
                  <
    tr>
                    <
    td height="40" class="em_height">&nbsp;</td>
                  </
    tr>
                  <
    tr>
                    <
    td align="center"><a href="#" target="_blank" style="text-decoration:none;"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/logo.png" width="230" height="80" style="display:block;font-family: Arial, sans-serif; font-size:15px; line-height:18px; color:#30373b;  font-weight:bold;" border="0" alt="LoGo Here" /></a></td>
                  </
    tr>
                  <
    tr>
                    <
    td height="30" class="em_height">&nbsp;</td>
                  </
    tr>
                  <!-- 
    LOGO SECTION -->
                  <
    tr>
                    <
    td valign="top" class="em_aside">
                      <
    table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <!--
                        <
    tr>
                          <
    td height="50" class="em_height">&nbsp;</td>
                        </
    tr>
                      -->
                        <
    tr>
                          <
    td height="14" style="font-size:1px; line-height:1px;">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:18px; font-weight:bold; line-height:22px; color:#feae39; text-transform:uppercase;">New message from your website!</td>
                        </
    tr>
                        <
    tr>
                          <
    td height="25" class="em_height">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td height="1" bgcolor="#d6d7d8" style="font-size:0px;line-height:0px;"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif" width="1" height="1" alt="" style="display:block;" border="0" /></td>
                        </
    tr>
                        <
    tr>
                          <
    td height="27" class="em_height">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:22px; font-weight:bold; color:#30373b; text-transform:uppercase;">Message Details</td>
                        </
    tr>
                        <
    tr>
                          <
    td height="23" class="em_height">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td valign="top">
                            <
    table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <
    tr>
                                <
    td valign="top">
                                  <
    table width="290" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" bgcolor="#f6f7f8">
                                    <
    tr>
                                      <
    td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                        <
    span style="color:#feae39;">First Name :</span> <?php $firstName?>
                                      </td>
                                    </tr>
                                  </table>
                                  <table width="290" border="0" cellspacing="0" cellpadding="0" align="right" class="em_wrapper">
                                    <tr>
                                      <td valign="top" class="em_pad_top">
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0"  bgcolor="#f6f7f8" >
                                          <tr>
                                            <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                              <span style="color:#feae39;">Last Name :</span> <?php $lastName?>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <!-- START OF BUSINESS NAME AND PHONE NUMBER SECTION -->
                        <tr>
                          <td valign="top" class="em_aside">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td valign="top">
                                  <table width="290" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" bgcolor="#f6f7f8">
                                    <tr>
                                      <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                        <span style="color:#feae39;">Business Name :</span> <?php $businessName?>
                                      </td>
                                    </tr>
                                  </table>
                                  <table width="290" border="0" cellspacing="0" cellpadding="0" align="right" class="em_wrapper">
                                    <tr>
                                      <td valign="top" class="em_pad_top">
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0"  bgcolor="#f6f7f8" >
                                          <tr>
                                            <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                              <span style="color:#feae39;">Phone Number :</span> <?php $phoneNumber?>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <!-- END OF BUSINESS NAME AND PHONE NUMBER SECTION -->
                        <!-- START OF ADDRESS SECTION -->
                        <tr>
                          <td height="20" style="font-size:1px; line-height:1px;">&nbsp;</td>
                        </tr>
                        <tr>
                          <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8">
                              <tr>
                                <td width="25" class="em_space">&nbsp;</td>
                                <td valign="top">
                                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                    </tr>
                                    <tr>
                                      <td align="left" valign="top"  class="em_font1" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:20px; font-weight:bold; color:#feae39; text-transform:uppercase;">Address :</td>
                                    </tr>
                                    <tr>
                                      <td valign="top">
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                          <tr>
                                            <td align="left" width="180" class="em_hide">&nbsp;</td>
                                            <td valign="top">
                                              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                <tr>
                                                  <td height="10" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                  <td align="left" class="em_font2" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:20px; color:#30373b;"><?php $businessAddress?>
                                                  </td>
                                                </tr>
                                              </table>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                    <tr>
                                      <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                    </tr>
                                  </table>
                                </td>
                                <td width="25" class="em_space">&nbsp;</td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <!-- END OF ADDRESS SECTION -->
                        <!-- START OF MESSAGE SECTION -->
                        <td valign="top">
                          <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8">
                            <tr>
                              <td width="25" class="em_space">&nbsp;</td>
                              <td valign="top">
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                  </tr>
                                  <tr>
                                    <td align="left" valign="top"  class="em_font1" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:20px; font-weight:bold; color:#feae39; text-transform:uppercase;">Message :</td>
                                  </tr>
                                  <tr>
                                    <td valign="top">
                                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                          <td align="left" width="180" class="em_hide">&nbsp;</td>
                                          <td valign="top">
                                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                              <tr>
                                                <td height="10" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                              </tr>
                                              <tr>
                                                <td align="left" class="em_font2" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:20px; color:#30373b;"><?php $comments?>
                                                </td>
                                              </tr>
                                            </table>
                                          </td>
                                        </tr>
                                      </table>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                  </tr>
                                </table>
                              </td>
                              <td width="25" class="em_space">&nbsp;</td>
                            </tr>
                          </table>
                        </td>
                      </tr>
          </table>
        </body>
      </html>
      ";
    }
     
  11. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    Where is the code to set up $emailAddress and $emailBody?
     
  12. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    Hi JiminSA, sorry I gave you old code here is the updated code:

    Main PHP Script:

    PHP:
    <?php
    //display errors
    ini_set('display_errors'1);
    //write errors to log
    ini_set('log_errors'1);
    //error log file name
    ini_set('log_errors''error.log');

    include 
    'email.php';
    use 
    PHPMailer\PHPMailer\PHPMailer;
    use 
    PHPMailer\PHPMailer\Exception;

    require 
    'Exception.php';
    require 
    'PHPMailer.php';
    require 
    'SMTP.php';

    /* VALIDATE RECAPTCHA */
    function validateRecaptcha($secret$clientResponse$clientIp)
    {
        
    $data http_build_query([
            
    "secret_password"   => $secret,
            
    "response" => $clientResponse,
            
    "remoteip" => $clientIp,
        ]);

        
    $options = [
            
    "http" => [
                
    "header" =>
                    
    "Content-Type: application/x-www-form-urlencoded\r\n".
                    
    "Content-Length: ".strlen($data)."\r\n",
                
    "method"  => "POST",
                
    "content" => $data,
            ],
        ];

        
    $response file_get_contents(
            
    "https://www.google.com/recaptcha/api/siteverify",
            
    false,
            
    stream_context_create($options)
        );

        if(
    $response === false)
        {
            return 
    false;
        }
        else if((
    $arr json_decode($responsetrue)) === null)
        {
            return 
    false;
        }
        else
        {
            return 
    $arr["success"];
        }
    }


    $errorMSG = array();


    /* NAME */

    if (empty($_POST["firstName"])) {

        
    $errorMSG[] = "Name is required";

    } else {

        
    $name $_POST["firsrtName"];

    }


    /* EMAIL */

    if (empty($_POST["emailAddress"])) {

        
    $errorMSG[] = "Email is required";

    } else if(!
    filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {

        
    $errorMSG[] = "Invalid email format";

    }else {

        
    $email $_POST["emailAddress"];

    }


    /* MSG SUBJECT */

    if (empty($_POST["lastName"])) {

        
    $errorMSG[] = "Subject is required";

    } else {

        
    $subject $_POST["lastName"];

    }


    /* MESSAGE */

    if (empty($_POST["comment"])) {

        
    $errorMSG[] = "Message is required";

    } else {

        
    $message $_POST["comment"];

    }

    /* RECAPTCHA */

    if (empty($_POST["g-recaptcha-response"])) {

        
    $errorMSG[] = "ReCaptcha is required";

    }


    if(empty(
    $errorMSG)){

    //PHPMailer

    $mail = new PHPMailer;

    //Enable SMTP debugging.
    $mail->SMTPDebug 3;
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();
    //Set SMTP host name
    $mail->Host "mail.server.com";
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth true;
    //Provide username and password
    $mail->Username "[email protected]";
    $mail->Password "secret_password";
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure "tls";
    //Set TCP port to connect to
    $mail->Port 465;

    $mail->From $emailAddress;

    $mail->addAddress("[email protected]""Recepient Name");

    $mail->isHTML(true);

    $mail->Subject "Subject Text";
    $mail->Body $emailBody;

    $response = array();

    if(!
    $mail->send())
        {
          
    $msg "Email Failure";
          echo 
    json_encode(['code'=>200'msg'=>$msg]);
        }
        else
        {
          
    $msg "Form submit successfully";
          echo 
    json_encode(['code'=>200'msg'=>$msg]);
        }

    }


    ?>
    Email.PHP:

    PHP:
    <?php

    function emailBody() {

      return 
    "
      <!DOCTYPE html PUBLIC "
    -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      
    <html xmlns="http://www.w3.org/1999/xhtml">
        <
    head>
          <
    title>THANK YOU</title>
          <
    meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
          <
    meta http-equiv="X-UA-Compatible" content="IE=edge" />
          <
    meta name="viewport" content="width=device-width, initial-scale=1.0 " />
          <
    meta name="format-detection" content="telephone=no" />
          <!--[if !
    mso]><!-->
          <
    link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800" rel="stylesheet">
          <!--<![endif]-->
          <
    style type="text/css">
            
    body {
            -
    webkit-text-size-adjust100% !important;
            -
    ms-text-size-adjust100% !important;
            -
    webkit-font-smoothingantialiased !important;
            }
            
    img {
            
    border!important;
            
    outlinenone !important;
            }
            
    {
            
    Margin0px !important;
            
    Padding0px !important;
            }
            
    table {
            
    border-collapsecollapse;
            
    mso-table-lspace0px;
            
    mso-table-rspace0px;
            }
            
    tdaspan {
            
    border-collapsecollapse;
            
    mso-line-height-ruleexactly;
            }
            .
    ExternalClass * {
            
    line-height100%;
            }
            
    span.MsoHyperlink {
            
    mso-style-priority:99;
            
    color:inherit;}
            
    span.MsoHyperlinkFollowed {
            
    mso-style-priority:99;
            
    color:inherit;}
            </
    style>
            <
    style media="only screen and (min-width:481px) and (max-width:599px)" type="text/css">
            @
    media only screen and (min-width:481px) and (max-width:599px) {
            
    table[class=em_main_table] {
            
    width100% !important;
            }
            
    table[class=em_wrapper] {
            
    width100% !important;
            }
            
    td[class=em_hide], br[class=em_hide] {
            
    displaynone !important;
            }
            
    img[class=em_full_img] {
            
    width100% !important;
            
    heightauto !important;
            }
            
    td[class=em_align_cent] {
            
    text-aligncenter !important;
            }
            
    td[class=em_pad_top]{
            
    padding-top:20px !important;
            }
            
    td[class=em_aside]{
            
    padding-left:10px !important;
            
    padding-right:10px !important;
            }
            
    td[class=em_height]{
            
    height20px !important;
            }
            
    td[class=em_space]{
            
    width:10px !important;
            }
            
    td[class=em_width55] {
            
    width:80px !important;
            
    text-align:center !important;
            }
            
    td[class=em_width75] {
            
    width:100px !important;
            }
            
    td[class=em_font]{
            
    font-size:14px !important;
            }
            
    td[class=em_font2] {
            
    text-align:center !important;
            }
            
    td[class=em_align_cent1] {
            
    text-aligncenter !important;
            
    padding-bottom10px !important;
            }
            }
            </
    style>
            <
    style media="only screen and (max-width:480px)" type="text/css">
            @
    media only screen and (max-width:480px) {
            
    table[class=em_main_table] {
            
    width100% !important;
            }
            
    table[class=em_wrapper] {
            
    width100% !important;
            }
            
    td[class=em_hide], br[class=em_hide], span[class=em_hide] {
            
    displaynone !important;
            }
            
    img[class=em_full_img] {
            
    width100% !important;
            
    heightauto !important;
            }
            
    td[class=em_align_cent] {
            
    text-aligncenter !important;
            }
            
    td[class=em_pad_top]{
            
    padding-top:20px !important;
            }
            
    td[class=em_height]{
            
    height20px !important;
            }
            
    td[class=em_aside]{
            
    padding-left:10px !important;
            
    padding-right:10px !important;
            }
            
    td[class=em_font]{
            
    font-size:14px !important;
            
    line-height:28px !important;
            }
            
    td[class=em_font1]{
            
    font-size:14px !important;
            
    line-height:18px !important;
            }
            
    td[class=em_font2]{
            
    font-size:14px !important;
            
    line-height:18px !important;
            
    text-align:center !important;
            }
            
    td[class=em_space]{
            
    width:10px !important;
            }
            
    span[class=em_br]{
            
    display:block !important;
            }
            
    td[class=em_width55] {
            
    width:55px !important;
            
    font-size:15px !important;
            
    line-height:19px !important;
            
    text-align:center !important;
            }
            
    td[class=em_width75] {
            
    width:75px !important;
            
    font-size:15px !important;
            
    line-height:19px !important;
            }
            
    td[class=em_align_cent1] {
            
    text-aligncenter !important;
            
    padding-bottom10px !important;
            }
            }
          </
    style>
        </
    head>
        <
    body style="margin:0px; padding:0px;" bgcolor="#ffffff">
          <
    table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#ffffff">

            <!-- 
    BODY  -->
            <
    tr>
              <
    td align="center" valign="top"  bgcolor="#ffffff">
                <
    table width="600" cellpadding="0" cellspacing="0" border="0" align="center" class="em_main_table" style="table-layout:fixed;">
                  <!-- 
    LOGO SECTION -->
                  <
    tr>
                    <
    td height="40" class="em_height">&nbsp;</td>
                  </
    tr>
                  <
    tr>
                    <
    td align="center"><a href="#" target="_blank" style="text-decoration:none;"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/logo.png" width="230" height="80" style="display:block;font-family: Arial, sans-serif; font-size:15px; line-height:18px; color:#30373b;  font-weight:bold;" border="0" alt="LoGo Here" /></a></td>
                  </
    tr>
                  <
    tr>
                    <
    td height="30" class="em_height">&nbsp;</td>
                  </
    tr>
                  <!-- 
    LOGO SECTION -->
                  <
    tr>
                    <
    td valign="top" class="em_aside">
                      <
    table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <!--
                        <
    tr>
                          <
    td height="50" class="em_height">&nbsp;</td>
                        </
    tr>
                      -->
                        <
    tr>
                          <
    td height="14" style="font-size:1px; line-height:1px;">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:18px; font-weight:bold; line-height:22px; color:#feae39; text-transform:uppercase;">New message from your website!</td>
                        </
    tr>
                        <
    tr>
                          <
    td height="25" class="em_height">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td height="1" bgcolor="#d6d7d8" style="font-size:0px;line-height:0px;"><img src="https://www.sendwithus.com/assets/img/emailmonks/images/spacer.gif" width="1" height="1" alt="" style="display:block;" border="0" /></td>
                        </
    tr>
                        <
    tr>
                          <
    td height="27" class="em_height">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td valign="top" align="center" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:22px; font-weight:bold; color:#30373b; text-transform:uppercase;">Message Details</td>
                        </
    tr>
                        <
    tr>
                          <
    td height="23" class="em_height">&nbsp;</td>
                        </
    tr>
                        <
    tr>
                          <
    td valign="top">
                            <
    table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <
    tr>
                                <
    td valign="top">
                                  <
    table width="290" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" bgcolor="#f6f7f8">
                                    <
    tr>
                                      <
    td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                        <
    span style="color:#feae39;">First Name :</span> <?php $firstName?>
                                      </td>
                                    </tr>
                                  </table>
                                  <table width="290" border="0" cellspacing="0" cellpadding="0" align="right" class="em_wrapper">
                                    <tr>
                                      <td valign="top" class="em_pad_top">
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0"  bgcolor="#f6f7f8" >
                                          <tr>
                                            <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                              <span style="color:#feae39;">Last Name :</span> <?php $lastName?>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <!-- START OF BUSINESS NAME AND PHONE NUMBER SECTION -->
                        <tr>
                          <td valign="top" class="em_aside">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td valign="top">
                                  <table width="290" border="0" cellspacing="0" cellpadding="0" align="left" class="em_wrapper" bgcolor="#f6f7f8">
                                    <tr>
                                      <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                        <span style="color:#feae39;">Business Name :</span> <?php $emailAddress?>
                                      </td>
                                    </tr>
                                  </table>
                                  <table width="290" border="0" cellspacing="0" cellpadding="0" align="right" class="em_wrapper">
                                    <tr>
                                      <td valign="top" class="em_pad_top">
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0"  bgcolor="#f6f7f8" >
                                          <tr>
                                            <td align="center" valign="middle" class="em_font1" height="42" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; font-weight:bold; color:#30373b; text-transform:uppercase;">
                                              <span style="color:#feae39;">Phone Number :</span> <?php $phoneNumber?>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                  </table>
                                </td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <!-- END OF BUSINESS NAME AND PHONE NUMBER SECTION -->
                        <!-- START OF ADDRESS SECTION -->
                        <tr>
                          <td height="20" style="font-size:1px; line-height:1px;">&nbsp;</td>
                        </tr>
                        <tr>
                          <td valign="top">
                            <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8">
                              <tr>
                                <td width="25" class="em_space">&nbsp;</td>
                                <td valign="top">
                                  <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                      <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                    </tr>
                                    <tr>
                                      <td align="left" valign="top"  class="em_font1" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:20px; font-weight:bold; color:#feae39; text-transform:uppercase;">Address :</td>
                                    </tr>
                                    <tr>
                                      <td valign="top">
                                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                          <tr>
                                            <td align="left" width="180" class="em_hide">&nbsp;</td>
                                            <td valign="top">
                                              <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                                <tr>
                                                  <td height="10" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                  <td align="left" class="em_font2" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:20px; color:#30373b;"><?php $businessAddress?>
                                                  </td>
                                                </tr>
                                              </table>
                                            </td>
                                          </tr>
                                        </table>
                                      </td>
                                    </tr>
                                    <tr>
                                      <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                    </tr>
                                  </table>
                                </td>
                                <td width="25" class="em_space">&nbsp;</td>
                              </tr>
                            </table>
                          </td>
                        </tr>
                        <!-- END OF ADDRESS SECTION -->
                        <!-- START OF MESSAGE SECTION -->
                        <td valign="top">
                          <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#f6f7f8">
                            <tr>
                              <td width="25" class="em_space">&nbsp;</td>
                              <td valign="top">
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                  <tr>
                                    <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                  </tr>
                                  <tr>
                                    <td align="left" valign="top"  class="em_font1" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px; line-height:20px; font-weight:bold; color:#feae39; text-transform:uppercase;">Message :</td>
                                  </tr>
                                  <tr>
                                    <td valign="top">
                                      <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                        <tr>
                                          <td align="left" width="180" class="em_hide">&nbsp;</td>
                                          <td valign="top">
                                            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                              <tr>
                                                <td height="10" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                              </tr>
                                              <tr>
                                                <td align="left" class="em_font2" style="font-family:'Open Sans', Arial, sans-serif; font-size:17px;line-height:20px; color:#30373b;"><?php $comments?>
                                                </td>
                                              </tr>
                                            </table>
                                          </td>
                                        </tr>
                                      </table>
                                    </td>
                                  </tr>
                                  <tr>
                                    <td height="12" style="font-size:1px; line-height:1px;">&nbsp;</td>
                                  </tr>
                                </table>
                              </td>
                              <td width="25" class="em_space">&nbsp;</td>
                            </tr>
                          </table>
                        </td>
                      </tr>
          </table>
        </body>
      </html>
      ";
    }
     
  13. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    The error that shows up is:

    Code:
    Parse error: syntax error, unexpected '<' in /home/ser1/public_html/contact/w7/email.php on line 7
     
  14. EspressoBean

    EspressoBean Thread Starter

    Joined:
    Feb 29, 2016
    Messages:
    264
    This new code just shows up as white page, no errors or anything:

    PHP:
    <?php
    //display errors
    ini_set('display_errors'1);
    //write errors to log
    ini_set('log_errors'1);
    //error log file name
    ini_set('log_errors''error.log');

    use 
    PHPMailer\PHPMailer\PHPMailer;
    use 
    PHPMailer\PHPMailer\Exception;

    require 
    'Exception.php';
    require 
    'PHPMailer.php';
    require 
    'SMTP.php';

    /* VALIDATE RECAPTCHA */
    function validateRecaptcha($secret$clientResponse$clientIp)
    {
        
    $data http_build_query([
            
    "secret_password"   => $secret,
            
    "response" => $clientResponse,
            
    "remoteip" => $clientIp,
        ]);

        
    $options = [
            
    "http" => [
                
    "header" =>
                    
    "Content-Type: application/x-www-form-urlencoded\r\n".
                    
    "Content-Length: ".strlen($data)."\r\n",
                
    "method"  => "POST",
                
    "content" => $data,
            ],
        ];

        
    $response file_get_contents(
            
    "https://www.google.com/recaptcha/api/siteverify",
            
    false,
            
    stream_context_create($options)
        );

        if(
    $response === false)
        {
            return 
    false;
        }
        else if((
    $arr json_decode($responsetrue)) === null)
        {
            return 
    false;
        }
        else
        {
            return 
    $arr["success"];
        }
    }


    $errorMSG = array();


    /* NAME */

    if (empty($_POST["firstName"])) {

        
    $errorMSG[] = "Name is required";

    } else {

        
    $name $_POST["firsrtName"];

    }


    /* EMAIL */

    if (empty($_POST["emailAddress"])) {

        
    $errorMSG[] = "Email is required";

    } else if(!
    filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {

        
    $errorMSG[] = "Invalid email format";

    }else {

        
    $email $_POST["emailAddress"];

    }


    /* MSG SUBJECT */

    if (empty($_POST["lastName"])) {

        
    $errorMSG[] = "Subject is required";

    } else {

        
    $subject $_POST["lastName"];

    }


    /* MESSAGE */

    if (empty($_POST["comment"])) {

        
    $errorMSG[] = "Message is required";

    } else {

        
    $message $_POST["comment"];

    }

    /* RECAPTCHA */

    if (empty($_POST["g-recaptcha-response"])) {

        
    $errorMSG[] = "ReCaptcha is required";

    }


    if(empty(
    $errorMSG)){

    //PHPMailer

    $mail = new PHPMailer;

    //Enable SMTP debugging.
    $mail->SMTPDebug 3;
    //Set PHPMailer to use SMTP.
    $mail->isSMTP();
    //Set SMTP host name
    $mail->Host "mail.server.com";
    //Set this to true if SMTP host requires authentication to send email
    $mail->SMTPAuth true;
    //Provide username and password
    $mail->Username "[email protected]";
    $mail->Password "secret_password";
    //If SMTP requires TLS encryption then set it
    $mail->SMTPSecure "tls";
    //Set TCP port to connect to
    $mail->Port 465;

    $mail->From $emailAddress;

    $mail->addAddress("[email protected]""Recepient Name");

    $mail->isHTML(true);

    $mail->Subject "Subject Text";
    $mail->msgHTML("test body");

    $response = array();

    if(!
    $mail->send())
        {
          
    $msg "Email Failure";
          echo 
    json_encode(['code'=>200'msg'=>$msg]);
        }
        else
        {
          
    $msg "Form submit successfully";
          echo 
    json_encode(['code'=>200'msg'=>$msg]);
        }

    }


    ?>
     
  15. JiminSA

    JiminSA

    Joined:
    Dec 15, 2011
    Messages:
    3,240
    First Name:
    Jim
    Just replace " with ' on these lines in email.php:
    PHP:
      return "
    and
    PHP:
      ";
    right down the bottom:)
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1212585

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice