1

I am running VSCode, Nodejs, Nodemailer, and Reactjs in a Windows machine, but I cannot get Nodemailer to send email. According to the instructions in the web, it should work. Finally I did the following: I created two empty folders in both of which I ran node init, installed Nodemailer, and copied the email sending code. In the other folder I also ran create-react-app. Then I edited the files just enough to get the sending code running. In the first folder it works without problems, but in the folder with React, it does not do anything. Not even the usual following if(error)/else(success) statements get executed, they are just jumped over. However, the code before and after the transporter.sendMail (or .verify) part are executed... Anyone know why this happens or how to fix it?

This is the code I run in both cra and the non-cra folders:

const nodemailer = require("nodemailer");

const SendEmail = message => {
    const transporter = nodemailer.createTransport({
        service: "Gmail",
        auth: {
            user: "from@gmail.com",
            pass: "xxxxxxxx"
        }
    });
    transporter.verify(function(error) {
        if (error) {
            console.log(error);
        } else {
            console.log("Server is ready to take our messages");
        }
    });
    const mailOptions = {
        from: "from@gmail.com",
        to: "to@gmail.com",
        subject: "Subject",
        text: message,
        html: "<b>Html</b>"
      };

    transporter.sendMail(mailOptions, (err, info) => {
        if (err) console.log(err);
        else console.log(info.response);
    });
};

module.exports = SendEmail;

Tim

tim
  • 41
  • 4

1 Answers1

0

Gmail has spam filter to prevent spam, so most probably, you may get it pass through sometime and not most time without proper configuration.

and it is not a good idea to send your email in your client app, such as react. Since everyone can access to your email and password to do nasty thing, which is not really a good idea.

Best practice is to request your node server to send mail.

Other than, I noticed that you used gmail to do that. There are some free mail fake stmp server that you can do spamming without the mail provider to flag you as spam user. Such as mailTrap, if you are just interested to test, is react able to send email, try it with mailtrap. I never do it, but still it is better than using your own email provider, as they might have filter rules about it, could be the reason, you are not able to send it.

cYee
  • 1,915
  • 1
  • 16
  • 24
  • Using for example my own ISP's servers, the same thing happens, Nodemailer without React works, with it does not. – tim Apr 01 '20 at 06:41
  • May I know why do you want to use React to send mail? – cYee Apr 01 '20 at 06:46
  • As of it is not practical to send mail through client app, where user can hack into your client app and your mailbox is doomed. – cYee Apr 01 '20 at 06:48
  • Try check this out. https://stackoverflow.com/questions/26196467/sending-email-via-node-js-using-nodemailer-is-not-working – cYee Apr 01 '20 at 06:51
  • At this point I am just testing and I have also read that it is not safe to use Nodemailer in the Internet. (However, I might use it in LAN where I know that the users pose no threat to the mailbox.) But... it baffles me why Nodemailer stops working after React has been installed. I cannot see the connection. – tim Apr 01 '20 at 10:44
  • Yeah, it is not safe to use nodemailer with reactjs. I think if you are purely local there is other thing u can use instead of nodemailer. I am just curious what is your own ISP? – cYee Apr 01 '20 at 10:53
  • Actually, I can use my ISP's mail servers without authentication, so, Nodemailer would have been fine. But since it does not work, I'm now running the code under Linux using PHP to send email. – tim Apr 29 '20 at 08:03
  • Yes, it did. But after build, a bunch of new problems appeared... – tim May 03 '20 at 05:21