0

I am working with angular,Mongodb,Nodejs,express. simply a Mean stack.

my website is related to shopping where the carts loads dynamically. the products added to cart should be sent as mail to owner.

can we send directly mail? is yes please explain the process how. if not should be stored in database then how?

James Z
  • 12,209
  • 10
  • 24
  • 44
ramya
  • 1
  • 2

1 Answers1

0

So, the question is very unclear here. But I'll try my best to explain all the situations. So that, you don't have to mess with it a lot. I'll explain how you can send a mail to the user using front-end only or using your server.

To send an email to a user, we need to do it on the backend. That means you'll have to write some code in your server for sending the email to your client. As you have a MEAN Stack application, you must be familiar with Node package manager.

npm has a package called nodemailer that can be used for sending emails to the users. But make sure you write those emails in a good manner so that they don't end up in the spam box.

You can install nodemailer using

npm install nodemailer

Here's a sample code from w3schools

var nodemailer = require('nodemailer');

    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'youremail@gmail.com',
        pass: 'yourpassword'
      }
    });
    
    var mailOptions = {
      from: 'youremail@gmail.com',
      to: 'myfriend@yahoo.com',
      subject: 'Sending Email using Node.js',
      text: 'That was easy!'
    };
    
    transporter.sendMail(mailOptions, function(error, info){
      if (error) {
        console.log(error);
      } else {
        console.log('Email sent: ' + info.response);
      }
    });

You may also find other and maybe even better alternatives with a simple google search.

Also, i don't think there's any point in storing this data in the database unless you need it for future use. I don't know the flow of your ecommerce application. So, I'll leave this decision for you to take.

And if you want to send the e-mail directly from the front-end, there's a workaround. Sending emails requires a server, but you can use someone's else backend server for this. Basically a BaaS(Backend as a Service). You can check this previously answered question on stackoverflow for more information regarding sending emails directly from the frontend of an application.

How can i send emails without a server ? Only front-end Javascript

Daman Arora
  • 1,959
  • 1
  • 7
  • 7
  • I want to send a dynamic loading html file...is it possible to send ? How please suggest – ramya Jun 10 '21 at 16:51
  • Well the best way to know is to try it. And I think you can do that pretty easily. Just inject the HTML in your email's body. Here's a tutorial that might help you. https://blog.stvmlbrn.com/2018/09/06/send-html-email-in-node.html – Daman Arora Jun 15 '21 at 17:18
  • thankyou i will try let u know.code is in angular.does we need to create all html wiith template engines or only one we can create with .ejs/.handlers extension? – ramya Jun 18 '21 at 01:56
  • If you want to inject some variables from backend into the html, using a template engine would be better. – Daman Arora Jun 21 '21 at 20:01
  • Thankyou Daman for your guidance.i made it , now data is coming to database but still I couldn't complete my nodemailer part – ramya Aug 14 '21 at 05:59
  • If you want to send email from backend, that's mentioned in the answer above. If you want to send it from front-end itself, try https://stackoverflow.com/questions/32041229/how-can-i-send-emails-without-a-server-only-front-end-javascript-with-sendgrid – Daman Arora Sep 14 '21 at 19:00