Skip to content

Instantly share code, notes, and snippets.

@OscarGodson
Created February 25, 2012 02:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OscarGodson/1905250 to your computer and use it in GitHub Desktop.
Save OscarGodson/1905250 to your computer and use it in GitHub Desktop.
Send mail via JS and JSONP with some shitty PHP code
<!DOCTYPE html>
<html>
<head>
<title>JS Bin</title>
<style>
</style>
</head>
<body>
<p id="sending">Sending mail...</p>
<script class="jsbin" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</body>
</html>
<?php
header('Content-Type: text/javascript; charset=utf8');
if(!isset($_GET['callback'])){
$callback = 'callback';
}
else{
$callback = $_GET['callback'];
}
if(!isset($_GET['to'])){
$to = 'nobody@nowhere.com';
}
else{
$to = $_GET['to'];
}
if(!isset($_GET['from'])){
$from = 'nobody@nowhere.com';
}
else{
$from = $_GET['from'];
}
if(!isset($_GET['subject'])){
$subject = '';
}
else{
$subject = $_GET['subject'];
}
if(!isset($_GET['message'])){
$message = '';
}
else{
$message = $_GET['message'];
}
echo $callback.'({status:'.mail($to, $subject, $message, 'From: '.$from).'});';
//I'm sure there is a better way of doing this, but it works.
?>
var emailData = {
to:'someone@somewhere.com',
from:'another@person.com',
subject:'Just a test',
message:'It worked!'
}
$.getJSON('http://kneedeepincode.com/sendmailjs?callback=?',emailData,function(data){
var result = 'ERROR';
if(data.status == 1){
result = 'Mail sent successfully, yo!';
}
$('#sending').text(result);
});
//Demo: http://jsbin.com/awakoj/edit#javascript,html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment