问题
We are using NodeJS to publish messages. Is it possible for subscribers to receive the messages by email?
回答1:
PubNub Notifications by Email in Python
Geremy's response also is your solution for Ruby and I'm attaching a Python solution too. The best way to achieve sending an email today is to pair PubNub with a mail service provider such as SendGrid and you would do it like this in Python.
You can do this with Node.JS too npm install sendgrid
. Here follows Python example:
Here is a usage example:
## Send Email + Publish
publish( 'my_channel', { 'some' : 'data' } )
## Done!
Publish + Email method publish(...)
Copy/paste the following python to make your life easy when sending an Email and Publishing a PubNub message. We are pairing with SendGrid email client and the pip repo is attached.
## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## 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:
Currently, PubNub supports native PubNub, GCM, and APNS message endpoints. More info here on that: http://www.pubnub.com/how-it-works/mobile/
If you wanted to forward PubNub native messages to email (SMTP), it would be as easy as taking the message body, parsing it as needed, and then sending.
For example, some rough pseudocode in Ruby may look like this:
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
回答3:
There is a new way to receive PubNub messages as emails. Using the SendGrid BLOCK, you can subscribe a PubNub Function to a channel, and trigger an email with the message contents. SendGrid is an API for sending emails with a HTTP request. PubNub Functions are JavaScript event handlers that execute on every PubNub message over a provided channel. Here is the SendGrid BLOCK code. Make sure you sign up for SendGrid and provide your credentials to the PubNub Vault (a safe storage place for API keys and passwords).
// 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();
});
};
来源:https://stackoverflow.com/questions/25225300/can-we-receive-pubnub-notifications-by-email