`
sillycat
  • 浏览: 2551426 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Mail Server Solution(6)Email Scan and Auth on Client

 
阅读更多
Mail Server Solution(6)Email Scan and Auth on Client

1. Does Gmail API Support Forwarding, NO
We can set up customer filter manually
https://support.google.com/mail/answer/6579?hl=en

We can use the admin SDK to set up filter for enterprise domain account with the administrator user.
https://developers.google.com/admin-sdk/email-settings/

Or we need to download the email content and send a new email ourselves.

2. Scan the Email and Download the Attachment from Client
I plan to use JavaScript. Here is the list
https://developers.google.com/api-client-library/

Here is the API for javascript.
https://developers.google.com/api-client-library/javascript/
https://developers.google.com/api-client-library/javascript/samples/samples

Gmail API JavaScript Client
https://developers.google.com/gmail/api/quickstart/js

3. Set up Env
Check the python version. Life is short, let’s use python. haha.
> python -V
Python 2.7.6

Create a very simple html file index.html
> cat index.html
Hello, Sillycat.

This will start the web server
> python -m SimpleHTTPServer 8000

We can visit the page for testing
http://localhost:8000/index.html

4. Gmail JavaScript API Implementation
<html>
  <head>
    <script type="text/javascript">
      // Your Client ID can be retrieved from your project in the Google
      // Developer Console, https://console.developers.google.com
      var CLIENT_ID = ‘xxxxx';

      var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];

      /**
       * Check if current user has authorized this application.
       */
      function checkAuth() {
        gapi.auth.authorize(
          {
            'client_id': CLIENT_ID,
            'scope': SCOPES,
            'immediate': true
          }, handleAuthResult);
      }

      /**
       * Handle response from authorization server.
       *
       * @param {Object} authResult Authorization result.
       */
      function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
          // Hide auth UI, then load client library.
          authorizeDiv.style.display = 'none';
          loadGmailApi();
        } else {
          // Show auth UI, allowing the user to initiate authorization by
          // clicking authorize button.
          authorizeDiv.style.display = 'inline';
        }
      }

      /**
       * Initiate auth flow in response to user clicking authorize button.
       *
       * @param {Event} event Button click event.
       */
      function handleAuthClick(event) {
        gapi.auth.authorize(
          {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
          handleAuthResult);
        return false;
      }

      /**
       * Load Gmail API client library. List labels once client library
       * is loaded.
       */
      function loadGmailApi() {
        gapi.client.load('gmail', 'v1', listLabels);
        gapi.client.load('gmail', 'v1', function(){
          listMessages("me", "craigslist.org has:attachment larger:50K", messageQueryCallback);
        });
      }

      function messageQueryCallback(res){
        //console.log("message query response:" + JSON.stringify(res, null, 4));
        gapi.client.load('gmail', 'v1', function(){
          getMessage("me", “xxxxxx", getMessageCallback);
        });
      }

      function getMessageCallback(res){
        //console.log("message get response:" + JSON.stringify(res.payload.parts, null, 4));
        var parts = res.payload.parts;
        for(var i = 0; i < parts.length; i++){
          //console.log("message get response:" + JSON.stringify(parts[i].mimeType));
          var type = parts[i].mimeType;
          if( type === 'application/pdf' || type === 'application/doc'){
            //console.log("message get response:" + JSON.stringify(parts[i].body.attachmentId));
            var attachmentId = JSON.stringify(parts[i].body.attachmentId);
            console.log("attachment get response:" + attachmentId);
            var attachmentId = “xxxxxxx";
            gapi.client.load('gmail', 'v1', function(){
              getAttachments("me", attachmentId, “xxxxxx", getAttachmentCallback);
            });

          }
        }
      }

      function getAttachmentCallback(res){
        //console.log("attachment get response:" + JSON.stringify(res.data, null, 4));

      }

      function getAttachments(userId, attachmentId, messageId, callback) {
            var request = gapi.client.gmail.users.messages.attachments.get({
              'id': attachmentId,
              'messageId': messageId,
              'userId': userId
            });
            request.execute(function(attachment) {
              callback(attachment);
            });
      }

      /**
       * Get Message with given ID.
       *
       * @param  {String} userId User's email address. The special value 'me'
       * can be used to indicate the authenticated user.
       * @param  {String} messageId ID of Message to get.
       * @param  {Function} callback Function to call when the request is complete.
       */
      function getMessage(userId, messageId, callback) {
        var request = gapi.client.gmail.users.messages.get({
          'userId': userId,
          'id': messageId
        });
        request.execute(callback);
      }

      /**
       * Retrieve Messages in user's mailbox matching query.
       *
       * @param  {String} userId User's email address. The special value 'me'
       * can be used to indicate the authenticated user.
       * @param  {String} query String used to filter the Messages listed.
       * @param  {Function} callback Function to call when the request is complete.
       */
      function listMessages(userId, query, callback) {
        var getPageOfMessages = function(request, result) {
          request.execute(function(resp) {
            result = result.concat(resp.messages);
            var nextPageToken = resp.nextPageToken;
            if (nextPageToken) {
              request = gapi.client.gmail.users.messages.list({
                'userId': userId,
                'pageToken': nextPageToken,
                'q': query
              });
              getPageOfMessages(request, result);
            } else {
              callback(result);
            }
          });
        };
        var initialRequest = gapi.client.gmail.users.messages.list({
          'userId': userId,
          'q': query
        });
        getPageOfMessages(initialRequest, []);
      }


      /**
       * Print all Labels in the authorized user's inbox. If no labels
       * are found an appropriate message is printed.
       */
      function listLabels() {
        var request = gapi.client.gmail.users.labels.list({
          'userId': 'me'
        });

        request.execute(function(resp) {
          var labels = resp.labels;
          appendPre('Labels:');

          if (labels && labels.length > 0) {
            for (i = 0; i < labels.length; i++) {
              var label = labels[i];
              appendPre(label.name)
            }
          } else {
            appendPre('No Labels found.');
          }
        });
      }

      /**
       * Append a pre element to the body containing the given message
       * as its text node.
       *
       * @param {string} message Text to be placed in pre element.
       */
      function appendPre(message) {
        var pre = document.getElementById('output');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }

    </script>
    <script src="https://apis.google.com/js/client.js?onload=checkAuth">
    </script>
  </head>
  <body>
    <div id="authorize-div" style="display: none">
      <span>Authorize access to Gmail API</span>
      <!--Button for the user to click to initiate auth sequence -->
      <button id="authorize-button" onclick="handleAuthClick(event)">
        Authorize
      </button>
    </div>
    <pre id="output"></pre>
  </body>
</html>



References:
Mail Server Solution 1 ~ 5
http://sillycat.iteye.com/blog/2228923
http://sillycat.iteye.com/blog/2229222
http://sillycat.iteye.com/blog/2230801
http://sillycat.iteye.com/blog/2232877
http://sillycat.iteye.com/blog/2232889

http://stackoverflow.com/questions/24908700/mail-attachment-wrong-media-type-gmail-api
https://developers.google.com/gmail/api/guides/sending

https://developers.google.com/admin-sdk/email-settings/#manage_filters
https://developers.google.com/admin-sdk/email-settings/

https://developers.google.com/identity/sign-in/web/

meteor library load
http://stackoverflow.com/questions/22723300/how-do-you-include-3rd-party-javascript-libraries-in-meteor-js
https://atmospherejs.com/slava/gmail
package meteor library myself
http://www.meteorpedia.com/read/Packaging_existing_Libraries
https://github.com/MeteorCommunity/discussions/issues/14
分享到:
评论

相关推荐

    Laravel开发-laravel-auth-client

    在本文中,我们将深入探讨Laravel开发中的"laravel-auth-client",这是一个专门设计用于集成Jincor Auth服务的Artisan命令。Laravel是PHP框架中的一个流行选择,以其优雅的语法和强大的功能而受到开发者喜爱。Jincor...

    auth-client:认证客户端库

    【auth-client:认证客户端库】 `auth-client` 是一个专门设计用于处理认证流程的客户端库,它主要关注用户身份验证和授权的过程。这个库适用于Java开发者,尤其对那些需要在应用中集成安全认证机制的项目非常有用。...

    前端开源库-solid-auth-client

    6. **跨域兼容**:考虑到Web应用的特性,Solid Auth Client处理了跨域认证的问题,确保在不同域名下的应用间数据的无缝共享。 在使用Solid Auth Client时,开发者通常会遇到以下关键概念: - **WebID**:WebID是一...

    PyPI 官网下载 | middle_auth_client-3.7.0-py3-none-any.whl

    《PyPI官网下载:middle_auth_client-3.7.0-py3-none-any.whl——深入解析Python库的安装与使用》 在Python的世界里,PyPI(Python Package Index)是全球最大的Python软件仓库,提供了丰富的第三方库资源,极大地...

    PyPI 官网下载 | middle_auth_client-1.3.0-py3-none-any.whl

    标题提到的"PyPI官网下载 | middle_auth_client-1.3.0-py3-none-any.whl"意味着我们正在讨论一个可以从PyPI获取的Python库,名为`middle_auth_client`,版本号为1.3.0,它以.whl格式提供。 `.whl`文件是Python的二...

    AuthClient_Installer

    一款超强的网络监控软件,有了认证权限才能连接到网络!

    api-routes-apollo-server-and-client-auth

    自己部署使用部署示例:如何使用使用或执行以引导示例: npx create-next-app --example api-routes-apollo-server-and-client-auth api-routes-apollo-server-and-client-auth-app# oryarn create next-app --...

    Huawei Auth-Http Server 1.0 passwd文件泄露漏洞POC

    -u 指定单个url -l 指定url文件 先用fofa脚本爬取所有Huawei Auth-Http相关资产(fofa脚本下载地址:) python3 fofa-cwillchris.py -k body="umweb/u20.png" 然后用此脚本 ./poc.bin -l url.txt

    suse Mailserver安装手册

    在IT领域,邮件服务器是企业通信的...阅读`suse mailserver.docx`和`email3_manual.pdf`中的详细信息,将为您提供更深入的指导和最佳实践。在实践中不断学习和优化,您的邮件服务器将能够为企业提供可靠的邮件服务。

    James mail server&client installation&configuration

    -- These servernames are used for both matcher/mailet processing and SMTP auth --&gt; &lt;!-- to determine when a mail is intended for local delivery. --&gt; ``` #### 四、客户端配置 客户端配置主要包括设置...

    邮件服务器Courier Mail Server配置说明

    **邮件服务器Courier Mail Server配置详解** Courier Mail Server是一款开源、免费的邮件服务器软件,它集成了IMAP、POP3和SMTP服务,适用于个人和小型企业使用。本篇将详细介绍如何配置Courier Mail Server,以...

    PyPI 官网下载 | django-rest-email-auth-2.1.0.tar.gz

    6. `src`或`django_rest_email_auth`目录:包含实际的源代码,可能有`models.py`(数据模型)、`views.py`(视图函数)、`serializers.py`(序列化器)、`permissions.py`(权限控制)等文件,实现了RESTful API与...

    rsync远程同步报错:ERROR: auth failed on module www rsync error: error starting client-server protocol (cod

    rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9] rsync服务器配置如下 [root@localhost ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration

    rsync常见错误及解决方法

    二、@ERROR: auth failed on module(rsync error: error starting client-server protocol (code 5)) 这种错误的原因是服务器端的模块需要验证用户名密码,但是客户端没有提供正确的用户名密码,认证失败。解决方法...

    office online server2016中文版 详细安装步骤

    Office Online Server 2016 中文版详细安装步骤 Office Online Server 2016 中文版是 Microsoft 公司推出的在线办公服务器解决方案,旨在提供一站式的办公体验。以下是 Office Online Server 2016 中文版的详细安装...

    3A客户端AuthClient2.0.5

    3A客户端, Auth Client 2.0.5 用于校园网络登录,

    远程控制编码server/client代码

    远程控制编码技术是一种在计算机网络中实现一台设备(通常称为客户端或client)对另一台设备(服务器或server)进行操作的技术。这种技术广泛应用于系统管理、技术支持、远程协作以及自动化任务执行等领域。在这个...

Global site tag (gtag.js) - Google Analytics