0

I am trying to send email with javascript nodemailer package but returning undefined.

Checked following options:

Let me know what is missing ?

I have followed all possible noted on this website but could not help me.

Code

async sendEmailWithAttachments(
        fromemailid,
        toemailid,
        fileName,
        attachmentsPath
    ) {
        let transporter = nodemailer1.createTransport({
            // service: "gmail",
            host: "smtp.gmail.com",
            port: 587,
            secure: false,
            debug: true,
            auth: {
                user: "XXX@gmail.com", 
                pass: "XXXX", 
            },
             tls: {
                rejectUnauthorized: false,
             },
            logger: true,
        });

        let mailOptions = {
            from: "XXX@gmail.com",
            to: "XXX@gmail.com", 
            subject: "Hello ✔", // Subject line
            text: "Hello world?", // plain text body
            // attachments: [
            //  {
            //      // utf-8 string as an attachment
            //      filename: fileName,
            //      path: attachmentsPath,
            //  },
            // ],
        };

        // send mail with defined transport object
        let info = transporter.sendMail(mailOptions, (error, success) => {
            if (error) {
                console.log(error);
            }
        });
        console.log("================= Mail sent ===============", info);
    }

Here info object shows undefinded

Here Console log:

[0-0] [2022-05-31 05:52:03] DEBUG Sending mail using SMTP/6.7.5[client:6.7.5]
[0-0] ================= Mail sent =============== **undefined**
[0-0] 2022-05-31T05:52:03.787Z DEBUG @wdio/utils:shim: Finished to run "afterTest" hook in 0ms
[0-0] 2022-05-31T05:52:03.796Z INFO webdriver: COMMAND deleteSession()
[0-0] 2022-05-31T05:52:03.796Z INFO webdriver: [DELETE] http://localhost:9515/session/cfa202e5063720bed6ff7fa47b29f96d
[0-0] [2022-05-31 05:52:03] DEBUG [5nz3hZn0uA] Resolved smtp.gmail.com as XX.XXX.XXX.XXX [cache miss]
[0-0] [2022-05-31 05:52:03] INFO  [5nz3hZn0uA] Connection established to XX.XXX.XXX.XXX:587```



user19089852
  • 143
  • 1
  • 7
  • https://nodemailer.com/usage/ as per the documentation it says if you do not use callback then its return promise. Maybe try removing the callback and see. Is the issue only with the info or send mail too? can you see the email being delivered to the address? – Rajani B May 31 '22 at 09:57
  • Followed googleapi and nodemailer together and the issue was solved. refer github link: https://github.com/trulymittal/gmail-api – user19089852 Jun 01 '22 at 04:01

0 Answers0