thegrid22593
11/22/2017 - 1:13 AM

Basic Jquery Ajax Send PHP Mail Script

https://app.sendgrid.com
 
(Add login info to MISC_CLIENT_ACCOUNTS)
 
Settings > API Keys > Create API Key > General Give Full Access Copy API Key
 
Settings> IP Access Management Get IP Adddress from https://www.easywhois.com/ or Media Temple Admin
 
Add lib & vendor folders to same folder as processing php
 
include files to top of processing php require 'vendor/autoload.php'; require 'lib/SendGrid.php';
<?php
include(str_replace('//','/',dirname(__FILE__).'/') .'../app/config.php');
include(str_replace('//','/',dirname(__FILE__).'/') .'mailchimp.php');
use \DrewM\MailChimp\MailChimp;

# | some unnecessary variable stuff for crappy servers
$referralURL = $_POST['referralURL'];
$notification = $_POST['notification'];
$email = $_POST['email'];
$source = $_POST['source'];

$firstName = addslashes($_POST['firstName']);
$lastName = addslashes($_POST['lastName']);
$address = strtolower($_POST['address']);
$city = addslashes($_POST['city']);
$state = addslashes($_POST['state']);
$zip = addslashes($_POST['zip']);
$email = addslashes($_POST['email']);
$phone = addslashes($_POST['phone3']);
//$interestedin = addslashes($_POST['interestedin']);
$comments = addslashes($_POST['comments']);

// Checkbox Array
if (is_array($_POST['interestedin']) === true) {
	$cbArray = $_POST[ 'interestedin' ];
	for ( $i = 0; $i <= count($cbArray); $i++ ) {
		$cbFinal .= $cbArray[$i];
		if ( $i < (count($cbArray) - 1) ) {
			$cbFinal .= ', ';
		}
	}
} else {
	$cbFinal = $_POST['interestedin'];
}

if ($notification == "") {
	$notification = "p11creative@gmail.com";
}

if ($source == "") {
	$source = "Website";
}

# | ghetto domain test
$locDomain = $_SERVER['HTTP_REFERER'];
$posDomain = strpos($locDomain, $p11domaincheck);
if ($posDomain !== false) {
	$domainTest = "pass";
} else {
	$domainTest = "pass";
}

if ($domainTest == "pass" && $email != "") {
	
	$con = mysql_connect("$p11server","$p11username","$p11password");

	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("$p11database", $con);

	date_default_timezone_set('America/Los_Angeles');
	$currentDate = date("Y-m-d");

	# | dupe code
	$sqldupe = "SELECT * FROM contacts WHERE email = '$email'";
	if (!mysql_query($sqldupe,$con)) {
		die('Error: ' . mysql_error());
	}
	$duperesult=mysql_query($sqldupe);
	$duperows =mysql_num_rows($duperesult);

	# | additions need to go here

	echo $duperows;

	if ($duperows == 0) {

		$sql="INSERT INTO contacts (submitDate, source, firstName, lastName, address, city, state, zip, phone, email, how, comments) VALUES ('$currentDate', '$source', '$firstName', '$lastName', '$address', '$city', '$state', '$zip', '$phone', '$email', '$cbFinal', '$comments')";

		if (!mysql_query($sql,$con)) {
			die('Error: ' . mysql_error());
		}
	}

	$MailChimp = new MailChimp('69c02263961be4082fb7514e0aff6fcc-us16');
	$list_id = 'a9d44e3c78';
	
	$result = $MailChimp->post("lists/$list_id/members", array(
		'email_address' => $email,
		'status'        => 'subscribed',
		'merge_fields'  => array( 
			'FNAME' 	=> $firstName, 
			'LNAME' 	=> $lastName, 
			'INTEREST'  => $cbFinal,
			'ADDRESS' 	=> array(
				'addr1'   => $address,
				'addr2'   => '',
				'city'    => $city,
				'state'   => $state,
				'zip'     => $zip,
				'country' => ''
				),
			'ZIP' 		=> $zip,
			'PHONE' 	=> $phone,
		),
	));

	mysql_close($con);
	
}

?>
<?php

# | creasy special

include(str_replace('//','/',dirname(__FILE__).'/') .'../app/config.php');
require 'vendor/autoload.php';
require 'lib/SendGrid.php';
$sendgrid = new SendGrid('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');

# | some unnecessary variable stuff for crappy servers

$referralURL = $_POST['referralURL'];
$notification = $_POST['notification'];
$email = $_POST['email'];
$source = $_POST['source'];

