How to send Share-point Document using mail attachment from Node Mailer - node.js

I am using Node Mailer to send email from node application. Now this mail contains attachment which is a share point link and looks something like below -
{ // use URL as an attachment
filename: 'license.txt',
path: 'https://raw.github.com/nodemailer/nodemailer/master/LICENSE'
}
The problem is when I send this mail it gives me 403 forbidden error which I understand is due to the share point link needs authentication. My question is how do I pass the authentication and which type of authentication node mailer would accept for fetching the share point document as an attachment.

Related

Having issues sending "base64" email "attachments" using "#sendgrid/mail" in "nodejs"

So, basically I want to send a base64 encoded .zip file as email attachment in #sendgrid/mail. The file is saved in MongoDB like this,
I am fetching the data and converting the "file.buffer" which is a binary data to base64 using .toString("base64") something like this,
console.log('Converting to base64')
plugin.file.buffer = plugin.file.buffer.toString('base64')
and it gets perfectly converted into base64 (I know it is working because I am also using that "plugin.file" in .ejs file as a download button something like this,
<a class="btn btn-success btn-sm" download="plugin.zip" href="data:application/zip;base64,<%- plugin %>" role="button">Download</a>
So, now I will be using that "plugin" to send a email to the user, so I am doing something like this just before rendering the ejs page,
if (req.session.email) await sendMail(req.session.email , "Thanks for purchasing" , "Here is your delivery!" , plugin)
res.render('success' , {payment , type: "Payment" , discord , plugin: plugin.file.buffer})
It basically checks if the email of the user is stored in "sessions" i.e. logged in, if yes it sends a email and then renders the success page!
So, the issue is in the email.js file sendMail() function. It is neither sending me the email nor the attachment.
Before showing the code inside that function(), my api keys etc are correct as I am also sending a mail whenever the user creates an account and in that case, everything is working fine and the mail is being sent. But whenever I include the attachment logic, neither the mail nor the attachment is being sent! So, here is my code:-
const { MAILER_API_KEY } = require('../config.json')
const mailer = require('#sendgrid/mail')
mailer.setApiKey(MAILER_API_KEY)
async function sendMail(email , subject , text , plugin) {
mailOptions = {
to: email,
from: 'zoomdev.code#gmail.com',
subject: subject,
text: text
}
// if this if statement is false i.e. plugin is undefined(whenever the user is signing up, he/she doesnt need attachment or stuff. The attachment email is being sent whenever the payment is completed and the file is being sent to the user) it works fine. But sending an attachment doesn't work. The email and attachment doesn't get sent!
if (plugin) mailOptions.attachments = [{
content: plugin.file.buffer,
filename: plugin.file.originalname,
type: 'application/zip', // Here, I have also tried using "application/x-zip-compressed", the type saved in the database and it is the same! :(
disposition: 'attachment'
}]
await mailer.send(mailOptions).then(res => {
console.log(JSON.stringify(res))
return true
}).catch(err => {
console.log(JSON.stringify(err))
return false
})
console.log('Mail sent to ' + email)
}
module.exports = { sendMail }
Whenever sending the attachment email, I am getting this in console,
Executing Payment
Converting to base64
Updating user
// ^ these are something else. Ignore them
// this is the JSON.stringify(... console.log in sendMail function last lines
[{"statusCode":202,"body":"","headers":{"server":"nginx","date":"Fri, 22 Oct 2021 06:57:18 GMT","content-length":"0","connection":"close","x-message-id":"QpxSudqkRGytDU7YFleVgA","access-control-allow-origin":"https://sendgrid.api-docs.io","access-control-allow-methods":"POST","access-control-allow-headers":"Authorization, Content-Type, On-behalf-of, x-sg-elas-acl","access-control-max-age":"600","x-no-cors-reason":"https://sendgrid.com/docs/Classroom/Basics/API/cors.html","strict-transport-security":"max-age=600; includeSubDomains"}},""]
Mail sent to <my email , I have edited this part for privacy, but it correctly logs the email>
Note:-
I am using express#^4.17.1
#sendgrid/mail#^7.4.7
I have checked spam folder and everything
Twilio SendGrid developer evangelist here.
Using a Gmail address as your from address is not a good way to get your emails delivered. Doing so means you are spoofing an email address, even if it is your email address.
When you are spoofing an email address like that, the email server is unable to prove that it has permission to send emails for that address. SPF and DKIM are methods you can use to prove that you control the domain the email is coming from, but when you use a Gmail address (or any other mailbox provider) then you will appear to be spoofing and receiving mailboxes may choose to discard your emails or land them in the spam inbox.
You may have managed to send and receive your account creation emails with this method, but mailbox providers will be a lot more restrictive when it comes to receiving emails with attached zip files. The whole thing appears suspicious and the mailbox will likely reject it, send it spam, or silently delete it.
The advice here is just don't send emails from a domain that you don't control. What you should do is get a domain that you want to send emails from, set up domain authentication for that domain and you will have a much better chance of landing your emails in your users' inboxes than by spoofing Gmail.

How to send an email with excel file as attachment in angular-node app

I am working on angular (frontend) and node ( backend). I want to send an email using sendgrid , With excel file as attachment. I followed the app as reference demo app .Instead of downloading the excel, I want the same file to be sent as email attachment.
Note : In the backend I use nodejs, I am not sure how to pass the file with API to node.js, I can send email in nodejs code.
Can someone help me on this?
You can make an http post request in the node api back-end and pass the file. Do post using headers as Multipart form request.
Multipart form request helps you to make the node js api pass the excel sheet generated on the front end.
In your email service add the http service call to the backend. And make the service pass the file in it
Check this link out for more details
https://blog.jscrambler.com/implementing-file-upload-using-node-and-angular/

How can I return server side error to the client's HTML page in Express/node js?

I am creating an web app I which the user requests to create an account. The request from client's page will reach the server and query the database. Now what is the proper way of displaying User already exists error in my registeration page. In short, how can I send server side errors to my clients in Node JS?
There are some options to get what you need. The approach that I would not follow will be using the http protocol and sending a 500 response for example.
One good approach would be creating a status and a message fields in your response.
You could use code 200 for the sucessful requests and a different code for the rest. You could have a dedicated code to inform the api user that the backend coud not insert the data.
Example response:
{
code: 789
message: "User could not be inserted"
...
}

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

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