Skip to content

Instantly share code, notes, and snippets.

@areski
Forked from ShuvoHabib/contact.html
Created April 16, 2019 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save areski/6029830f2938bc44d8bd13d8bfddc0e4 to your computer and use it in GitHub Desktop.
Save areski/6029830f2938bc44d8bd13d8bfddc0e4 to your computer and use it in GitHub Desktop.
Mailgun Contact Form
<div class="contact-form">
<form id="contactform" name="contactform" method="post">
<div class="row">
<div class="col-sm-6">
<input name="name" class="form-control" type="text" placeholder="Name*"/>
</div>
<div class="col-sm-6">
<input name="email" class="form-control" type="email" placeholder="Email*"/>
</div>
<div class="clearfix"></div>
<div class="col-sm-12">
<textarea name="message" cols="5" class="form-control"
placeholder="Message*"></textarea>
</div>
<div class="clearfix"></div>
<div class="col-md-12">
<button type="submit" class="btn btn-blue contact-submit">Submit</button>
</div>
</div>
</form>
</div> <!--contact form-->
<?php
if (empty($_POST) || !isset($_POST)) {
ajaxResponse('error', 'Post cannot be empty.');
} else {
$postData = $_POST;
$dataString = implode($postData, ",");
$mailgun = sendMailgun($postData);
}
function ajaxResponse($status, $message, $data = NULL, $mg = NULL) {
$response = array(
'status' => $status,
'message' => $message,
'data' => $data,
'mailgun' => $mg
);
$output = json_encode($response);
exit($output);
}
function sendMailgun($data) {
$api_key = 'key-**';
$api_domain = 'user.xyz';
$send_to = 'user@domain.com';
$name = $data['name'];
$email = $data['email'];
$content = $data['message'];
$messageBody = "Contact: $name ($email)\n\nMessage: $content";
$config = array();
$config['api_key'] = $api_key;
$config['api_url'] = 'https://api.mailgun.net/v2/' . $api_domain . '/messages';
$message = array();
$message['from'] = $email;
$message['to'] = $send_to;
$message['h:Reply-To'] = $email;
$message['subject'] = 'Mail From GagaGugu';
$message['text'] = $messageBody;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $config['api_url']);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "api:{$config['api_key']}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
$('#contactform').on('submit', function (e) {
$(this).attr("disabled", true);
$('.contact-submit').text('Sending Message...');
var dataString = $(".contact-form form").serialize();
$.ajax({
type: 'POST',
url: "contact.php",
data: dataString,
success: function () {
$('.contact-form form').hide();
$('.contact-form').html("<div id='message'></div>");
$('#message').html("<h2>Thanks, We got your Message!</h2>")
.append("<p>We will be in touch soon.</p>");
},
error: function (data) {
console.log('Silent failure.');
}
});
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment