Guest User

Untitled

a guest
Jul 18th, 2018
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. ## Send Email + Publish
  2. publish( 'my_channel', { 'some' : 'data' } )
  3. ## Done!
  4.  
  5. ## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  6. ## Send Email and Publish Message on PubNub
  7. ## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  8. import Pubnub ## pip install Pubnub
  9. import sendgrid ## pip install sendgrid
  10.  
  11. def publish( channel, message ):
  12. # Email List
  13. recipients = [
  14. [ "john.smith@gmail.com", "John Smith" ],
  15. [ "jenn.flany@gmail.com", "Jenn Flany" ]
  16. ]
  17.  
  18. # Info Callback
  19. def pubinfo(info): print(info)
  20.  
  21. # Connection to SendGrid
  22. emailer = sendgrid.SendGridClient( 'user', 'pass', secure=True )
  23. pubnub = Pubnub( publish_key="demo", subscribe_key="demo", ssl_on=True )
  24.  
  25. # PubNub Publish
  26. pubnub.publish( channel, message, callback=pubinfo, error=pubinfo )
  27.  
  28. # Email Message Payload
  29. email = sendgrid.Mail()
  30. email.set_from("PubNub <pubsub@pubnub.com>")
  31. email.set_subject("PubNub Message")
  32. email.set_html(json.dumps(message))
  33. email.set_text(json.dumps(message))
  34.  
  35. ## Add Email Recipients
  36. for recipient in recipients:
  37. email.add_to("%s <%s>" % (recipient[1], recipient[0]))
  38.  
  39. ## Send Email
  40. emailer.send(email)
  41.  
  42. require 'net/smtp'
  43. require 'pubnub'
  44.  
  45. def SMTPForward(message_text)
  46.  
  47. # build the headers
  48.  
  49. email = "From: Your Name <your@mail.address>
  50. To: Destination Address <someone@example.com>
  51. Subject: test message
  52. Date: Sat, 23 Jun 2001 16:26:43 +0900
  53. Message-Id: <unique.message.id.string@example.com>
  54.  
  55. " + message_text # add the PN message text to the email body
  56.  
  57. Net::SMTP.start('your.smtp.server', 25) do |smtp| # Send it!
  58. smtp.send_message email,
  59. 'your@mail.address',
  60. 'his_address@example.com'
  61. end
  62.  
  63. @my_callback = lambda { |envelope| SMTPForward(envelope.msg) } # Fwd to email
  64.  
  65. pubnub.subscribe( # Subscribe on channel hello_world, fwd messages to my_callback
  66. :channel => :hello_world,
  67. :callback => @my_callback
  68. )
Add Comment
Please, Sign In to add comment