Jump to content
screadore

Need help with this PHP contact form please. Cannot get it to work!

Recommended Posts

So I've been trying to get my contact forms to work on my website and I cannot figure out where to change my email so when they submit it that it goes to my email. Along with another problem being when I click the submit button it does not show if it was submitted or if there was an error, etc. PLEASE HELP the code is below! I put my email address in the section where I thought it would be, but it doesn't seem to be working.

<?php
header('Content-type: application/json');
require_once('php-mailer/PHPMailerAutoload.php'); // Include PHPMailer

$mail = new PHPMailer();
$emailTO = $emailBCC =  $emailCC = array();

// Enter Your Sitename
$sitename = 'levo...';

// Enter your email addresses: @required
$emailTO[] = array( 'email' => 'stefan@...', 'name' => 'Stefan' );

// Enable bellow parameters & update your BCC email if require.
//$emailBCC[] = array( 'email' => 'stefan', 'name' => 'Stefan' );

// Enable bellow parameters & update your CC email if require.
//$emailCC[] = array( 'email' => 'stefan@', 'name' => 'Stefan' );

// Enter Email Subject
$subject = "Contact Us" . ' - ' . $sitename;

// Success Messages
$msg_success = "We have <strong>successfully</strong> received your message. We'll get back to you soon.";

if( $_SERVER['REQUEST_METHOD'] == 'POST') {
   if (isset($_POST["cf_email"]) && $_POST["cf_email"] != '' && isset($_POST["cf_name"]) && $_POST["cf_name"] != '') {
      // Form Fields
      $cf_email = $_POST["cf_email"];
      $cf_name = $_POST["cf_name"];
      $cf_address = isset($_POST["cf_subject"]) ? $_POST["cf_subject"] : '';
      $cf_address = isset($_POST["cf_address"]) ? $_POST["cf_address"] : '';
      $cf_company = isset($_POST["cf_company"]) ? $_POST["cf_company"] : '';
      $cf_date = isset($_POST["cf_date"]) ? $_POST["cf_date"] : '';
      $cf_budget = isset($_POST["cf_budget"]) ? $_POST["cf_budget"] : '';
      $cf_msg = isset($_POST["cf_msg"]) ? $_POST["cf_msg"] : '';

      $honeypot  = isset($_POST["form-anti-honeypot"]) ? $_POST["form-anti-honeypot"] : '';
      $bodymsg = '';

      if ($honeypot == '' && !(empty($emailTO))) {
         $mail->IsHTML(true);
         $mail->CharSet = 'UTF-8';

         $mail->From = $cf_email;
         $mail->FromName = $cf_name . ' - ' . $sitename;
         $mail->AddReplyTo($cf_email, $cf_name);
         $mail->Subject = $subject;

         foreach( $emailTO as $to ) {
            $mail->AddAddress( $to['email'] , $to['name'] );
         }

         // if CC found
         if (!empty($emailCC)) {
            foreach( $emailCC as $cc ) {
               $mail->AddCC( $cc['email'] , $cc['name'] );
            }
         }

         // if BCC found
         if (!empty($emailBCC)) {
            foreach( $emailBCC as $bcc ) {
               $mail->AddBCC( $bcc['email'] , $bcc['name'] );
            }
         }

         // Include Form Fields into Body Message
         $bodymsg .= isset($cf_name) ? "Contact Name: $cf_name<br><br>" : '';
         $bodymsg .= isset($cf_email) ? "Contact Email: $cf_email<br><br>" : '';
         $bodymsg .= isset($cf_subject) ? "Contact Email: $cf_subject<br><br>" : '';
         $bodymsg .= isset($cf_address) ? "Contact Address: $cf_address<br><br>" : '';
         $bodymsg .= isset($cf_company) ? "Company: $cf_company<br><br>" : '';
         $bodymsg .= isset($cf_date) ? "Date: $cf_date<br><br>" : '';
         $bodymsg .= isset($cf_budget) ? "Budget: $cf_budget<br><br>" : '';
         $bodymsg .= isset($cf_msg) ? "Message: $cf_msg<br><br>" : '';
         $bodymsg .= $_SERVER['HTTP_REFERER'] ? '<br>---<br><br>This email was sent from: ' . $_SERVER['HTTP_REFERER'] : '';

         $mail->MsgHTML( $bodymsg );
         $is_emailed = $mail->Send();

         if( $is_emailed === true ) {
            $response = array ('result' => "success", 'message' => $msg_success);
         } else {
            $response = array ('result' => "error", 'message' => $mail->ErrorInfo);
         }
         echo json_encode($response);

      } else {
         echo json_encode(array ('result' => "error", 'message' => "Bot <strong>Detected</strong>.! Clean yourself Botster.!"));
      }
   } else {
      echo json_encode(array ('result' => "error", 'message' => "Please <strong>Fill up</strong> all required fields and try again."));
   }
}

 

Share this post


Link to post
Share on other sites

Looks to me like you are missing the code that sets the SMTP host info.

Share this post


Link to post
Share on other sites

could you give me an example of what it looks like? I have several PHP files in the folder that may be linked to it. I'm using an HTML template so maybe they put it somewhere else?

Share this post


Link to post
Share on other sites

Is the host on which this is running also an SMTP server? By default that is how PHPMailer works. If you have a different email server (e.g. gmail.com) then you need to specify that SMTP server and your login information. The PHPMailer page has an example.

Share this post


Link to post
Share on other sites

Note: I edited the original post and removed the comments about the code block. I also removed the email and site url specifics.

This is the line that sets that:

// Enter your email addresses: @required
$emailTO[] = array( 'email' => 'stefan@...', 'name' => 'Stefan' );

If that is your email address, then I don't see any obvious coding issues.  So to gw1500se's point, this would suggest a configuration issue with the server, where the Mail Transfer Agent which will be delivering your mail needs to be setup and working.  There are many things involved in getting a working MTA.  You'll likely need support from your hosting company.

Share this post


Link to post
Share on other sites

right now I'm running it off my computer from PYcharm. Could that be the problem? I'll try uploading it online and seeing if that works

Share this post


Link to post
Share on other sites
Posted (edited)

Now it's working. Thank you guys I was just an idiot and didn't realize it wouldn't work unless it was on a host... LOL

Edited by screadore

Share this post


Link to post
Share on other sites
1 hour ago, screadore said:

right now I'm running it off my computer from PYcharm. Could that be the problem? I'll try uploading it online and seeing if that works

Yes absolutely.  PyCharm is simply an editor/Integrated Development Environment.  You need some sort of server environment to test.  It's possible to make it work in a localhost or virtual server on your workstation, but for a smallish project like this one, probably not worth the trouble.  

Share this post


Link to post
Share on other sites

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.