diff options
author | Elmer Thomas <elmer@thinkingserious.com> | 2016-05-06 19:26:23 -0700 |
---|---|---|
committer | Elmer Thomas <elmer@thinkingserious.com> | 2016-05-06 19:26:23 -0700 |
commit | 25f33c27274546dfbdd6a4ebc1b8f2dbe3048200 (patch) | |
tree | 1797e8d987fc66bca553047d2111b0232966b401 /examples/helpers/mail/example.js | |
parent | 0cf806985869ac7d17e3e164f8c6eb6d4485b4b9 (diff) | |
download | sendgrid-nodejs-25f33c27274546dfbdd6a4ebc1b8f2dbe3048200.zip sendgrid-nodejs-25f33c27274546dfbdd6a4ebc1b8f2dbe3048200.tar.gz sendgrid-nodejs-25f33c27274546dfbdd6a4ebc1b8f2dbe3048200.tar.bz2 |
Preparing for launch
Diffstat (limited to 'examples/helpers/mail/example.js')
-rw-r--r-- | examples/helpers/mail/example.js | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/examples/helpers/mail/example.js b/examples/helpers/mail/example.js new file mode 100644 index 0000000..271c268 --- /dev/null +++ b/examples/helpers/mail/example.js @@ -0,0 +1,162 @@ +function helloEmail(){ + var helper = require('../../../lib/helpers/mail/mail.js') + + mail = new helper.Mail() + email = new helper.Email("dx@sendgrid.com") + mail.setFrom(email) + + personalization = new helper.Personalization() + email = new helper.Email("elmer.thomas@sendgrid.com") + personalization.addTo(email) + mail.addPersonalization(personalization) + + mail.setSubject("Hello World from the SendGrid Node.js Library") + + content = new helper.Content("text/plain", "some text here") + mail.addContent(content) + content = new helper.Content("text/html", "<html><body>some text here</body></html>") + mail.addContent(content) + + return mail.toJSON() +} + +function kitchenSink(){ + var helper = require('../../../lib/helpers/mail/mail.js') + + mail = new helper.Mail() + email = new helper.Email("dx@sendgrid.com", "DX") + mail.setFrom(email) + + mail.setSubject("Hello World from the SendGrid Node.js Library") + + personalization = new helper.Personalization() + email = new helper.Email("elmer.thomas@sendgrid.com", "Elmer Thomas") + personalization.addTo(email) + email = new helper.Email("elmer.thomas@gmail.com", "Elmer Thomas Personal") + personalization.addTo(email) + email = new helper.Email("elmer.thomas+cc@sendgrid.com", "Elmer Thomas CC") + personalization.addCc(email) + email = new helper.Email("elmer.thomas+cc@gmail.com", "Elmer Thomas Personal CC") + personalization.addCc(email) + email = new helper.Email("elmer.thomas+bcc@sendgrid.com", "Elmer Thomas BCC") + personalization.addBcc(email) + email = new helper.Email("elmer.thomas+bcc@gmail.com", "Elmer Thomas Personal BCC") + personalization.addBcc(email) + personalization.setSubject("Hello World from the Personalized SendGrid Node.js Library") + header = new helper.Header("X-Test", "True") + personalization.addHeader(header) + header = new helper.Header("X-Test2", "False") + personalization.addHeader(header) + substitution = new helper.Substitution("%name%", "Elmer") + personalization.addSubstitution(substitution) + substitution = new helper.Substitution("%city%", "Riverside") + personalization.addSubstitution(substitution) + custom_arg = new helper.CustomArgs("timing", "morning") + personalization.addCustomArg(custom_arg) + custom_arg = new helper.CustomArgs("type", "marketing") + personalization.addCustomArg(custom_arg) + personalization.setSendAt(1443636899) + mail.addPersonalization(personalization) + + content = new helper.Content("text/plain", "some text here") + mail.addContent(content) + content = new helper.Content("text/html", "<html><body>some text here</body></html>") + mail.addContent(content) + content = new helper.Content("text/calendar", "Party Time") + mail.addContent(content) + + attachment = new helper.Attachment() + attachment.setContent("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") + attachment.setType("application/pdf") + attachment.setFilename("balance_001.pdf") + attachment.setDisposition("attachment") + mail.addAttachment(attachment) + + attachment = new helper.Attachment() + attachment.setContent("BwdW") + attachment.setType("image/png") + attachment.setFilename("banner.png") + attachment.setDisposition("inline") + attachment.setContentId("banner") + mail.addAttachment(attachment) + + mail.setTemplateId("439b6d66-4408-4ead-83de-5c83c2ee313a") + + section = new helper.Section("%section1%", "Textforasubstitutiontagofsection1") + mail.addSection(section) + section = new helper.Section("%section2%", "Textforasubstitutiontagofsection2") + mail.addSection(section) + + header = new helper.Header("X-Test3", "1") + mail.addHeader(header) + header = new helper.Header("X-Test4", "2") + mail.addHeader(header) + + category = new helper.Category("January") + mail.addCategory(category) + category = new helper.Category("2015") + mail.addCategory(category) + + custom_arg = new helper.CustomArgs("timing", "evening") + mail.addCustomArg(custom_arg) + custom_arg = new helper.CustomArgs("type", "summer_contest") + mail.addCustomArg(custom_arg) + + mail.setSendAt(1443636899) + + mail.setBatchId("some_batch_id") + + asm = new helper.ASM(3, [1,4,5]) + mail.setASM(asm) + + mail.setIpPoolName("23") + + mail_settings = new helper.MailSettings() + bcc = new helper.BCC(true, "elmer.thomas+spam@sendgrid.com") + mail_settings.setBcc(bcc) + footer = new helper.Footer(true, "some footer text", "<html><body>some footer text</body></html>") + mail_settings.setFooter(footer) + sandbox_mode = new helper.SandBoxMode(true) + mail_settings.setSandBoxMode(sandbox_mode) + spam_check = new helper.SpamCheck(true, 1, "https://gotchya.example.com") + mail_settings.setSpamCheck(spam_check) + mail.addMailSettings(mail_settings) + + tracking_settings = new helper.TrackingSettings() + click_tracking = new helper.ClickTracking(false, false) + tracking_settings.setClickTracking(click_tracking) + open_tracking = new helper.OpenTracking(true, "Optional tag to replace with the open image in the body of the message") + tracking_settings.setOpenTracking(open_tracking) + subscription_tracking = new helper.SubscriptionTracking(true, "text to insert into the text/plain portion of the message", "html to insert into the text/html portion of the message", "Optional tag to replace with the open image in the body of the message") + tracking_settings.setSubscriptionTracking(subscription_tracking) + ganalytics = new helper.Ganalytics(true, "some utm source", "some utc medium", "some utm term", "some utm content", "some utm campaign", "some utm name") + tracking_settings.setGanalytics(ganalytics) + mail.addTrackingSettings(tracking_settings) + + email = new helper.Email("dx+reply@sendgrid.com", "DX") + mail.setReplyTo(email) + + return mail.toJSON() +} + +function send(toSend){ + console.log(JSON.stringify(toSend, null, 2)) + //console.log(JSON.stringify(toSend)) + + var sg = require('../../../lib/sendgrid.js').SendGrid(process.env.SENDGRID_API_KEY, "3wyb2hp7emnqja6ys.stoplight-proxy.io") + + var requestBody = toSend + var emptyRequest = require('sendgrid-rest').request + var requestPost = JSON.parse(JSON.stringify(emptyRequest)) + requestPost.method = 'POST' + requestPost.path = '/v3/mail/send/beta' + requestPost.requestBody = requestBody + sg.API(requestPost, function (response) { + console.log(response.statusCode) + console.log(response.responseBody) + console.log(response.responseHeaders) + }) +} + +send(helloEmail()) +send(kitchenSink())
\ No newline at end of file |