Reputation: 765
i'm trying to use the emailjs (https://github.com/eleith/emailjs) for send emails with nodejs, but, it gives me an error: { [Error: timedout while connecting to smtp server] code: 4, smtp: undefined }
I'm trying to use hotmail, but I can use other, I only want this working. Any idea please?
var email = require("emailjs");
var server = email.server.connect({
user: "[email protected]",
password:"xxxyyyy",
host: "smtp-mail.live.com",
ssl: true
});
// send the message and get a callback with an error or details of the message that was sent
server.send({
text: "i hope this works",
from: "you <[email protected]>",
to: "someone <[email protected]>",
//cc: "else <[email protected]>",
subject: "testing emailjs"
}, function(err, message) { console.log(err || message); });
Upvotes: 5
Views: 20400
Reputation: 1
I think you can use this version
, For me, this is working in version 2.2.0 where version 3.0 and above will show the error.
npm i [email protected]
Upvotes: 0
Reputation: 798
make sure you have installed exact package with same version
npm i [email protected]
and try below code
var email = require("emailjs");
var server = email.server.connect({
user: "[email protected]",
password: "your-password",
host: "smtp.gmail.com",
ssl: true
});
server.send(
{
text: "Hey howdy",
from: "NodeJS",
to: "Wilson <[email protected]>",
cc: "",
subject: "Greetings"
},
function(err, message) {
console.log(err || message);
}
);
seems latest version is not working its using es6 exports "[email protected]" but version still works fine . also don't forget to enable less secure setting for email address you are using with password .
Upvotes: 0
Reputation: 4817
Using Gmail
First: Required activate the use less secure apps at your own risk as indicated in the Gmail documentation. I use an account not very important for my tests:
https://myaccount.google.com/u/2/lesssecureapps?pageId=none
After:
var email = require("emailjs");
var server = email.server.connect({
user: "[email protected]",
password:"YOUR_PASSWORD",
host: "smtp.gmail.com",
ssl: true
});
server.send({
text: "Hello world",
from: "YOUR_NAME <[email protected]>",
to: "FRIEND_NAME <[email protected]>",
subject: "Hello"
}, function(err, message) { console.log(err || message); });
Additional Note: About how set "from" and displayed name in Gmail inbox:
Example 1:
Set "from" with only name.
from: "YOUR_NAME", // Only name
In inbox display <>
YOUR_NAME <> Hello 18:11
Example 2:
Set "from" with name and emial.
from: "YOUR_NAME <[email protected]>", // Name and email
In inbox not display <>
YOUR_NAME Hello 18:11
Upvotes: 2
Reputation: 79
Make sure you are linking the emailjs files correctly (check var email) or it won't work.
//Email stuff
var email = require("./node_modules/emailjs/email");
var server = email.server.connect({
user: "[email protected]",
password:"INSERTPASSWORDHERE",
host: "smtp.gmail.com",
ssl: true
});
//If button is clicked then send email
server.send({
text: 'Hello World! \n\n Regards,\n INSERTNAMEHERE \n',
from: 'Name',
to: 'Name <[email protected]>',
cc: '',
subject: ''
}, function (err, message) {
console.log(err || message);
});
Upvotes: 1
Reputation: 9136
I tested with the code below (using gmail) and it is working:
var email = require('emailjs');
var server = email.server.connect({
user: '[email protected]',
password: 'stackoverflow',
host: 'smtp.gmail.com',
ssl: true
});
server.send({
text: 'Hey howdy',
from: 'NodeJS',
to: 'Wilson <[email protected]>',
cc: '',
subject: 'Greetings'
}, function (err, message) {
console.log(err || message);
});
In my console the output is:
{
attachments: [],
alternative: null,
header: {
'message-id': '<[email protected]>',
date: 'Tue, 02 Jun 2015 10:48:58 -0400',
from: '=?UTF-8?Q?NodeJS?= <>',
to: '=?UTF-8?Q?Wilson?= <[email protected]>',
cc: '',
subject: '=?UTF-8?Q?Greetings?='
},
content: 'text/plain; charset=utf-8',
text: 'Hey howdy'
}
Indeed I have received in my inbox the email message.
I suspect that you should use the host smtp.live.com instead of smtp-mail.live.com
Note: The account ([email protected]) used is a valid one I just created for testing purposes.
Upvotes: 4