Skip to content

Instantly share code, notes, and snippets.

@m-hiyama
Last active August 31, 2015 06:00
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 m-hiyama/7c8fc61a9eb52437eb22 to your computer and use it in GitHub Desktop.
Save m-hiyama/7c8fc61a9eb52437eb22 to your computer and use it in GitHub Desktop.
Parse.com を使うサンプル
// -*- coding: utf-8 -*-
// 注意: ソースコードのエンコーディングは utf-8 にすること。
var MAIL_RECIPIENT = 'hiyama@××××××';
var MAILGUN_API_KEY = 'key-××××××××××××××××';
var MAILGUN_SANDBOX = 'sandbox××××××××××××××××';
var MAILGUN_API_URL = MAILGUN_SANDBOX + '.mailgun.org';
var mailgun = require('mailgun');
mailgun.initialize(MAILGUN_API_URL, MAILGUN_API_KEY);
// テストメールを送る
Parse.Cloud.define("sendTestMail", function(request, response) {
var from = 'Mailgun Sandbox <postmaster@' + MAILGUN_SANDBOX + '.mailgun.org>';
var to = MAIL_RECIPIENT;
var subject = 'Hello, this is test mail';
var text = 'Congratulations, everything is OK.';
mailgun.sendEmail(
{
to: to,
from: from,
subject: subject,
text: text
},
{
success: function(httpResponse) {
response.success("Email sent!");
},
error: function(httpResponse) {
console.dir(httpResponse);
response.error("Uh oh, something went wrong");
}
});
})
// 問い合わせフォームを処理する
Parse.Cloud.define("processContact", function(request, response) {
var name = request.params.name;
var email = request.params.email;
var message = request.params.message;
var from ="Mailgun Sandbox <postmaster@" + MAILGUN_SANDBOX + ".mailgun.org>";
var to = MAIL_RECIPIENT;
var subject = "お問い合せ";
var text =
"Name: " + name + "\n"+
"Email: " + email + "\n"+
"Message: \n" + message + "\n";
mailgun.sendEmail(
{
to: to,
from: from,
subject: subject,
text: text
},
{
success: function(httpResponse) {
response.success("Email sent!");
},
error: function(httpResponse) {
console.dir(httpResponse);
response.error("Uh oh, something went wrong");
}
});
});
<!-- -*- coding: utf-8 -*- -->
<!-- contact.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Contact</title>
<script src="https://www.parsecdn.com/js/parse-1.5.0.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="contact.js"></script>
<style type="text/css">
.success { color: white; background-color: green;}
.error { color; yellow; background-color: red;}
</style>
</head>
<body>
<h1>お問い合せ</h1>
<form id="commentForm">
<label for="name">お名前</label>
<input type="text" name="name" id="name" required><br>
<label for="email">メールアドレス</label>
<input type="email" name="email" id="email" required><br>
<label for="message">メッセージ<br/></label>
<textarea name="message" id="message" required></textarea><br>
<input type="submit" value="送信">
</form>
<br>
<div id="response"></div>
</body>
</html>
// -*- coding: utf-8 -*-
// contact.js
var PARSE_APPLICATION_ID='××××××××××××××××××××'
var PARSE_JAVASCRIPT_API_KEY='××××××××××××××××××××'
$(document).ready(function() {
Parse.initialize(PARSE_APPLICATION_ID, PARSE_JAVASCRIPT_API_KEY);
$("#commentForm").on("submit", function(ev) {
ev.preventDefault();
var data = {};
data.name = $("#name").val();
data.email = $("#email").val();
data.message = $("#message").val();
$('#response').html('').removeClass('success').removeClass('error');
Parse.Cloud.run("processContact", data, {
success:function(result) {
$('#response').html('送信成功').
addClass('success').fadeIn('fast');
},
error:function(error) {
console.dir(error);
$('#response').html('送信失敗:エラーが生じました').
addClass('error').fadeIn('fast');
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment