Cannot read property 'createTransport' of undefined - javascript

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';

Related

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

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.

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) => {
});

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();
});
}

Not able to send mail using Nodemailer

It's a basic example for Nodemailer.
var http = require('http');
var port = process.env.PORT || 8080;
var async = require('async');
var nodemailer = require('nodemailer');
// create reusable transporter object using SMTP transport
var mailOptions = {
from: '*********', // sender address
to: 'rishiag.iitd#gmail.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world ✔</b>', // html body
attachments: [
{ // utf-8 string as an attachment
filename: 'text1.txt',
content: 'hello world!'
}]
};
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: '*******',
pass: '****'
}
});
// send mail with defined transport object
var server = http.createServer(function(request, response) {
if (request.url === '/favicon.ico') {
response.writeHead(200, {'Content-Type': 'image/x-icon'} );
response.end();
return;
}
response.writeHead(200, {
"Content-Type": "text/plain"
});
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log("error is " ,error);
}else{
console.log('Message sent: ' + info.response);
}
});
response.end("Hello World\n");
}).listen(port);
console.log("Node server listening on port " + port);
I am getting following error on going to localhost:
[Error: No transport method defined]
I am using Nodemailer version 1.4.23 on Windows 7. What could be the problem?
Their initial example appears to mention Gmail for the service when it should instead be gmail per other examples provided. Seems to be a documentation issue.

How to hide SMTP Auth user mail id in Nodemaile

I am using nodemailer in my project . (sorry for
My english is not good )
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "agodatest#gmail.com",
pass: "pwd"
}
});
smtpTransport.sendMail({
from: "James andrison <james#andr.com>", // sender address
to: "user2 surname<receiver#receiver.com>", // comma separated list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world ✔" // plaintext body
}, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
});
In the Above code When I receive mail. I can see Sender name with auth.user email id in the mail but cannot see Sender email id.
I got output like this --> James andrison ;
So can you tell me how to hide SMTP / Auth email id ?
Thankx in advance .

Categories