cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

"Error: connect ECONNREFUSED" when sending Office 365 mail via Node.JS script

I am trying to set up a webrouter on my GoDaddy cPanel hosting platform to automatically email all employee accounts when there is a form contact. I have shared hosting. I use Office 365 email addresses, through GoDaddy.

 

The relevant portion of the script is in bold red:

 

 

 
const http = require('http')
const fs = require('fs')
const nodemailer = require('nodemailer')
const parser = require('express-formidable')
const express = require('express')
const path = require('path')
const util = require('util')
const CloudmersiveValidateApiClient = require('./node_modules/cloudmersive-validate-api-client/')
const CloudmersiveVirusApiClient = require('./node_modules/cloudmersive-virus-api-client/')
const cred = require('../mailerconfig')


const cmDefaultClient = CloudmersiveValidateApiClient.ApiClient.instance

// Configure API key authorization: Apikey
const cmApikey = cmDefaultClient.authentications['Apikey']
cmApikey.apiKey = cred.apiKey
const cmAPI = new CloudmersiveValidateApiClient.DomainApi()
const cmDomain = ""
const cmCallBack = (error, data, response) => {
	if (error) {
		return console.log('An error occurred at virus scan: ' + error) 
	} else {
			
	console.log('API called successfully. Returned data ' + data)
	console.log('The response is: ' + response)
	}
};

const avDefaultClient = CloudmersiveVirusApiClient.ApiClient.instance
const avApikey = avDefaultClient.authentications['Apikey']
avApikey.apiKey = cred.apiKey
const avAPI = new CloudmersiveVirusApiClient.ScanApi()
const avCallBack = (error, data, response) => {
	if (error) {
		console.log('Virus scan failed.\n\n' + error)
	} else {
		console.log('Virus scan successful. See: \n\n' + data + '\n\n' + response)
	}
}

const app = express()
app.use(parser({
	encoding: 'utf8',
	uploadDir: './uploads/',
	multiples: true,
	hash: "sha1"
}))

app.set('view engine', 'ejs')
app.set('env', 'production')

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/home', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/index', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/about', (req, res) => {
	res.sendFile(__dirname + '/about.html')
})

app.get('/advocacy', (req, res) => {
	res.sendFile(__dirname + '/advocacy.html')
})

app.get('/news', (req, res) => {
	res.sendFile(__dirname + '/news.html')
})

app.get('/updates', (req, res) => {
	res.sendFile(__dirname + '/news.html')
})

app.get('/ongoings', (req, res) => {
	res.sendFile(__dirname + '/news.html')
})

app.get('/contact', (req, res) => {
	console.log(req.url)
	res.sendFile(__dirname + '/contact.html')
})

app.post('/send', (req, res) => {
	
	req.fields
	req.files

	if (!req.fields && !req.files) {
		res.sendFile(__dirname + '/contact-fail.html')
	} else {

		console.log(req.fields)
		req.fields
		req.files
		/*avAPI.scanFile('*', avCallBack())
		cmAPI.domainCheck(cmDomain, cmCallBack())*/
		let data = {
			senderName: req.fields.name,
			senderEmail: req.fields.email,
			senderAddress: req.fields.address,
			senderCity: req.fields.city,
			senderState: req.fields.state,
			senderZip: req.fields.zipcode,
			senderTel: req.fields.telephone,
			senderSubject: req.fields.subject,
			senderComments: req.fields.comments
		}

		let files = {
			senderFiles: req.files
		}
				
		let TextBody = {
		senderMessageBody: `The ...`
		}
			
		let HTMLbody = {
			senderMessageBody: `<h2>The .... </h2>`
		}
	
		let transporter = nodemailer.createTransport({
			host: 'smtp-mail.outlook.com',
			port: 587,
			secureConnection: false,
			auth: {
				user: 'myEmail@myDomain.info',
				pass: 'PASSWORD'
			},
			tls: {
				ciphers: 'SSLv3'
			}
		})
	
		let mailOptions = {
			from: '[redacted]', // sender address
			to: '[redacted]', // list of receivers
			replyTo: data.senderEmail,
			bcc: [redacted list]
			subject: 'Webform Submit'
			text: TextBody.senderMessageBody, // plain text body 
			html: HTMLbody.senderMessageBody // plain HTML body 
		}
		
		transporter.sendMail(mailOptions, (error, info) => {
			if (error) {
				console.log(`An error has occurred. \n\n${error}`)
				res.sendFile(__dirname + '/contact-fail.html')
			} else {
				console.log(`Message sent. \n\n${info}`)
				res.sendFile(__dirname + '/contact-success.html')
			}
		})
	}
})
app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})

