【问题标题】:Automatically Sending Notification Email w/ Server使用服务器自动发送通知电子邮件
【发布时间】:2017-10-02 14:06:57
【问题描述】:

我需要在 linux 服务器上做一个通知发送者。这个想法是这个发件人每周在 JQL 中进行一次查询,如果它返回某些内容,则将电子邮件发送到某个列表。 我对服务器和 JavaScript 比较陌生。我已经在做 JQL 查询了,但是:

  1. 我需要发件人知道执行此操作的正确时间。触发 Sender 的最有效方式是什么?
  2. 如何在 JS 中将电子邮件从特定地址发送到电子邮件列表?

粗略的草图:

//magic, part 1

WaitUntilMonday();

var result = DoJQLQuery();

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

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

【问题讨论】:

    标签: javascript email server notifications automation


    【解决方案1】:

    您可以将 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
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2018-06-29
      • 2018-12-10
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      相关资源
      最近更新 更多