$firstName = addslashes($_POST['firstName']);
$lastName = addslashes($_POST['lastName']);
$address = addslashes($_POST['address']);
$city = addslashes($_POST['city']);
$state = addslashes($_POST['state']);
$zip = addslashes($_POST['zip']);
$email = addslashes($_POST['email']);
$phone = addslashes($_POST['phone']);
//$interestedin = addslashes($_POST['interestedin']);
$comments = addslashes($_POST['comments']);

// Checkbox Array
if (is_array($_POST['interestedin']) === true) {
	$cbArray = $_POST[ 'interestedin' ];
	for ( $i = 0; $i <= count($cbArray); $i++ ) {
		$cbFinal .= $cbArray[$i];
		if ( $i < (count($cbArray) - 1) ) {
			$cbFinal .= ', ';
		}
	}
} else {
	$cbFinal = $_POST['interestedin'];
}

if ($notification == "") {
	$notification = "p11creative@gmail.com";
}

if ($source == "") {
	$source = "Website";
}

# | ghetto domain test
$locDomain = $_SERVER['HTTP_REFERER'];
$posDomain = strpos($locDomain, $p11domaincheck);
if ($posDomain !== false) {
	$domainTest = "pass";
} else {
	$domainTest = "pass";
}

# | database connection

if ($domainTest == "pass" && $email != "") {

	$con = mysql_connect("$p11server","$p11username","$p11password");

	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("$p11database", $con);

	date_default_timezone_set('America/Los_Angeles');
	$currentDate = date("Y-m-d");

	# | dupe code

	$sqldupe = "SELECT * FROM contacts WHERE email = '$email'";
	if (!mysql_query($sqldupe,$con)) {
		die('Error: ' . mysql_error());
	}
	$duperesult=mysql_query($sqldupe);
	$duperows =mysql_num_rows($duperesult);

	# | additions need to go here

	echo $duperows;

	if ($duperows == 0) {

		$sql="INSERT INTO contacts (submitDate, source, firstName, lastName, address, city, state, zip, phone, email, how, comments) VALUES ('$currentDate', '$source', '$firstName', '$lastName', '$address', '$city', '$state', '$zip', '$phone', '$email', '$cbFinal', '$comments')";

		if (!mysql_query($sql,$con)) {
			die('Error: ' . mysql_error());
		}

		$mail = array();
		$mail['sender']    = "$p11contact";
		$mail['recipient'] = "$notification";
		$mail['subject']   = "$p11pagename Interest List";
		$mail['header']    = sprintf("From: %s\n", $mail['sender']);
		$mail['header']   .= sprintf("Reply-To: %s",$mail['sender']);
		$mail['fields'][]  = array('label' => 'Source', 'variable' => $_POST['source']);
		$mail['fields'][]  = array('label' => 'First Name', 'variable' => $_POST['firstName']);
		$mail['fields'][]  = array('label' => 'Last Name', 'variable' => $_POST['lastName']);
		$mail['fields'][]  = array('label' => 'Address', 'variable' => $_POST['address']);
		$mail['fields'][]  = array('label' => 'City', 'variable' => $_POST['city']);
		$mail['fields'][]  = array('label' => 'State', 'variable' => $_POST['state']);
		$mail['fields'][]  = array('label' => 'Zip', 'variable' => $_POST['zip']);
		$mail['fields'][]  = array('label' => 'Phone', 'variable' => $_POST['phone']);
		$mail['fields'][]  = array('label' => 'Interested In', 'variable' => $cbFinal);
		$mail['fields'][]  = array('label' => 'Email', 'variable' => $_POST['email']);

		# | additions need to go here
		$mail['fields'][]  = array('label' => 'Comments', 'variable' => $_POST['comments']);

		# | message formatting
		$mail['message']   = "";

		foreach($mail['fields'] as $field) {
			$mail['message']  .= sprintf("%s: %s\r\n\r",$field['label'], stripslashes($field['variable']));
		}

		$clientalert = new SendGrid\Email();
		$clientalert
			->addTo($mail['recipient'])
			->setFrom($mail['sender'])
			->setSubject($mail['subject'])
			->setText($mail['message'])
		;

		try {
			$sendgrid->send($clientalert);
		} catch(\SendGrid\Exception $e) {
			echo $e->getCode();
			foreach($e->getErrors() as $er) {
				echo $er;
			}
		}

	}

	$sql="SELECT * FROM auto_response Where idNum = 1";
	$result=mysql_query($sql);

	if (!mysql_query($sql,$con)) {
	  die('Error: ' . mysql_error());
	}

	$i = 0;

	$idNumTwo=mysql_result($result,$i,"idNum");
	$copyBlockTwo=mysql_result($result,$i,"content");
	$copyTitle=mysql_result($result,$i,"title");

	$sendername = "$p11contactname";
	$senderemail = "$p11contact";
	$sender    = "$sendername <$senderemail>";

	$headers   = "From: $sender\r\n";
	$headers  .= "Content-Type: text/html; charset=iso-8859-1";

	$subject   = "$copyTitle";

	$message = '<!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>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
	<title>'.$p11contactname.'</title>
	<style type="text/css"><!--  Fix Outlook.com line height issue (IE).  Overrides 131% default  -->
	.ExternalClass * {
	line-height:100%;
	}
	td {
	-webkit-text-size-adjust:none;
	}
	</style>
	</head>

	<body bgcolor="#ffffff">
	<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
	<tr><td align="center">

	<table width="600" border="0" cellpadding="0" cellspacing="0">
	    <tr>
	        <td width="600" height="20" colspan="3" style="font-size:20px; line-height:20px;"></td>
	    </tr>
	    <tr>
	        <td align="center"><img src="'.$p11logo.'" alt="'.$p11contactname.'" title="'.$p11contactname.'" style="display: block;" /></td>
	    </tr>
	    <tr>
	        <td width="600" height="20" colspan="3" style="font-size:20px; line-height:20px;"></td>
	    </tr>
	    <tr>
	        <td align="center" style="font-family:Arial, Helvetica, sans-serif; color:'.$p11bodycolor.'; font-size:15px; line-height:25px;">'.stripslashes(nl2br($copyBlockTwo)).'</td>
	    </tr>
	    <tr>
	        <td width="600" height="20" colspan="3" style="font-size:20px; line-height:20px;"></td>
	    </tr>
	</table>

	</td></tr>
	</table>
	</body>
	</html>';

	$autoalert = new SendGrid\Email();
	$autoalert
		->addTo($email)
		->setFrom($p11contact)
		->setSubject($subject)
		->setHtml($message)
	;

	try {
		$sendgrid->send($autoalert);
	} catch(\SendGrid\Exception $e) {
		echo $e->getCode();
		foreach($e->getErrors() as $er) {
			echo $er;
		}
	}

	mysql_close($con);

}

