0
votes

I've created a shopping React app and I want to send the user an approval mail after he clicked the purchase button. I've researched but couldn't find the way to do so. I've tried nodemailer bu it only let's you send an email to yourself (meaning a pre-specified address in the code)

Anyone knows a package/method to do it?

Edit : This is the answer using nodemailer -

IMPORTANT - if you want to use Gmail as the sender account, you need to enable use of less safe applications in your Gmail account via this link : https://myaccount.google.com/lesssecureapps

        const transporter = nodemailer.createTransport({
        service : 'gmail',
        auth: {
            user: 'YOUR_MAIL_ADDRESS',
            pass: 'YOUR_MAIL_PASSWORD'
        },
        tls:{
            rejectUnauthorized:false
          }
    });

    let mailOptions = {
        to: "RECIPIENT_MAIL_ADDRESS",
        subject: 'Node Contact Request', // Subject line
        text: 'Hello world?', // plain text body
        html: htmlMail // html body
    };


    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message sent: %s', info.messageId);   

    });
1
You can change to parameter in sendMail method and send email to anyone.Nenad Vracar
Thank you but I can't get it to work. I've edited my main message with the code i'm using, can you take a look? trying to send to a gmail account by the way if it means anything.falcon25
What error are you getting?Nenad Vracar
I found the solution eventually - edited my main message with it for others. Turns out the Ethereal test account won't send actual messages, so I needed to use an actual mail account to do it. Thank youfalcon25
Ok, you can also check out mailgun.com, they have free limited option.Nenad Vracar

1 Answers

1
votes

Using Nodemailer

let useEmail ="[email protected]"
let info = await transporter.sendMail({
    from: '"Fred Foo 👻" <[email protected]>', // sender address
    to: userEmail, // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world?", // plain text body
    html: "<b>Hello world?</b>" // html body
  });

Here, "to" is just a string.

You can take user's email, you would have stored it somewhere in your code(Redux or AsyncStorage or would have taken user Input) and use it in the to field.