Session.send() does not work: "session is not defined" - javascript

I am trying to use session.send instead of console.log in transporter.sendMail, so that the user knows when the Email was sent successfully, but it does not work.
The error is "session is not defined".
This is how my code looks like:
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Gmail
// change the user and pass !
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'myemail#gmail.com',
pass: 'myPassword'
}
});
// setup e-mail data
var mailOptions = {
from: '"Our Code World " <myemail#gmail.com>', // sender address (who sends)
to: 'mymail#mail.com, mymail2#mail.com', // list of receivers (who receives)
subject: 'Hello', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info,session){
if(error){
return console.log(error);
}
session.send('Message sent: ' + info.response);
}
);

this is an example of how to doing it. Just make sure you call this method inside a session context like:
const sendmail = require('./email'); // in case you have the class called email
bot.dialog('/', function(session) {
sendmail.sendmail(session);
session.send("hello")
});
function sendmail(session){
var nodemailer = require('nodemailer');
// Create the transporter with the required configuration for Outlook
// change the user and pass !
var transport = nodemailer.createTransport( {
service: "hotmail",
auth: {
user: "",
pass: ""
}
});
// setup e-mail data, even with unicode symbols
var mailOptions = {
from: '"Our Code World " <shindar902009#hotmail.com>', // sender address (who sends)
to: 'shindar902009#hotmail.com', // list of receivers (who receives)
subject: 'Hello ', // Subject line
text: 'Hello world ', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js' // html body
};
// send mail with defined transport object
transport.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
session.send('Message sent');
});
}
module.exports.sendmail = sendmail;

transporter.sendMail(mailOptions, function(error, info , session)
You are not using a correct function definition, since the callback of transport.SendMail can only have two parameters (error & info). Have a look at the Nodemailer example.
In your case it would look like this. Just make sure you use this snippet where the session context is available.
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return session.error(error);
}
session.send('Message sent: ' + info.messageId);
});

I just ran this snippet replacing the username and password appropriately and ended up with this:
{ Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continu> Please log in via
534-5.7.14 your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/ - gsmtp
at SMTPConnection._formatError (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:605:19)
at SMTPConnection._actionAUTHComplete (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:1340:34)
at SMTPConnection._responseActions.push.str (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:378:26)
at SMTPConnection._processResponse (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:764:20)
at SMTPConnection._onData (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:570:14)
at TLSSocket._socket.on.chunk (C:\projects\nodemailertest\node_modules\nodemailer\lib\smtp-connection\index.js:522:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
code: 'EAUTH',
response: '534-5.7.14 <https://accounts.google.com/signin/continue?..._sniped> Please log in via\n534-5.7.14 your web browser and then try again.\n534-5.7.14 Learn more at\n534 5.7.14 https://support.google.com/mail/answer/78 - gsmtp',
responseCode: 534,
command: 'AUTH PLAIN' }
Furthermore I received an email from Google:
Someone just used your password to try to sign in to your account from a non-Google app. Google blocked them, but you should check what happened. Review your account activity to make sure no one else has access.
Are you sure this is the code that you're running? The error you posted, "session is not defined" seems like a syntax error.

Related

Node Mailer does not work even though it says its successful

I have question about the nodemailer that I am working on. The result returns email sent,
but I do not get any email. I am sure I am doing something wrong and I have no idea what that is.
I will post my code below.
let transporter = nodemailer.createTransport({
//pool: true,
host: "*****.net",
port: ****,
secure: false,
auth: {
user: "****.com",
pass: "*****",
},
tls: {
// do not fail on invalid certs
rejectUnauthorized: false,
},
});
var mailOptions = {
from: '"xxx"<info#****.com>;', // sender address
to: result.recordset[0].toEmail, // list of receivers
subject: "NEW USER", // Subject line
html: result.recordset[0].content, // plain text body
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("Email sent: " + info.response);
}
});
transporter.verify(function (error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
AND THIS IS THE MESSAGE I GET FROM THE TERMINAL BELOW
Server is ready to take our messages
Email sent: 250 uPMSkJg8an7bs mail accepted for delivery
If you are using for example a cheap mail service, mails might be sent after some time. I had that problem before. Services use queue system and prioritize the mail requests based on the pricing. Other than that, you can check your spam folder just in case if that might be a problem.

Using nodemailer and smtp send mail without authentication

Below is my code. This works fine with gmail smtp server.
But when I use my office one (which does not require authentication) it fails
May be the syntax I am using is wrong.
below is the code working with gmail smtp:
var mailOptions = {
from: 'xxxx#abcd.com',
to: 'xxxxyy#abcd.com',
subject: 'hello world!',
html: '<img src="cid:logo">',
attachments: [{
filename: 'test.png',
path: 'D:/bbbbb/mmmm/src/test.png',
cid: 'logo' //same cid value as in the html img src
}]
};
transport.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
}
console.log(`Message sent: ${info.response}`);
});
As Our company smtp does not require authentication,
I have tried below code:
var transport = nodemailer.createTransport("smtps://xxxx#abcd.com:"+encodeURIComponent('') + "#xxxx.xxxrxx.com:25");
OR
var transport = nodemailer.createTransport("smtps://xxx.xxx.com:25");
but all resulted error
{ Error: 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:797:
at Error (native) code: 'ECONNECTION', command: 'CONN' }
My guess is syntax is wrong.
Can some one pls correct me
Thanks and Regards.
You can remove auth option from the example code when creating an SMTP Transport message.
so it should look like the following:
/**
* Initialize transport object
*/
const transporter = nodemailer.createTransport({
host: "", //Host
port: , // Port
secure: true
});
let mailOptions = {
from: , // sender address
to: , // list of receivers
subject: , // Subject line
text: , // plain text body
html: // html body
};
/**
* send mail with defined transport object
*/
transporter.sendMail(mailOptions,
(error, info) => {
});