?>
# | creasy special

include(str_replace('//','/',dirname(__FILE__).'/') .'../app/config.php');

define('SITE_BASE', dirname(__FILE__));
require_once SITE_BASE . '/mime.php';

# | some unnecessary variable stuff for crappy servers

$referralURL = $_POST['referralURL'];
$notification = $_POST['notification'];
$email = $_POST['email'];
$source = $_POST['source'];

$firstName = addslashes($_POST['firstName']);
$lastName = addslashes($_POST['lastName']);
$address = addslashes($_POST['address']);
$city = addslashes($_POST['city']);
$state = addslashes($_POST['state']);
$zip = addslashes($_POST['zip']);
$email = addslashes($_POST['email']);
$phone = addslashes($_POST['phone']);
$how = addslashes($_POST['how']);
$comments = addslashes($_POST['comments']);

if ($notification == "") {
	$notification = "p11creative@gmail.com";
}

if ($source == "") {
	$source = "Website";
}

# | ghetto domain test
$locDomain = $_SERVER['HTTP_REFERER'];
$posDomain = strpos($locDomain, $p11domaincheck);
if ($posDomain !== false) {
	$domainTest = "pass";
} else {
	$domainTest = "pass";
}

# | database connection

if ($domainTest == "pass" && $email != "") {

	$con = mysql_connect("$p11server","$p11username","$p11password");
	
	if (!$con) {
		die('Could not connect: ' . mysql_error());
	}
	
	mysql_select_db("$p11database", $con);
	
	date_default_timezone_set('America/Los_Angeles');
	$currentDate = date("Y-m-d");
	
	# | additions need to go here
	
	$sql="INSERT INTO contacts (submitDate, source, firstName, lastName, address, city, state, zip, phone, email, how, comments) VALUES ('$currentDate', '$source', '$firstName', '$lastName', '$address', '$city', '$state', '$zip', '$phone', '$email', '$how', '$comments')";	

	if (!mysql_query($sql,$con)) {
		die('Error: ' . mysql_error());
	}
	
	mysql_close($con);
			
	$mail = array();
	$mail['sender']    = "$p11contact";
	$mail['recipient'] = "$notification";
	$mail['subject']   = "$p11pagename Interest List";
	$mail['header']    = sprintf("From: %s\n", $mail['sender']);
	$mail['header']   .= sprintf("Reply-To: %s",$mail['sender']);
	$mail['fields'][]  = array('label' => 'Source', 'variable' => $_POST['source']);
	$mail['fields'][]  = array('label' => 'First Name', 'variable' => $_POST['firstName']);
	$mail['fields'][]  = array('label' => 'Last Name', 'variable' => $_POST['lastName']);
	$mail['fields'][]  = array('label' => 'Address', 'variable' => $_POST['address']);
	$mail['fields'][]  = array('label' => 'City', 'variable' => $_POST['city']);
	$mail['fields'][]  = array('label' => 'State', 'variable' => $_POST['state']);
	$mail['fields'][]  = array('label' => 'Zip', 'variable' => $_POST['zip']);
	$mail['fields'][]  = array('label' => 'Phone', 'variable' => $_POST['phone']);
	$mail['fields'][]  = array('label' => 'How', 'variable' => $_POST['how']);
	$mail['fields'][]  = array('label' => 'Email', 'variable' => $_POST['email']);
	
	# | additions need to go here
	$mail['fields'][]  = array('label' => 'Comments', 'variable' => $_POST['comments']);
		
	# | message formatting
	$mail['message']   = "";
			
	foreach($mail['fields'] as $field) {
		$mail['message']  .= sprintf("%s: %s\n",$field['label'], stripslashes($field['variable']));
	}
	
	# | mail dispatch
	mail( $mail['recipient'], $mail['subject'], $mail['message'], $mail['header']);
	
}

