通过nodemailer nodejs发送邮件

Send email through nodemailer nodejs

首先,我检查数据库中存在的电子邮件,如果存在,它将生成哈希码,然后更新数据库,一旦生成哈希码,就会将电子邮件发送给用户。所以我不知道如何发送电子邮件请任何帮助。

    const md5 = require('md5');
    let transporter = require("../config/transporter");
    
    export const newemail = async(req,res ) => {
    try{
        var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        const user = await db.findOne('USER', { email: req.body.email });
        res.status(200).json(user);
    
        if (!re.test(req.body.email)) {
            res.json('Email address is not valid!');
        } else {
            const email = req.body.email;
            const name = email.substring(0, email.lastIndexOf("@"));
            const domain = email.substring(email.lastIndexOf("@") +1);
            const hashCode = md5(name + domain);
            function sendingMail(referredBy){
          
            const user = await db.findOneandUpdate('USER', { email: req.body.email, referralCode: hashCode, referredBy: referredBy }); 
          const email = user.email;
          console.log(email + "email");
               //send verification mail

               let mailOptions = {
                from: 'xxyyzz@gmail.com', // sender address
                to: email, // list of receivers
                subject: settingsConfig.subjectLine, // Subject line
                text: settingsConfig.message, // plaintext body
                html: settingConfig.message // html body
            };
            console.log("MAILING!");
        console.log(mailOptions)
        // send mail with defined transport object
        transporter.sendMail(mailOptions, function (error, info) {
            if (error) {
                return console.log(error);
            }
            console.log('Message sent: ' + info.response);
        });
         };

    }
}
       catch (err) {
        console.log(err)
        res.status(500).json({ message: "Something went wrong" });
    }

}

请大家如何进一步。

const md5 = require('md5');
let transporter = require("../config/transporter");


export const sendEmail = async (to, from, subject, body) => {
    // use any mailer library to send the email
}

export const isValidEmail = async (email) => {
    const regex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return regex.test(email);
}

export const findUserByEmail = async (email) => {
    // db instance
    return await db.findOne('USER', { email });
}

export const generateHash = async (email) => {
    const name = email.substring(0, email.lastIndexOf("@"));
    const domain = email.substring(email.lastIndexOf("@") +1);
    return md5(name + domain);
}



export const verifyAndEmail = async(req,res ) => {
try{
    // var re = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    // const user = await db.findOne('USER', { email: req.body.email });
    // res.status(200).json(user);
    // step-1 destructure email from request body
    const {
        email
    } = req.body;

    // step-2, validate the email id
    const isEmailValid = await isValidEmail(email);

    if(!isEmailValid) {
        throw new Error('Invalid email');
        // return appropiate response
    }

    // step-3 find a user by email
    const user = await findUserByEmail(email);

    // step-4 validate, if user does not exists
    if (!user) {
        throw new Error('User does not exist');
        // return appropiate response
    }

    //step-5 generate the hash using user's email
    const hash = await generateHash(email);

    // step-6, call send email method with the parameters
    }
       catch (err) {
        console.log(err)
        res.status(500).json({ message: "Something went wrong" });
    }

}
I would suggest, you should refactor your code to following. 

Run it and let me know if you face any issues. 


This approach will help you to debug the error.