Can't send email with nodemailer and outlook - node.js

i try to send an email with nodemailer and outlook but after multi try and multiple forum i visited, i found nothing.
I try all the solution i've seen but nothing work. can you help me please
this is my code :
{
const path = require('path');
var message=fs.readFileSync(path.join(__dirname+'../../../asset/templateMail/mail.html')).toString();
message=message.replace('${user}', user.confirmation_code.toString());
var nodemailer = require("nodemailer");
const transporter = nodemailer.createTransport(
{
//service :"Outlook365",
//pool:true,
host: 'SMTP.office365.com',
port: '587',
secure:false,
auth: { user: "it-factory-flex#outlook.fr", pass: process.env.NODEMAILLERPASS },
//secureConnection: false,
//requireTLS:true,
//tls: { ciphers: 'SSLv3' }
}
);
var mailOptions = {
name:"FlexOffice",
from: "it-factory-flex#outlook.com",
to: user.email,
subject: "FlexOffice: Code d'inscription",
html: message
}
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
}
all the commented part are my test
and i always got this:
Error: Connection timeout
at SMTPConnection._formatError (/home/romain/bred/flex-server/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
at SMTPConnection._onError (/home/romain/bred/flex-server/node_modules/nodemailer/lib/smtp-connection/index.js:770:20)
at Timeout.<anonymous> (/home/romain/bred/flex-server/node_modules/nodemailer/lib/smtp-connection/index.js:235:22)

solved, due to my network who block the flux.

Related

Incomplete response received from application NodeJS running on Plesk

i have a controller that sends email with nodemailer.
my app is running o plesk.
when i run it in my localhost every thing is ok. but when i run it on host with plesk i get this error :
Incomplete response received from application
my function :
submitInvoice = async (req, res, next) => {
try {
var transporter = mailer.createTransport({
host: process.env.mailhost,
port: 587,
auth: {
user: process.env.mailuser,
pass: process.env.mailPass,
},
tls: { rejectUnauthorized: false },
});
var mailOptions = {
from: process.env.mailuser,
to: process.env.reciever,
subject: "", // Subject line
// attachments: [{ filename: "new-form.xlsx", content: data }],
html:
"<!DOCTYPE html>" +
"<html><head><title>title </title>" +
"<style>" +
"body {background-color:#ffdd00;background-repeat:no-repeat;background-position:top left;background-attachment:fixed;}" +
"h2{font-family:Arial, sans-serif;color:#000000;background-color:#ffdd00;text-align:center;}" +
"p {text-align:center;font-family:Georgia, serif;font-size:19px;font-style:normal;font-weight:bold;color:#000000}" +
"</style>" +
"</head><body><div>" +
" <h2>فایل اکسل پیوست شد</h2>" +
"</div></body></html>",
};
await transporter.sendMail(mailOptions);
return res.render("client/Counseling");
} catch (err) {
next(err);
}
};
the out put of nodemailer is :
250 2.0.0 Ok: queued as 9A795414F662E
do you have any idea what the problem is ?

nodemailer not sent email in cloud function

I have this code in a cloud function where I want to use nodemailer to send some notification emails.
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: 'mygmailaddress#gmail.com',
pass: 'apppassword'
}
})
/* Send email for custom programs to unsigned patients */
exports.sendEmailToNewPatient = functions.https.onRequest( (req, res) => {
cors( req, res, () => {
const mailOptions = {
to: req.body.userEmail,
from: 'mygmailaddress#gmail.com',
subject: `${req.body.doctorName} test!`,
text: 'Test message',
html: '<h1>Test message/h1>'
}
transporter.sendMail(mailOptions, (error, info) => {
if( error ) {
res.send(error.message)
}
const sendMailResponse = {
accepted: info.accepted,
rejected: info.rejected,
pending: info.pending,
envelope: info.envelope,
messageId: info.messageId,
response: info.response,
}
res.send(sendMailResponse)
})
})
})
I'm calling the function using a POST request made with axios, when the request is send I will get a status code of 200 and in the data object of axios response I will have this informations
data:
code: "EENVELOPE"
command: "API"
When I check my test email address to verify if the email is sent, I will not have any message as expected.
Any suggestion about?
I can see a small mistake in your code that the heading tag is not closed in the html message.
For more reference on how to send the mail using node mailer you can follow the below code.
As mentioned in the link:
const functions = require("firebase-functions");
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const cors = require("cors")({
origin: true
});
exports.emailMessage = functions.https.onRequest((req, res) => {
const { name, email, phone, message } = req.body;
return cors(req, res, () => {
var text = `<div>
<h4>Information</h4>
<ul>
<li>
Name - ${name || ""}
</li>
<li>
Email - ${email || ""}
</li>
<li>
Phone - ${phone || ""}
</li>
</ul>
<h4>Message</h4>
<p>${message || ""}</p>
</div>`;
var sesAccessKey = 'YOURGMAIL#gmail.com';
var sesSecretKey = 'password';
var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: sesAccessKey,
pass: sesSecretKey
}
}));
const mailOptions = {
to: "myemail#myemail.com",
from: "no-reply#myemail.com",
subject: `${name} sent you a new message`,
text: text,
html: text
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error.message);
}
res.status(200).send({
message: "success"
})
});
}).catch(() => {
res.status(500).send("error");
});
})
;
For more information you can check the blog , thread and documentation where brief explanations including code is provided.
If all above has been followed well then you can check this thread for further details:
First of all, you have to enable the settings to allow less secure apps for the gmail account that you are using. Here is the link.
Secondly, Allow access for "Display Unlock captcha option" (Allow access to your Google account). Here is the link.
As mentioned by Frank van Puffelen ,here you can process POST data in Node.js using console.log(req.body.userEmail) for reference you can check link.

