We are using NodeJS to publish messages. Is it possible for subscribers to receive the messages by email?
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)
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
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();
});
};
Related
I want to send otp via SMS and email to the user who is signing-up to the web app to verify their email and phone no.
Is there any NPM package for sending and verifying OTP or should I write my own code?
Yes, there is a package called sendotp. This is used for sending OTP over SMS only.
In npmjs you can find different examples to achieve your task.
But my suggestion is to implement your own methodology for sending and verifying OTP is better. Because by using packages you might get extra methods that you don't want at all or there is no functionality implemented that you want.
You can use Nodemailer for email verification and twiolio for sms verification. Create a random string and send it to user via sms or email and if using mongodb you can check when otp is created and from that you can check how much time is passed after its sent but by that you can set expirey.
You can use the msg91 for sending the otp to users.
Login to msg91 and get your template_id and api_key.
using msg91 api with the axios.
const axios = require('axios');
async function sendOtpTOUser(phone) {
const template = "template _id";
const apiKey = "api_key";
const sendotp = "https://api.msg91.com/api/v5/otp?template_id="+template+"&mobile="+phone+"&authkey="+apiKey;
let request_options1 = {
method: 'get',
url: sendotp
};
let otpResponse = await axios(request_options1);
console.log(otpResponse.data)
return otpResponse.data;
}
it will return the object as
{ request_id: '3166686e7867313634383535', type: 'success' }
I have a firebase cloud function trigger an send a Welcome email when someone signs up. I would like to include my email verification link in that same email to reduce the amount of emails users get upon signup and improve the onboarding experience (rather than sending two separate emails).
exports.sendWelcomeEmail = functions.auth.user().onCreate(event => {
// Get user that signed up
const user = event.data; // The Firebase user.
// get the email of the user that signed up
const email = user.email; // The email of the user.
// Create email verification link
var emailVerificationLink = user.createEmailVerificationLink() // NEED HELP HERE: ideally, I would like to create/call a function to create an email verification link for the user here
// send email
mailgun.messages().send({
from: 'support#example.com',
to: email,
subject: 'Welcome & Get Started',
text: 'Welcome! Here are some resources to help you get started, but first verify your email: ' + emailVerificationLink + '!',
html: // some nice formatted version of the text above
}, function (error, response) {
console.log("Email response");
console.log(response);
console.log("Email error");
console.log(error);
});
})
I have carefully looked through the documentation on custom email handlers, but it doesn't seem like they return the email verification link, so I do not see how to use that approach for my purposes here (although I hope I'm wrong).
Is there a way to create the email verification link inside a Firebase Cloud Function in such a way that I could then use resulting link as I please (like in my Welcome email)?
There is no public API to get the OOB verification code, or the link that contains that code.
But you can implement this yourself with a few steps:
Generate your own verification code, that you store somewhere securely (e.g. in a protected section of your Firebase Database).
Embed that code in your message in a link.
Create a Cloud Function at that link.
Handle the request, check the verification code in the database
Set emailVerified to true.
This isn't all that much different from what Firebase Authentication does when you call sendEmailVerification().
For some reasons I can't seem to receive an email from my suitescript.
Here's my code.
define(["N/email"], function(email) {
function afterSubmit(scriptContext) {
var newOrder = scriptContext.newRecord.id;
email.send({
author: 17874,
recipients: "sam172#gmail.com",
subject: "mail from netsuite",
body: "test body email order id: " + newOrder
});
}
return {
afterSubmit: afterSubmit
};
});
This email will send when a sales order is created.
My order is send through Webservice API and every time an order is send to Netsuite, it will trigger this script to send me an email when the sales order get created.
I've been testing by place multiple order, but never receive any email. There's no error on the netsuite server error log.
What could be the problem here? thanks
You are in a Sandbox Account, Sandbox emails can only be sent to the user testing the script.
I have a use case where an HTML form is filled with user data and an email is sent to their email address, CC'ed to a logic app.
The logic app would receive this email and read only the values after name: and email form fields so that I can pass them along to another function.
How would one do this in a logic apps or within an Azure function?
This blog has great information on using Azure Functions from Logic Apps.
Assuming you have the logic app set up to receive emails, you then add a step to process emails in an Azure Function App sending the email content as an input.
Sample Input payload to nodejs webhook trigger:
{
"email": {
"emailBody": "Body×",
"text": "Hello from Logic Apps"
}
}
Note: "Bodyx" is the dynamic content representing the email body that was received in an earlier step.
Corresponding index.js in the function app:
module.exports = function (context, data) {
var email = data.email;
// You can now do processing on the emailBody
context.log('email body', email.emailBody);
context.res = {
body: {
greeting: 'Hello !' + email.text
}
};
context.done();
};
Hope this helps!
I am using node.js 's email module nodemail and sendgrid API to send the email to the users.
Here is my code for sending a email
SOURCE_EMAIL = ?????;
var options = {
auth: {
api_user: sendgrid.api,
api_key: sendgrid.key
}
};
transport = nodemailer.createTransport(sgTransport(options));
transport.sendMail({from: SOURCE_EMAIL, to: email, subject: subject, html: html}, function(err,data){
});
my question is when I use the sendgrid API to send email, what SOURCE_MAIL i should configure to ?
You may set SOURCE_EMAIL to anything you want.
It's best to set it to an email you own, control, and can receive email from.
However, it's entirely up to you.