自动发送带服务器的通知电子邮件

时间:2017-10-02 14:06:57

标签: javascript email server notifications automation

我需要在linux服务器上进行通知发送。这个想法是每周一次这个发件人在JQL中进行查询,如果它返回一些东西,则发送一个电子邮件到某个列表。 我对服务器和javascript很新。我已经在做JQL查询,但是:

  1. 我需要发件人知道合适的时间。触发发件人的最有效方法是什么?
  2. 如何将JS中的电子邮件从特定地址发送到电子邮件列表?
  3. 粗略草图:

    //magic, part 1
    
    WaitUntilMonday();
    
    var result = DoJQLQuery();
    
    if (result != '')
      SendNotificationEmail(from,to,message);
      
    //magic, part 2

    我试图搜索它,但我不知道从哪里开始。我也很欣赏任何阅读材料的建议。谢谢。

1 个答案:

答案 0 :(得分:0)

您可以将NODEJS与sendgrid api一起用于您的服务器端和电子邮件api。实际的客户端,您可以将其连接到网站上或只是运行nodejs应用程序。 因此,开始的地方是https://nodejs.org/http://sendgrid.com 这肯定会处理您的电子邮件需求。这是我用过的api键的一个功能(显然),对postEmail的调用实际上是发送邮件,其余的是验证。 发送电子邮件非常简单。祝你好运。

const SENDGRID_API_KEY = "SG.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

var helper = require('sendgrid').mail;
var sg = require('sendgrid')(SENDGRID_API_KEY);
var emptyRequest = require('sendgrid-rest').request;
var request = require("request");
    function makeEmail(whereFrom, whereTo, mysubject, mycontent){
      from_email = new helper.Email(whereFrom);
      to_email = new helper.Email(whereTo);
      subject = mysubject;
      content = new helper.Content("text/html", mycontent);

      mail = new helper.Mail(from_email, subject, to_email, content);
      custom_arg = new helper.CustomArgs("OURGUID", "ourcustomarg");
      mail.addCustomArg(custom_arg);
      var finished = mail.toJSON();


      var timer = 0;

      function postEmail(){
          var requestBody = finished;
          var requestPost = JSON.parse(JSON.stringify(emptyRequest));
          requestPost.method = 'POST';
          requestPost.path = '/v3/mail/send';
          requestPost.body = requestBody;
          sg.API(requestPost, function (error, response) {
            console.log(response.statusCode + ' STATUS')
             console.log('MESSAGE ID = '+ response.headers['x-message-id']) 
            if(response.statusCode == '202') {
              console.log(response.statusCode + ' EMAIL SENT TO SENDGRID AND QUED')
                          }//response if statement
          if(response.statusCode != '202'){
            console.log(response.statusCode + ' Something went wrong')}
                          })//end internal api function
            }//end api

    postEmail(finished);//actually sending the mail

      function getEmail(){
          var requestBody = finished;
          var requestPost = JSON.parse(JSON.stringify(emptyRequest));
          requestPost.method = 'GET';
          requestPost.path = '/v3/mail/send';
          requestPost.body = requestBody;
          sg.API(requestPost, function (error, response) {
            console.log(response.statusCode + ' SUCCESSFUL EMAIL');
             console.log('MESSAGE ID = '+ response.headers['x-message-id']) ;
                        })//end internal api function
            }//end api  


          function checkBounce(){
            console.log('this is checkBounce');
          var options = { method: 'GET',
            url: 'https://api.sendgrid.com/v3/suppression/bounces/'+ whereTo,
            headers: { authorization: 'Bearer your api key here' },
            body: '{}' };

          request(options, function (error, response, body) {
            if (error) throw new Error(error);
            console.log(response.statusCode);
            console.log(body);
          });
          }//check Bounce


          function checkInvalid(){
            console.log('This is check invalid');
          var options = { method: 'GET',
            url: 'https://api.sendgrid.com/v3/suppression/invalid_emails'+ whereTo,
            headers: { authorization: 'Bearer your api key here' },
            body: '{}' };

          request(options, function (error, response, body) {
            if (error) throw new Error(error);
            console.log(response.statusCode);
            console.log(body);
          });

          }//check Invalid

          function checkBlock(){
            console.log('This is check Block');
            var options = { method: 'GET',
             url: 'https://api.sendgrid.com/v3/suppression/blocks'+ whereTo,
             headers: { authorization: 'Bearer your api key here' },
             body: '{}' };

            request(options, function (error, response, body) {
              if (error) throw new Error(error);
              console.log(response.statusCode);
              console.log(body.created);
          });

          }//check Block



    function checkTest(){
            console.log('This is check Test');
            var options = { method: 'GET',
             url: 'https://api.sendgrid.com/v3/',

             headers: { authorization: 'Bearer your api key here' },
             body: '{}' };

            request(options, function (error, response, body) {
              if (error) throw new Error(error);
              body = JSON.parse(body);
              console.log(response.statusCode);
              console.log(body);



          });//end request


          }//check Processed

    }//make email end