Azure SendGrid connected to create-react-app for UI - node.js

So I have my app created using create-react-app in Azure, but need to add ability to send mail once a Form is submitted. I know this cannot be done in my react app, but being away from backend work for a bit, I was trying to search for a tutorial or something online that could guide me and setting up some sort of node.js with sendgrid so that this UI can now process sending emails with my forms. Can any of you recommend some direction, tutorials, etc... to assist with this as I am not seeing things on the Sendgrid website for this.
Thanks for the help.

Check the library sendgrid-nodejs repo, which includes detailed examples for Node.js:
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'test#example.com',
from: 'test#example.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);
For more complex use cases, please see USE_CASES.md.

Related

How to send mail using node js without nodemailer

I tried to send mail in node js using nodemailer.
Question is: is it possible to use another framework - without nodemailer using send mail. If so - can you provide some example code?
You can use sendgrid to send an email. It is more reliable in my opinion. You will need to create a sendgrid account to get the API key. Then you can do something like this:
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'test#example.com',
from: 'test#example.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);

Email is not receiving in recipient's email box while using sendgrid in protractor

I am using protractor-cucumber framework.I am tryig to implement sendgrid method for email sending.For that i have used api from https://sendgrid.com/free/. I am using sendgrid/mail version 5.2.3 and used below code.
const sgMail = require('#sendgrid/mail');
var env=require('../support/sendgrid.js');
sgMail.setApiKey(env.SENDGRID_API_KEY);
console.log(env.SENDGRID_API_KEY);
const msg = {
to: 'xx#gmail.com',
from: 'xxx#gmail.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);
I am exporting the api from sendgrid.js(i am able to successfully launch the api to the code).When i ran the above code initially, the email not received, then i directly hardcoded the api in the code, then it worked.So i again try to export it from the sendgrid.js file.That time also it worked.But after running the code 2,3 times then again the email was not receiving in the recipient list.I am not understanding why this behave inconsistently.Also i am not able to console whether there is a success or failure.How can we catch/print the error here.Can anyone help me.Thanks in advance

Communication between client and server in Angular 2+

I am using NodeMailer for mail service. I have to get an email address from a field in client side and send that value to app.js where my nodemailer code resides.
client side
ngOnInit() {
this.forgotForm = this.formBuilder.group({
email: this.email,
});
}
sendmail() {
}
app.js, Nodemailer code (I have to get email id for to address here)
let mailOptions = {
from: 'xyz#gmail.com',
to: '',
subject: 'Test Mailer',
text: 'I am testing Nodemailer to send email.',
};
You Should Consider Looking/ Learning Angular and then going into forms . and then into http modules which will help you post data to services
This is a gist not the actual answer
There are a lots of ways to get this done , using normal inputs and getting data from that input using button control or using Forms[the best approach] as you might have other details to send as well.
There are two kind of froms in Angular Template Driven or Reactive Forms.
After getting the details in your form you will need to post it to a Rest service i am guessing. For that you will need to look at Angular Http Client Module
Please look at those links for more detailed Info on them.
You need to use services in angular in order to do this. in terminal set the path to a folder where you want create service, and then use the command ng generate service <service_name> . This will create a file service_name.service.ts. You can refer https://codecraft.tv/courses/angular/http/overview/ or https://angular.io/tutorial/toh-pt4 for more details.
You can use APIs along with http methods get, post, put, delete etc. to complete your task.
In respective component.ts file create a variable email like:
email =string;
In the html file bind the input field with ngModel as:
[(ngModel)]="email"
Then make a function in service.ts that accepts email as arguments
endMail(email) {
//make api call using http method(say post)
// supply email
let url = <APIURL>
return http.post(url, {email: email});
}
Again in component.ts import this service and instantiate in constructor and use a method to call service

View order schema does not appear using nodemailer but google app script

I have been trying to create an Google order Schema markup for our orders. However, the html template that I generate works when I run it using the google App Script Test
Code.gs file in Google App script
function testSchemas() {
var htmlBody = HtmlService.createHtmlOutputFromFile('template_html').getContent();
MailApp.sendEmail({
// to: Session.getActiveUser().getEmail(),
to:'MY GMAIL EMAIL',
subject: 'Test Actions in the Inbox - ' + new Date(),
htmlBody: htmlBody,
});
}
But when I use the same html template in nodemailer and run the nodemailer with the same gmail credentials using the Gmail Service, I do not find the
"View order" section.
I am aware that to: and from: addresses need to be same for testing schemas and I have the same to and from address.
My nodemailer config looks something like:
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "GMAIL EMAIL",
pass: "PASSWORD"
}
});
smtpTransport.sendMail(mailOptions,function(err,response){console.log('Done');});
Google says your email must be SPF/DKIM enabled in order to stop phishing attacks but when I am using the Gmail service, it must not be the case.
Can anybody tell me what might be the reason for this and the workaround of how could I test it using node mailer before I raise a request to whitelist my IpP to Google.
Also, can we make Google orders to appear as Google card in Google Now.

mail for nodemailer didn't work on azure server

I am trying to send mail by using nodemailer's mail api from express js server on microsoft azure. It's getting success in local machine but not on azure server(remote). Any idea?
var mail = require("nodemailer").mail;
mail({from: "from#gmail.com", // sender address
to: "to#gmail.com", // list of receivers
subject: "subject", // Subject line
text: "text", // plaintext body
html: "<b>helloworld</b>" // html body
});
thanks,
av
Just in case someone else meet the same problem.
I had the same problem about running Nodemailer in Azure Website. Finally I found the problem was that I forgot put Nodemailer as a dependency in package.json. Then azure did not know it had to install Nodemailer for the hosted server.
Add "nodemailer" : "*" at the end of dependency list in package.json. Don't forget to add a "," after the last line before you add "nodemailer".
Hope it helps.
Please do changes in iisnode.yml file to call newer nodejs version as follows-
nodeProcessCommandLine: "D:\Program Files (x86)\nodejs\8.5.0\node.exe"

Resources