Relay server of godaddy server issue with express app contact form

From my express application, I am trying to send the contact form from godaddy relay server. locally it works fine. but after I hosted with godaddy server it's not working. my local code: ( working using gmail properties )
app.post("/send_mail", function (req, res) {
let { text } = req.body;
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 25,
secure: true,
auth: {
user: "xxxxxx#gmail.com",
pass: "xxxxxxxx"
}
});
let message = {
from: "from#email.com",
to: "xxxx#gmail.com",
subject: "Enquiry from Deane Project Management",
html: `
<hr />
<h1>Request for: ${text.option || "No title"}</h1>
<span>Name: ${text.username}</span><br/>
<span>Email: ${text.email}</span><br/>
<span>Phone: ${text.phone || "No phone"}</span><br/>
<span>Message:</span><br/>
<p>${text.message || "No message"}</p>
<hr />
`
}
transporter.sendMail(message, function (err, info) {
if (err) {
console.log(err);
} else {
console.log('sent', info);
}
});
res.send(200);
});
I am taken this reference,(https://in.godaddy.com/help/send-form-mail-using-an-smtp-relay-server-953#cpanel) relay server so updated my server app.js like this: But not works. I am not getting any error as well. from from end it's shows success message. But no email received yet.
app.post("/send_mail", function(req, res) {
let {text} = req.body;
const transporter = nodemailer.createTransport({
host: 'localhost',
port: 25,
secure: false
});
let message = {
from: "from#email.com",
to: "xxxxx#gmail.com",
subject: "Enquiry from Deane Project Management",
html: `
<hr />
<h1>Request for: ${text.option||"No title"}</h1>
<span>Name: ${text.username}</span><br/>
<span>Email: ${text.email}</span><br/>
<span>Phone: ${text.phone||"No phone"}</span><br/>
<span>Message:</span><br/>
<p>${text.message||"No message"}</p>
<hr />
`
}
transporter.sendMail(message, function(err, info) {
if (err) {
console.log(err);
} else {
console.log('sent',info);
}
});
res.send(200);
})

Nodemailer : How to send two different email to two different email address?

I have tried all and searched all stackoverflow but couldn't figure it out. Is there anyone who can help me out:
example:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail#gmail.com',
pass: 'yourpassword'
}
});
var mailOptions1 = {
from: 'company#gmail.com',
to: 'customer#yahoo.com',
subject: 'Sending Email to customers',
html: '<h1>Email to Custmer</h1>'
};
var mailOptions2 = {
from: 'company#gmail.com',
to: 'sales#yahoo.com',
subject: 'Sending Email to sales department',
html: '<h1>New Sales Order</h1>'
};
transporter.sendMail([mailOptions1, mailOptions2], function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
I have tried to pass mailOptions1 & mailOptions2 as array in the transporter.sendMail function but only one email is sent. How to make both emails to send ??
Thanks in advance...
Look at the interface of sendMail method of "nodemailer": "^6.5.0":
/** Sends an email using the preselected transport object */
sendMail(mailOptions: Mail.Options, callback: (err: Error | null, info: SentMessageInfo) => void): void;
sendMail(mailOptions: Mail.Options): Promise<SentMessageInfo>;
It only supports accept one mail option, does NOT support mail option array.
So you can use Promise.all() to send different email to different user concurrencily.
E.g.
Promise.all([mailOptions1, mailOptions2].map((opt) => transporter.sendMail(opt).catch(console.log))).then(
([sendMail1Res, sendMail2Res]) => {
console.log('sendMail1Res: ', sendMail1Res);
console.log('sendMail2Res: ', sendMail2Res);
},
);
Promise.all([transporter.sendMail(mailOptions), transporter.sendMail(mailOptions2)],(error,info)=>{
if (error) {
console.log(error)
} else {
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
}
})

How to send email in Firebase for free?

I'm aware that Firebase doesn't allow you to send emails using 3rd party email services. So the only way is to send through Gmail.
So I searched the internet for ways, so here's a snippet that works and allows me to send email without cost.
export const shareSpeechWithEmail = functions.firestore
.document("/sharedSpeeches/{userId}")
.onCreate(async (snapshot, context) => {
// const userId = context.params.userId;
// const data = snapshot.data();
const mailTransport = nodemailer.createTransport(
`smtps://${process.env.USER_EMAIL}:${process.env.USER_PASSWORD}#smtp.gmail.com`
);
const mailOptions = {
to: "test#gmail.com",
subject: `Message test`,
html: `<p><b>test</b></p>`
};
try {
return mailTransport.sendMail(mailOptions);
} catch (err) {
console.log(err);
return Promise.reject(err);
}
});
I want to create a template, so I used this package called email-templates for nodemailer.
But the function doesn't get executed in Firebase Console and it doesn't show an error and shows a warning related to "billing".
export const shareSpeechWithEmail = functions.firestore
.document("/sharedSpeeches/{userId}")
.onCreate(async (snapshot, context) => {
const email = new Email({
send: true,
preview: false,
views: {
root: path.resolve(__dirname, "../../src/emails")
// root: path.resolve(__dirname, "emails")
},
message: {
// from: "<noreply#domain.com>"
from: process.env.USER_EMAIL
},
transport: {
secure: false,
host: "smtp.gmail.com",
port: 465,
auth: {
user: process.env.USER_EMAIL,
pass: process.env.USER_PASSWORD
}
}
});
try {
return email.send({
template: "sharedSpeech",
message: {
to: "test#gmail.com",
subject: "message test"
},
locals: {
toUser: "testuser1",
fromUser: "testuser2",
title: "Speech 1",
body: "<p>test using email <b>templates</b></p>"
}
});
} catch (err) {
console.log(err);
return Promise.reject(err);
}
});
You can definitely send emails using third party services and Cloud Functions, as long as your project is on the Blaze plan. The official provided samples even suggest that "if switching to Sendgrid, Mailjet or Mailgun make sure you enable billing on your Firebase project as this is required to send requests to non-Google services."
https://github.com/firebase/functions-samples/tree/master/quickstarts/email-users
The key here, no matter which email system you're using, is that you really need to upgrade to the Blaze plan in order to make outgoing connections.
you can send emails by using nodemailer:
npm install nodemailer cors
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const cors = require('cors')({origin: true});
admin.initializeApp();
/**
* Here we're using Gmail to send
*/
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'yourgmailaccount#gmail.com',
pass: 'yourgmailaccpassword'
}
});
exports.sendMail = functions.https.onRequest((req, res) => {
cors(req, res, () => {
// getting dest email by query string
const dest = req.query.dest;
const mailOptions = {
from: 'Your Account Name <yourgmailaccount#gmail.com>', // Something like: Jane Doe <janedoe#gmail.com>
to: dest,
subject: 'test', // email subject
html: `<p style="font-size: 16px;">test it!!</p>
<br />
` // email content in HTML
};
// returning result
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
});
See also here
Set Security-Level to avoid error-messages:
Go to : https://www.google.com/settings/security/lesssecureapps
set the Access for less secure apps setting to Enable
Refer to
Call a sendMail() cloud function directly via functions.https.onCall(..) :
As #Micha mentions don't forget to enable Less Secure Apps for the outgoing email: https://www.google.com/settings/security/lesssecureapps
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
let mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'supportabc#gmail.com',
pass: '11112222'
}
});
exports.sendMail = functions.https.onCall((data, context) => {
console.log('enter exports.sendMail, data: ' + JSON.stringify(data));
const recipientEmail = data['recipientEmail'];
console.log('recipientEmail: ' + recipientEmail);
const mailOptions = {
from: 'Abc Support <Abc_Support#gmail.com>',
to: recipientEmail,
html:
`<p style="font-size: 16px;">Thanks for signing up</p>
<p style="font-size: 12px;">Stay tuned for more updates soon</p>
<p style="font-size: 12px;">Best Regards,</p>
<p style="font-size: 12px;">-Support Team</p>
` // email content in HTML
};
mailOptions.subject = 'Welcome to Abc';
return mailTransport.sendMail(mailOptions).then(() => {
console.log('email sent to:', recipientEmail);
return new Promise(((resolve, reject) => {
return resolve({
result: 'email sent to: ' + recipientEmail
});
}));
});
});
Thanks also to: Micha's post
You can send for free with Firebase extensions and Sendgrid:
https://medium.com/firebase-developers/firebase-extension-trigger-email-5802800bb9ea
DEPRECATED: https://www.google.com/settings/security/lesssecureapps
Use "Sign in with App Passwords": https://support.google.com/accounts/answer/185833?hl=en to create an app password.
follow the steps in the link then use the given password with your user in mailTransport:
const mailTransport = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'user#gmail.com',
pass: 'bunchofletterspassword'
}
});

Resources