【问题标题】:Can we receive pubnub notifications by email?我们可以通过电子邮件接收 pubnub 通知吗?
【发布时间】:2014-08-10 03:37:22
【问题描述】:

我们正在使用 NodeJS 发布消息。订阅者是否可以通过电子邮件接收消息?

【问题讨论】:

    标签: pubnub


    【解决方案1】:

    在 Python 中通过电子邮件发送 PubNub 通知

    Geremy 的回复也是您针对 Ruby 的解决方案,我也附上了 Python 解决方案。今天实现发送电子邮件的最佳方法是将 PubNub 与 SendGrid 等邮件服务提供商配对,您可以在 Python 中这样做。

    你也可以用 Node.JS 做到这一点npm install sendgrid。下面是 Python 示例:

    这是一个用法示例:

    ## Send Email + Publish
    publish( 'my_channel', { 'some' : 'data' } )
    ## Done!
    

    发布+Email方式publish(...)

    复制/粘贴以下 python 以使您在发送电子邮件和发布 PubNub 消息时更轻松。我们正在与 SendGrid 电子邮件客户端配对,并附上 pip 存储库。

    ## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    ## Send Email and Publish Message on PubNub
    ## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    import Pubnub    ## pip install Pubnub
    import sendgrid  ## pip install sendgrid
    
    def publish( channel, message ):
        # Email List
        recipients = [
            [ "john.smith@gmail.com", "John Smith" ],
            [ "jenn.flany@gmail.com", "Jenn Flany" ]
        ]
    
        # Info Callback
        def pubinfo(info): print(info)
    
        # Connection to SendGrid
        emailer = sendgrid.SendGridClient( 'user', 'pass',  secure=True )
        pubnub = Pubnub( publish_key="demo", subscribe_key="demo", ssl_on=True )
    
        # PubNub Publish
        pubnub.publish( channel, message, callback=pubinfo, error=pubinfo )
    
        # Email Message Payload
        email = sendgrid.Mail()
        email.set_from("PubNub <pubsub@pubnub.com>")
        email.set_subject("PubNub Message")
        email.set_html(json.dumps(message))
        email.set_text(json.dumps(message))
    
        ## Add Email Recipients
        for recipient in recipients:
            email.add_to("%s <%s>" % (recipient[1], recipient[0]))
    
        ## Send Email
        emailer.send(email)
    

    【讨论】:

      【解决方案2】:

      目前,PubNub 支持原生 PubNub、GCM 和 APNS 消息端点。更多信息:http://www.pubnub.com/how-it-works/mobile/

      如果您想将 PubNub 原生消息转发到电子邮件 (SMTP),只需获取消息正文,根据需要对其进行解析,然后发送即可。

      例如,Ruby 中的一些粗略伪代码可能如下所示:

      require 'net/smtp'
      require 'pubnub'
      
      def SMTPForward(message_text)
      
      # build the headers
      
          email = "From: Your Name <your@mail.address>
          To: Destination Address <someone@example.com>
          Subject: test message
          Date: Sat, 23 Jun 2001 16:26:43 +0900
          Message-Id: <unique.message.id.string@example.com>
      
          " + message_text # add the PN message text to the email body
      
          Net::SMTP.start('your.smtp.server', 25) do |smtp| # Send it!
              smtp.send_message email,
              'your@mail.address',
              'his_address@example.com'
          end        
      
      @my_callback = lambda { |envelope| SMTPForward(envelope.msg) } # Fwd to email
      
      pubnub.subscribe( # Subscribe on channel hello_world, fwd messages to my_callback
          :channel  => :hello_world,
          :callback => @my_callback
      )
      

      杰瑞米

      【讨论】:

      【解决方案3】:

      receive PubNub messages as emails 有一种新方法。使用SendGrid BLOCK,您可以将PubNub Function 订阅到频道,并触发包含消息内容的电子邮件。 SendGrid 是一个使用 HTTP 请求发送电子邮件的 API。 PubNub 函数是 JavaScript 事件处理程序,它通过提供的通道在每个 PubNub 消息上执行。这是 SendGrid BLOCK 代码。确保您注册了 SendGrid,并将您的凭据提供给 PubNub Vault(API 密钥和密码的安全存储位置)。

      // Be sure to place the following keys in MY SECRETS
      // sendGridApiUser - User name for the SendGrid account.
      // sendGridApiPassword - Password for the SendGrid account.
      // senderAddress - Email address for the email sender.
      
      const xhr = require('xhr');
      const query = require('codec/query_string');
      const vault = require('vault');
      
      export default (request) => {
          const apiUrl = 'https://api.sendgrid.com/api/mail.send.json';
      
          let sendGridApiUser, sendGridApiPassword, senderAddress;
      
          return vault.get('sendGridApiUser').then((username) => {
              sendGridApiUser = username;
              return vault.get('sendGridApiPassword');
          }).then((password) => {
              sendGridApiPassword = password;
              return vault.get('sendGridSenderAddress');
          }).then((address) => {
              senderAddress = address;
      
              // create a HTTP GET request to the SendGrid API
              return xhr.fetch(apiUrl + '?' + query.stringify({
                  api_user: sendGridApiUser,     // your sendgrid api username
                  api_key: sendGridApiPassword,  // your sendgrid api password
                  from: senderAddress,           // sender email address
                  to: request.message.to,             // recipient email address
                  toname: request.message.toname,     // recipient name
                  subject: request.message.subject,   // email subject
                  text: request.message.text +        // email text
                  '\n\nInput:\n' + JSON.stringify(request.message, null, 2)
              })).then((res) => {
                  console.log(res);
                  return request.ok();
              }).catch((e) => {
                  console.error('SendGrid: ', e);
                  return request.abort();
              });
          }).catch((e) => {
              console.error('PubNub Vault: ', e);
              return request.abort();
          });
      };
      

      【讨论】:

        猜你喜欢
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多