post_form($_POST); logger('Hubspot reply:' . json_encode($hub_res)); $res = sendMail($_POST); logger($res); $hub_res['body'] = json_decode($hub_res['body']); header('Content-Type: application/json'); $data = []; if (@$hub_res['body']->inlineMessage) { $data['success'] = $hub_res['body']->inlineMessage; } else { $data['error'] = 'Invalid input, check your email or phone number'; } echo json_encode($data); /* header("Location: app.html"); */ } function logger($text) { if (!is_dir('logs')) { mkdir('logs'); } $text = date('c') . '| ' . $text; @file_put_contents('logs/' . date('Ymd') . '.log', $text . "\n", FILE_APPEND); } class Hubspothelper { private $url = 'https://api.hsforms.com/submissions/v3/integration/submit/%s/%s'; private $consent_text = "By continuing you agree to the Terms and Conditions and Privacy Policies."; private $filter_keys = []; public $send_ip = true; public $send_page_url = true; public $send_hubspotkey = true; public function set_key_filer($key) { $this->filter_keys[] = $key; } public function set_consent_text($str) { $this->consent_text = $str; } public function post_form($data) { $str = $this->decrypt_b64(); $url = vsprintf($this->url, $str); return $this->curl_post($url, $this->create_json($data)); } private function create_json($arr) { $json_data = []; if (isset($arr['terms'])) { $json_data['legalConsentOptions'] = [ 'consent' => [ 'consentToProcess' => true, 'text' => $this->consent_text, ], ]; unset($arr['terms']); } $json_data['fields'] = $this->array_to_fields($arr); $json_data['context'] = []; if ($this->send_ip) { $json_data['context']['ipAddress'] = $this->get_ip(); } if ($this->send_page_url) { $json_data['context']['pageUri'] = $_SERVER['HTTP_REFERER']; } if ($this->send_hubspotkey && isset($_COOKIE['hubspotutk'])) { $json_data['context']['hutk'] = $_COOKIE['hubspotutk']; } if (empty($json_data['context'])) { unset($json_data['context']); } return $json_data; } private function array_to_fields($arr) { $data = []; foreach ($arr as $key => $value) { if (in_array($key, $this->filter_keys)) { continue; } $data[] = ['name' => $key, 'value' => $value]; } return $data; } private function curl_post($url, $data) { $result = null; try { $ch = curl_init($url); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $result = curl_exec($ch); curl_close($ch); } catch (\Throwable $e) { echo $e->getMessage();die; error_log($e->getMessage()); } return [ 'code' => $httpcode, 'body' => $result, ]; } private function decrypt_b64() { return [ PORTAL_ID, FORM_ID, ]; } private function get_ip() { $ipaddress = ''; if (getenv('HTTP_CLIENT_IP')) { $ipaddress = getenv('HTTP_CLIENT_IP'); } else if (getenv('HTTP_X_FORWARDED_FOR')) { $ipaddress = getenv('HTTP_X_FORWARDED_FOR'); } else if (getenv('HTTP_X_FORWARDED')) { $ipaddress = getenv('HTTP_X_FORWARDED'); } else if (getenv('HTTP_FORWARDED_FOR')) { $ipaddress = getenv('HTTP_FORWARDED_FOR'); } else if (getenv('HTTP_FORWARDED')) { $ipaddress = getenv('HTTP_FORWARDED'); } else if (getenv('REMOTE_ADDR')) { $ipaddress = getenv('REMOTE_ADDR'); } else { $ipaddress = 'UNKNOWN'; } return $ipaddress; } } function sendMail($post) { $mail = new PHPMailer(true); try { //Server settings //$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = 'smtp.gmail.com'; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->Username = 'dev@appedology.com'; //SMTP username $mail->Password = '!@2021Pakist@nMAY@!';//'Pakist@n123!!@@'; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged $mail->Port = 587; //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above //Recipients $mail->setFrom('dev@appedology.com'); $mail->addAddress('presale@appedology.com'); //Add a recipient $body = ''; foreach ($post as $key => $value) { $body .= '
' . $key . ': ' . $value . '
'; } //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = 'New lead submission on Appedology Landing Page'; $mail->Body = $body; $mail->send(); return 'Message has been sent'; } catch (Exception $e) { return "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } }