Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Send Email + Publish
- publish( 'my_channel', { 'some' : 'data' } )
- ## Done!
- ## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
- ## 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)
- 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
- )
Add Comment
Please, Sign In to add comment