app.get('/', (req, res) => {
	res.sendFile(__dirname + '/index.html')
})


app.listen(3000, (error, info) => {
	if (error) {
		console.log(`Server failure. \n\n${error}` )
	}
	else {
		console.log(`Listening on port 3000. \n\n${info}` )
	}
})
 

The server script works perfectly when run on my personal computer using localhost:3000.  I can upload the script to my cPanel file manager and run it via ssh connection and everything works fine, including the routing. However, once I hit submit on a completed contact form, I get:

 

"Error: connect ECONNREFUSED".

Port 3000, the port on which the server script listens, is not a reserved port for cPanel or GoDaddy. Nonetheless, I get the error no matter what port I set the server script to listen on. Port 587 is the secure smtp port for email; that is correct. While not correctly displayed here, both my password and usernames are correct in my actual script. 

I have performed Google search after Google search. The common suggestion when it comes to this type of error is that there is a firewall blocking connections to the port. But it makes no sense that GoDaddy when be blocking the smtp port for the Office 365 email addresses it sells for $9/user/month. 

How do I connect to the Office 365 server to send mail from Node.JS scripts using Express?

5 REPLIES 5
Community Manager
Community Manager

Re: "Error: connect ECONNREFUSED" when sending Office 365 mail via Node.JS script

Hi @milkandgin. Thanks for being part of GoDaddy Community! Our hosting platforms are fairly locked down when it comes to scripts sending email. For a shared hosting plan, you'll only be able to send email via relay-hosting.secureserver.net, using port 25. As far as I know, there isn't a way around this. Sorry Man Sad

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.

Re: "Error: connect ECONNREFUSED" when sending Office 365 mail via Node.JS script

I have a VPS and have transferred all the files from shared hosting to the VPS. I can't modify the zone files, either from cPanel in the VPS or on the shared hosting site, because the zone files are set to clientDeleteProhibited. Accordingly, I can't modify them either, so the IP address for the site, http://www.tottm.org, points to the VPS rather than shared hosting site.

Community Manager
Community Manager

Re: "Error: connect ECONNREFUSED" when sending Office 365 mail via Node.JS script

@milkandgin - I see. Actually, VPS and Dedicated Server are also required to use a GoDaddy SMTP server. If there are ways to get around this limitation, it wouldn't be supported by our support teams. Though other community members are free to chime in. Additionally, unless the connection was set up to mimic an email client, I don't think you would be able to relay via an Office 365 account either. (My apologies if this is what you have in the script above. I'm not a developer and wouldn't be able to recognize that.) 

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.

Re: "Error: connect ECONNREFUSED" when sending Office 365 mail via Node.JS script

Then can you ask someone how to go about handling POST events to perform functions like processing a 'contact us' form using JavaScript rather than the PHP engine?

Community Manager
Community Manager

Re: "Error: connect ECONNREFUSED" when sending Office 365 mail via Node.JS script

@milkandgin - Assistance with composing or debugging a script would be outside the scope of support for our teams. Other community members may be able to chime in. You may also consider posting a similar request on the VPS and Dedicated Servers board.

 

JesseW - GoDaddy | Community Manager | 24/7 support available at x.co/247support | Remember to choose a solution and give kudos.