How to create a web service to send emails for you Android, iOS or web application
Since it's a common task to have to send emails from your app, this post outlines the quickest way to get a mail service up and running using server side JavaScript, Parse and Mandrill. No JavaScript or web coding experience is needed.
2. Download https://www.parse.com/downloads/windows/console/parse.zip
3. Extract the zip
4. Run parse console
5. cd into your working directory
6. run the command parse new <name-of-project>
7. make changes to your code if you like
8. run the command parse deploy
done
Set up Parse
1. Go to parse.com and create a cloud code app following the process2. Download https://www.parse.com/downloads/windows/console/parse.zip
3. Extract the zip
4. Run parse console
5. cd into your working directory
6. run the command parse new <name-of-project>
7. make changes to your code if you like
8. run the command parse deploy
done
Set up Mandrill
Mandrill is a is an email infrastructure service by MailChimp. It's free to use up to a limit of 12,000 emails per month (and 250 per hour) and it's easy to set up.
Head over to https://mandrill.com/ and sign up to get an API key.
The Code
Inside your new Parse project, there should be a folder called cloud, cd into that and create a file called main.js (if it doesn't already exist).
Paste the following code into cloud/main.js
Parse.Cloud.define("sendMail", function(request, response) { var Mandrill = require('mandrill'); Mandrill.initialize('<Manddrill_api_key>'); Mandrill.sendEmail({ message: { text: request.params.text, subject: request.params.subject, from_email: request.params.fromEmail, from_name: request.params.fromName, to: [{ email: request.params.toEmail, name: request.params.toName }] }, async: true }, { success: function(httpResponse) { console.log(httpResponse); response.success("Email sent!"); }, error: function(httpResponse) { console.error(httpResponse); response.error("ERROR - mail failed to send"); } }); });
And change the <api key> to your Mandrill API key (obviously withoiut the pointy brackets)
Once this id done, run parse deploy to push your work to parse.
Calling your service
Below is all the paramaters you'll need to send emails from your application.
URL:
https://api.parse.com/1/functions/sendMail
URL Parameter Key
|
Value
|
Content-Type
|
application/json
|
Accept
|
application/json
|
X-Parse-Application-Id
|
<Your_parse_application_id>
|
X-Parse-REST-API-Key
|
<Your_parse_rest_api_key>
|
Raw JSON body:
{
"toEmail":"someone@hotmail.com",
"toName":"jane doe",
"fromEmail":"someone_else@live.com",
"fromName":"john smith",
"text":"this is the email body for the main message",
"subject":"this is the email subject"
}
You will probably want to test this out before you include it in your app. A good way to do this is to use the PostMan client availible free on the Chrome store (similar versions will be out there for Firefox and Safari).
Fill in the form so that it looks like the image below, and you should see your new email service working :)
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment