Mail Solution with Content.IO

本文介绍了一个基于Context IO的邮件解决方案,通过API实现邮件账户信息获取、邮件筛选及附件下载等功能。文章提供了使用Node.js客户端库的具体代码示例,并介绍了如何通过过滤条件来查找特定主题或带有指定文件类型的邮件。
Mail Solution with Content.IO

1. Introduction
The develop console is here
https://console.context.io

The API key and secret are here
https://console.context.io/#settings/keys

Lite API will be more powerful for web hook. 2.0 API will be more powerful to filter lists of historical messages.

2. Start from 2.0 API
Here is the clients.
https://context.io/docs/2.0/libraries

I directly go to there console and try the “explore”. This should work for me.
subject = /sillycat post ([0-9]*): (.*)$/
email = gmail.com
filename = /(.*?)\.(pdf|doc|docx|DOC|DOCX|PDF)$/

I will go on trying the nodeJS client library.
https://github.com/contextio/ContextIO-node

Based on the nodeJS API, here are some codes
fetchAccountInfoByEmail : function(email, callback) {
var logger = services.get('logger');
var cxtioClient = this.cxtioClient;

logger.trace("EmailScan - fetchAccountInfoByEmail - email = " + email);
cxtioClient.accounts().get({ "email": email}, function(err, data){
if(err){
logger.error("EmailScan - fetchAccountInfoByEmail - fail to fetch the account info.");
callback(err, null);
return ;
}
if(data){
logger.trace("EmailScan - fetchAccountInfoByEmail - get the account info with key = " + email);
logger.trace("EmailScan - fetchAccountInfoByEmail - get response info = " + JSON.stringify(data));
var msgBody = data.body;
logger.trace("EmailScan - fetchAccountInfoByEmail - get message body = " + JSON.stringify(msgBody));
if(msgBody.length > 0){
var account = _.first(msgBody);
logger.trace("EmailScan - fetchAccountInfoByEmail - get account = " + JSON.stringify(account));
callback(null, { id: account.id } );
return ;
}else{
logger.trace("EmailScan - fetchAccountInfoByEmail - fail to fetch any account info with key = " + email);
callback(null,null);
return ;
}
}else{
logger.trace("EmailScan - fetchAccountInfoByEmail - fail to fetch any account info with key = " + email);
callback(null,null);
return ;
}
});
}

Scan the emails with some filters
fetchSillycatJobsByAccountId : function(accountId, pager, callback) {
var sillycatFilter = {
subject: ‘/sillycat post ([0-9]*): (.*)$/',
from: ‘robot@sillycat.ddns.org'
} ;
var filter = _.merge(sillycatFilter, pager);

this.fetchEmailsWithFilter(accountId, filter, callback);
}

Scan the emails with attachment
fetchSillycatResumesByAccountId : function(accountId, pager, callback) {
var sillycatFilter = {
file_name: '/(.*?)\.(pdf|doc|docx|DOC|DOCX|PDF)$/',
email: ’sillycat.org'
} ;
var filter = _.merge(sillycatFilter, pager);

this.fetchEmailsWithFilter(accountId, filter, callback);
},

Private Method provide here for previous methods.

fetchEmailsWithFilter : function(accountId, filter, callback){
var logger = services.get('logger');
var cxtioClient = this.cxtioClient;

logger.trace("EmailScan - fetchEmailsWithFilter - accountId = " + accountId);
logger.trace("EmailScan - fetchEmailsWithFilter - filter = " + JSON.stringify(filter));

cxtioClient.accounts(accountId).messages().get(filter, function(err, data){
if(err){
logger.error("EmailScan - fetchEmailsWithFilter - fail to fetch the messages.");
callback(err, null);
return ;
}
if(data){
logger.trace("EmailScan - fetchEmailsWithFilter - get the messages info with filter = " + JSON.stringify(filter));
logger.trace("EmailScan - fetchEmailsWithFilter - get response info = " + JSON.stringify(data));
var msgBody = data.body;
logger.trace("EmailScan - fetchEmailsWithFilter - get message body = " + JSON.stringify(msgBody));
if(msgBody.length > 0 ){
callback(null, msgBody);
return ;
}else{
logger.warn("EmailScan - fetchEmailsWithFilter - fail to fetch any messages with filter = " + JSON.stringify(filter));
return ;
}
}else{
logger.warn("EmailScan - fetchEmailsWithFilter - fail to fetch any messages with filter = " + JSON.stringify(filter));
return ;
}
});
}

Fetch the Email Content
fetchMailContent: function(accountId, messageId, callback) {
var logger = services.get('logger');
var cxtioClient = this.cxtioClient;

logger.trace("EmailScan - fetchMailContent - accountId = " + accountId);
logger.trace("EmailScan - fetchMailContent - messageId = " + messageId);

cxtioClient.accounts(accountId).messages(messageId).body().get(function(err, data){
if(err){
logger.error("EmailScan - fetchMailContent - fail to fetch the mail content.");
callback(err, null);
return ;
}
if(data){
logger.trace("EmailScan - fetchMailContent - get response info = " + JSON.stringify(data));
var msgBody = data.body;
logger.trace("EmailScan - fetchMailContent - get message body = " + JSON.stringify(msgBody));
if(msgBody.length > 0){
var contents = _.filter(msgBody, function(item){
return (item.type==='text/plain');
});
logger.trace("EmailScan - fetchMailContent - get any plain result = " + JSON.stringify(contents));
if(contents.length > 0){
logger.trace("EmailScan - fetchMailContent - return the text content.");
callback(null, _.first(contents));
return ;
}else{
logger.trace("EmailScan - fetchMailContent - return the html content or etc.");
callback(null,_.first(msgBody));
return ;
}
}
}else{
logger.warn("EmailScan - fetchMailContent - fail to fetch any email content.");
return ;
}
});
},

Fetch the Attachment Link
fetchAttachmentLink: function(accountId, fileId, callback){
var logger = services.get('logger');
var cxtioClient = this.cxtioClient;

logger.trace("EmailScan - fetchAttachmentLink - accountId = " + accountId);
logger.trace("EmailScan - fetchAttachmentLink - fileId = " + fileId);

cxtioClient.accounts(accountId).files(fileId).content().get({ as_link: 1 },function(err, data){
if(err){
logger.error("EmailScan - fetchAttachmentLink - fail to fetch the file link.");
callback(err, null);
return ;
}
if(data){
logger.trace("EmailScan - fetchAttachmentLink - get response info = " + JSON.stringify(data));
var link = data.body;
logger.trace("EmailScan - fetchAttachmentLink - get link = " + link);
callback(null, link);
return ;
}
});
},

References:
https://www.context.io/
https://context.io/docs/lite/quickstart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值