screadore Posted July 11, 2019 Share Posted July 11, 2019 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.")); } } Quote Link to comment Share on other sites More sharing options...
gw1500se Posted July 11, 2019 Share Posted July 11, 2019 Looks to me like you are missing the code that sets the SMTP host info. Quote Link to comment Share on other sites More sharing options...
screadore Posted July 11, 2019 Author Share Posted July 11, 2019 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? Quote Link to comment Share on other sites More sharing options...
gw1500se Posted July 11, 2019 Share Posted July 11, 2019 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 11, 2019 Share Posted July 11, 2019 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. Quote Link to comment Share on other sites More sharing options...
screadore Posted July 11, 2019 Author Share Posted July 11, 2019 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 Quote Link to comment Share on other sites More sharing options...
screadore Posted July 11, 2019 Author Share Posted July 11, 2019 (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 July 11, 2019 by screadore Quote Link to comment Share on other sites More sharing options...
gizmola Posted July 11, 2019 Share Posted July 11, 2019 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.