我们可以通过电子邮件接收pubnub通知吗?

时间:2014-08-10 03:37:23

标签: pubnub

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

3 个答案:

答案 0 :(得分: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
)

geremy

答案 1 :(得分:2)

通过Python发送电子邮件的PubNub通知

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

您也可以使用Node.JS npm install sendgrid执行此操作。以下是Python示例:

以下是一个用法示例:

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

发布+电子邮件方法publish(...)

复制/粘贴以下python,以便在发送电子邮件和发布PubNub消息时轻松生活。我们正在与SendGrid电子邮件客户端配对,并附加了pip repo。

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## 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 :(得分:0)

有一种新的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();
    });
};