Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMTP connection with node

I'm trying to connect my client to a gmail smtp server without using specific node-smtp libs ( i want to understand how sockets and smtp both work ), but after setting a connection to a server i get no response whatever i write to a socket. This is a part of my code:

var options = {
    host: 'smtp.gmail.com',
    username: '[email protected]',
    password: 'mypass',
    port: 25
    },
    net = require('net');

exports.addEmail = function(req, res) {

    var client = net.connect(options.port, options.host, function() {
        console.log('CONNECTED TO: ' + options.host + ':' + options.port);
        //i can write to a socket anything, still no response
        client.write('HELO smtp.gmail.com');
    });

    client.on('data', function(data) {
        console.log('DATA: ' + data);
    });

    client.on('error', function(err) { console.log(err );})

    client.on('close', function() {
        console.log('Connection closed');
    });
}

What i get in console is: CONNECTED TO: smtp.gmail.com:25 DATA: 220 mx.google.com ESMTP n7sm5406410lae.47 - gsmtp

That's all - no errors, no 5xx or 4xx responses, only 220 after establishing a connection and i don't understand why it happesns I will highly appreciate your help.

like image 886
Andrei Hrabouski Avatar asked Oct 06 '14 07:10

Andrei Hrabouski


1 Answers

As a reference for future readers, you need to add a carriage return (\r) and newline (\n) character at the end of your commands:

client.write('HELO smtp.gmail.com\r\n');
like image 190
quentinadam Avatar answered Sep 22 '22 07:09

quentinadam