/* email */

$con = mysql_connect("$p11server", "$p11username", "$p11password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("$p11database", $con);

$sql="SELECT * FROM auto_response Where idNum = 1";
$result=mysql_query($sql);
$num=mysql_numrows($result);

if (!mysql_query($sql,$con)) {
  die('Error: ' . mysql_error());
}

mysql_close($con);

$i = 0;

$idNumTwo=mysql_result($result,$i,"idNum");
$copyBlockTwo=mysql_result($result,$i,"content");
$copyTitle=mysql_result($result,$i,"title");

$sendername = "$p11contactname";
$senderemail = "$p11contact";
$sender    = "$sendername <$senderemail>";

$headers   = "From: $sender\r\n";
$headers  .= "Content-Type: text/html; charset=iso-8859-1";

$subject   = "$copyTitle";

$message = '<!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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>'.$p11contactname.'</title>
<style type="text/css"><!--  Fix Outlook.com line height issue (IE).  Overrides 131% default  -->
.ExternalClass * {
line-height:100%;
}
td {
-webkit-text-size-adjust:none;
}
</style>
</head>

<body bgcolor="#ffffff">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffff">
<tr><td align="center">

<table width="600" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td width="600" height="20" colspan="3" style="font-size:20px; line-height:20px;"></td>
    </tr>
    <tr>
        <td align="center"><img src="'.$p11logo.'" alt="'.$p11contactname.'" title="'.$p11contactname.'" style="display: block;" /></td>
    </tr>
    <tr>
        <td align="center" style="font-family:Arial, Helvetica, sans-serif; color:'.$p11bodycolor.'; font-size:15px; line-height:25px;">'.stripslashes(nl2br($copyBlockTwo)).'</td>
    </tr>
    <tr>
        <td width="600" height="20" colspan="3" style="font-size:20px; line-height:20px;"></td>
    </tr>
</table>

</td></tr>
</table>
</body>
</html>';

mail($_POST['email'], $subject, $message, $headers);

// header("Location: $referralURL"); 
var dataString = $('#interest-list').serialize();
  console.log(dataString);

      $.ajax({
        type: "POST",
        url: "/admin/webservice/index.php",
        data: dataString,
        success: function() {

            $('#interest-list').fadeOut(function() {
              $(this).empty();
              $(this).html('<div id="success"><h4 class="twhite">Thank you for your interest.</h4><p class="twhite">Your information has been received.</p></div>');
              $('#interest-list')[0].reset();
              $('#interest-list').fadeIn();
              //ga('send', 'event', 'Contact', 'Submission', 'Interest List');
            });
          }
      });