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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值