Can we receive email notifications by email?

We use NodeJS to post messages. Is it possible for subscribers to receive emails?

+4
source share
2 answers

PubNub email notifications in Python

Geremy's answer is also your solution for Ruby , and I am attaching a Python solution . The best way to get your email sent today is to install PubNub with a mail service provider such as SendGrid, and you will do it like in Python.

You can do this with Node.JS too npm install sendgrid. The following is an example of Python:

Here is a usage example:

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

+ publish(...)

/ python, PubNub. SendGrid repopo.

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## 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
)

Geremy

+2

All Articles