Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 30082

Error: Connection timeout at SMTPConnection._formatError

$
0
0

Error: Connection timeout at SMTPConnection._formatError, don't know what is wrong, can't send mails,please help me. am trying to send a mail using nodemailer, but i keep getting this error in my console

    Error: Connection timeout
        at SMTPConnection._formatError (/home/codabae/Desktop/mailmonster/Backend/v1/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
        at listOnTimeout (internal/timers.js:531:17)
        at processTimers (internal/timers.js:475:7) {
      code: 'ETIMEDOUT',
      command: 'CONN'
    }

this is my api here i don't know what am doing wrong, i getting the details from the mongodb and am filling it in the nodmailer fields, i really don't know what am doing wrong.

smtpUrl = 'Gmail'
smtpEMail = 'the users gmail...@gmail.com'
smtpPassword = 'the password of the users email'
    const express = require('express');
    const router = express.Router();
    const nodemailer = require("nodemailer");
    const { Mail } = require('../../model/Mail');
    const { Smtp } = require('../../model/Smtp');
    const { auth } = require('../../middleware/auth')

    // Mail
    // @route   GET api/mails/test

    router.get('/', auth, async (req, res) => {
        const mail = await Mail.find().populate('comment', ['comment']);
        res.send(mail)
    })
    router.get('/me', auth, async (req, res) => {
        const mail = await Mail.findOne({ user: req.user._id })
        if (!mail) return 'Mail with id doesnt exist'
        res.send(mail)
    })

    router.post('/', auth, (req, res) => {
        const { to, cc, bcc, subject, message, attachment, smtpDetails } = req.body;

        if (!to || !subject || !message || !smtpDetails) return res.status(400).send('input cannot be empty')

        Smtp.findOne({ smtpEmail: smtpDetails }).then(user => {
            if (user) {
                let smtpUrl = user.smtpUrl.trim()
                let smtpPassword = user.smtpPassword.trim()
                let smtpEmail = user.smtpEmail.trim()

                console.log(smtpEmail, smtpUrl, smtpPassword)

                let transporter = nodemailer.createTransport({
                    service: `${smtpUrl}`,
                    auth: {
                        user: `${smtpEmail}`,
                        pass: `${smtpPassword}`
                    }
                });

                let mailOptions = {
                    from: `${smtpEmail}`,
                    to: to,
                    cc: cc,
                    bcc: bcc,
                    subject: subject,
                    text: `${message}`
                };

                transporter.sendMail(mailOptions, function (error, info) {
                    if (error) {
                        console.log(error);
                    } else {
                        console.log('Email sent: ' + info.response);

                    }
                });

                console.log(to)
                console.log(message)
                console.log(Mail)


                const newMail = new Mail({
                    to,
                    cc,
                    bcc,
                    subject,
                    message,
                    attachment,
                    smtpDetails
                });
                return newMail
                    .save()
                    .then(mail => {
                        res.status(200).send('sent')

                    })
                    .catch(err => console.log(err));


            } else {
               res.send('add account')

            }


        })




    })


    router.delete('/:id', async (req, res) => {
        // const result = await User.deleteOne({ _id: id })
        const mail = await Contact.findByIdAndRemove(req.params.id)
        if (!mail) return res.status(404).send('user with id not found')
        res.send(mail)
    })


    module.exports = router;

Viewing all articles
Browse latest Browse all 30082

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>