Automatic Send Update eMail

I want to start manege orders from suppliers.
I build a orders pages, that in one side- mine, i can place a new order, and
the 2nd side, the supplier, can see the orders, and update the status of every item.
Now, what i need is to automatic send mail to the supplier when sending the order, And the same for when each item is being ready for shipment.
What can be a good way to do it[no PHP if possible]? is it with node.js? cause when i checked Formspree for example, it was good only for sending to one mail address (like contact-us form).
Thank you
To send an email from node.js, you can use Nodemailer.
Example (copied from main website):
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 465,
secure: true, // secure:true for port 465, secure:false for port 587
auth: {
user: 'username#example.com',
pass: 'userpass'
}
});
// setup email data with unicode symbols
let mailOptions = {
from: '"Fred Foo 👻" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ?', // plain text body
html: '<b>Hello world ?</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message %s sent: %s', info.messageId, info.response);
});

Send email is not sending email when used nodemailer with protractor

No message is displayed for the error but also for the message for success is also not displayed. Also no email is recieved.
This is the code i had in the config file of protractor. Here there is no error response for the email but also there is no successful message also and no email is received.
this is in the config file
onComplete: function () {
creating reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP", {
host: "smtp.company.com",
port: "110",
auth: {
user: "anuvrat.singh#companyemail.com",
pass: "XXXX#2015"
}
});
setup the email data
var mailOptions = {
from: "anuvrat.singh#Company.com", // sender address
to: "anuvrat.singh#Company.com",
subject: "Hello ✔", // Subject line
text: "Hello world ✔", // plaintext body
html: "<b>Hello world ✔</b>" // html body
}
send mail with defined transport object
smtpTransport.sendMail(mailOptions, function (error, response) {
if there is an error it will display other wise it will send the message
if (error) {
console.log(error);
} else {
console.log("Message sent: " + response.message);
}
shut down the connection pool, no more messages
smtpTransport.close();
});
}

Cannot read property 'createTransport' of undefined

I am testing sending email with meteor js and nodemailer plugin:
meteor add mrt:meteor-nodemailer
when the page loaded, i saw error in the console of the navigator :
Cannot read property 'createTransport' of undefined.
so what is the problem ?
this is the code :
///////////////////////////////////////////
var nodemailer = Nodemailer;
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "myname#gmail.com",
pass: "mypass"
}
});
var emailNodemailer = function() {
// setup e-mail data with unicode symbols
var mailOptions = {
from: "Sender Name ✔ ", // sender address
to: "someone#yahoo.fr", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔", // plaintext body
html: "Hello world ✔" // html body
};
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
//smtpTransport.close(); // shut down the connection pool, no more messages
});
};
///////////////
This worked for me import * as nodemailer from 'nodemailer';

Categories