I am trying to send an email to my mailcheap account with nodejs and nextjs. For reference I followed this tut nodemailer and nextjs
just can't seem to connect to it. My email is pointing correctly to vercel which is where I am hosting my app. As I can email myself directly without nodejs.
This is the code
require('dotenv').config()
export default function (req, res) {
let nodemailer = require('nodemailer')
const transporter = nodemailer.createTransport({
port:'465',
host:'mail.privateemail.com',
auth: {
user: 'lessons#teacher-esl.com',
pass: process.env.PASS,
},
secure: true,
});
const mailData = {
from: 'randomemail#hotmail.com',
to: 'lessons#teacher-esl.com',
subject: `Message From ${req.body.name}`,
text: req.body.message,
html: <div>{req.body.message}</div>
}
transporter.sendMail(mailData, function (err, info) {
if(err)
console.log(err)
else
console.log(info)
})
res.status(200).end()
console.log(req.body);
}
In the terminal I keep getting missing credentials for "PLAIN".
You are authenticating with lessons#teacher-esl.com but trying to send the mail with randomemail#hotmail.com.
That's where the problem lies.
If you want to send mail to lessons#teacher-esl.com then you would have to do this:
const transporter = nodemailer.createTransport({
host:'mail.privateemail.com',
port:'465',
secure: true,
auth: {
user: 'randomemail#hotmail.com',
pass: <randomemail-pass>,
},
});
Related
My Node js code is sending proper email on localhost but in backend there showing email and password not valid ?
const nodemailer = require("nodemailer");
const sendEmail = async (subject, message, send_to, sent_from, reply_to) => {
// Create Email Transporter
const transporter = nodemailer.createTransport({
host: process.env.EMAIL_HOST,
port: 465,
secure: true,
auth: {
user: process.env.EMAIL_USER,
pass: process.env.EMAIL_PASS,
},
});
// Options for sending email
const options = {
from: sent_from,
to: send_to,
reply_to: reply_to,
subject: subject,
html: message,
};
// Check email sent successfully or not
transporter.sendMail(options, function (err, info) {
if (err) {
console.log(err);
} else {
console.log(info);
}
});
};
module.exports = sendEmail;
I am getting error "Invalid login: 535-5.7.8 Username and Password not accepted" while trying to send mail using nodejs.
Below is my configuration
this._transport = nodemailer.createTransport({
service: 'Outlook365',
name: 'smtp.office365.com',
host: 'smtp.office365.com', // Office 365 server
port: '587',
secure: false,
auth: {
user: process.env.MAIL_USERNAME,
pass: process.env.MAIL_PASSWORD,
},
secureConnection: false,
tls: { ciphers: 'SSLv3' }
})
and while sending the mail
var mailOptions = {
from: `Admin<${settingObj.Contact_Email}>`,
to: req.body.email,
subject: "Subject",
html: 'Hello ' + '<b>' + req.body.email + '<br>Thank You for contacting'
};
var sendMail = await transporter.sendMail(mailOptions);
below is sendmail function
async sendMail(from, to, subject, tplName, locals) {
try {
const mailer = new Mailer();
const templateDir = path.join(__dirname, "../views/", 'email-templates', tplName + '/html')
const email = new Email({
message: { from: from },
transport: { jsonTransport: true },
views: { root: templateDir, options: { extension: 'ejs' } }
});
let getResponse = await email.render(templateDir, locals);
if (getResponse) {
let options = { from: from, to: to, subject: subject, html: getResponse };
let mailresponse = await mailer._transport.sendMail(options);
if (mailresponse) {
return true;
}
else {
return false;
}
}
}
catch (e) {
console.log("44>>", e.message);
return false;
}
};
Before doing anything, ensure you are able to log in to your office 365 account with your username and password.
if you're facing this error in a hosted app, make sure you've created the env variables in config-vars/parameter-store/secrets or whichever is applicable for the platform?
Use telnet to access your office 365 SMTP account, doing so, you can get to know if it's a problem with your code or the settings.
Here is my working code,
const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
app.get('/', (req, resp)=>{
const email = `
<h3>Hello dee.....</h3>
<ul>
<li>Name: Deepak </li>
<li>Email: dee#dee.com </li>
</ul>
<h2>Message<h2>
<p>Wassup....howdeeee</p>
`
let transporter = nodemailer.createTransport({
host: "smtp.office365.com",
port: '587',
auth: {
user: '....',
pass: 'blahhhhh'
},
secureConnection: false,
tls: { ciphers: 'SSLv3' }
});
transporter.sendMail({
from: '.....',
to: '.....',
subject: 'Test for twix',
text: 'hello twix',
html: email
}).then(res=>{
console.log("success........", res)
resp.send("yeahhhhhhhh", res);
}).catch(e=>{
console.log("failed........", e)
});
});
app.listen(5000, console.log("Server listening to 6k"));
I enabled two-factor authentication and logged out of all active sessions then tried again, it worked fine with the above code.
Updated answer in response to comments
const express = require('express');
const nodemailer = require('nodemailer');
const app = express();
app.get('/send', async (req, resp) => {
let transporter = nodemailer.createTransport({
host: "smtp.office365.com",
port: '587',
auth: {
user: '......',
pass: '......'
},
secureConnection: false,
tls: { ciphers: 'SSLv3' }
});
var mailOptions = {
from: '.......',
to: '.......',
subject: "Twix mailer",
html: 'Hello ' + '<b>' + 'deechris27' + '<br>Thank You for contacting'
};
let sendMail = await transporter.sendMail(mailOptions);
console.log("twix email.....", sendMail.response);
});
app.listen(5000, console.log("Server listening to 5k"));
Success message below
Suggested changes
this._transport = nodemailer.createTransport({
service: 'Outlook365', // remove this
name: 'smtp.office365.com', // not needed
host: 'smtp.office365.com',
port: '587',
secure: false, // remove this
auth: {
user: process.env.MAIL_USERNAME, // hardcode username to try
pass: process.env.MAIL_PASSWORD, // hardcode pwd to try in local
},
secureConnection: false,
tls: { ciphers: 'SSLv3' }
});
comment out sendMail(from, to, subject, tplName, locals) function. Where is this called? this appears to be like you're trying to do what Nodemailer does internally. Though it wouldn't throw an invalid login error, I advise you to comment out for debugging.
I got the email "Hello deechris27 Thank You for contacting" to my inbox when this
let sendMail = await transporter.sendMail(mailOptions); promise was resolved.
createTransport of nodemailer returns an instance of Mailer which provides the sendMail function. The "From" address in mailOptions should be same as the auth.user.
I'm using Nodemailer Version 6.6.3. Refer to node_modules -> nodemailer -> lib -> nodemailer.js for more clarity.
Helpful Resources:
https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/concept-fundamentals-security-defaults
https://answers.microsoft.com/en-us/outlook_com/forum/all/i-cant-send-e-mails-from-my-server-through-office/999b353e-9d4f-49b2-9da9-14d6d4e6dfb5
On your Outlook dashboard, you need to allow less secure apps from your outlook account to send emails. Through this link. Allow less secure apps
For a school project I have to build a website and all the stuff...
I want to send an email when a certain button is pressed. For now I used an gmail address for the server BUT it needs authentification and all. How can I bypass the authentification ? Are there some other STMP servers that do not require authentification so I send an email easily ?
Thanks guys !
You should use Nodemailer
Its a npm module installed it and there you go.
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail#gmail.com',
pass: 'yourpassword'
}
});
var mailOptions = {
from: 'youremail#gmail.com',
to: 'myfriend#yahoo.com',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
Refer : https://www.w3schools.com/nodejs/nodejs_email.asp
var transporter = nodemailer.createTransport({
host:'smtp.gmail.com',
port:587,
secure:false,
auth: {
user: 'youremail#gmail.com',
pass: 'here app password'
}
});
url=`http://localhost:3000/verify-email/${token}`;
let mailDetails={
from:'"verify Your Email"< youremail#gmail.com>',
to:user.email, //receiver email address
subject:'Register Verify Your Email',
html:`<h2>${user.fullname} thanks for register on our site </h2>
<h4>please verfiy your mail to continue....</h4>
verfiy your Email`
}
transporter.sendMail(mailDetails,function(err,data){
if(err){
console.log('error ocures..',err)
}else{
console.log('verfify email is sent to your account');
}
})
I have a simple piece of code which is not working as expected. I am using nodemailer.
var nodemailer = require('nodemailer');
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
user: 'personal gmail address',
pass: 'password'
}
});
var mailOptions = {
from: 'personal gmail address',
to: 'personal gmail address',
subject: 'Hello world!',
text: 'Plaintext message example.'
};
smtpTransport.sendMail(mailOptions, function(err) {
console.log('Message sent!');
});
I am getting the Message Sent in console. But no emails in inbox.
For gmail service I use this :
const smtpTransport = require('nodemailer-smtp-transport')
const nodemailer = require('nodemailer')
const transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: 'email',
pass: 'pass'
}
}))
But you need to allow less secure authentication on your gmail account or emails are not sent.
I think the steps are :
Go to : https://www.google.com/settings/security/lesssecureapps
set the Access for less secure apps setting to Enable
Found this SO: self signed certificate in certificate chain error in mail .
I needed to add this and it worked for me.
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'myemail#gmail.com',
pass: 'password'
},
tls: {
rejectUnauthorized: false
}
});
Can you check for the error message.
...
smtpTransport.sendMail(mailOptions, function(err) {
if(err)
{
console.log(err);
}
else
{
console.log('Message sent!');
}
});
Because in the sample code you had, even if the nodemailer returns valid error, it will still print Message Sent
Some times message can be queued or say any type of error in the mailOptions, the message cant be delivered.
I'm using NodeJS and I want to configure my email account with Nodemailer.
All the Nodemailer examples that I found are for Gmail...
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user#gmail.com',
pass: 'userpass'
}
});
How can I put another service? In special I bought a email domain in ovh (SSL0.OVH.NET) and I interested to configure this email account.
I tried but I don't found the way to get this...
Thank you!
I create a module Mail for send html mails by SMTP
exports.send = function(email, subject, htmlcontent, callback) {
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var configMail = require('../bin/config').mail;//my json configurations mail
var transporter = nodemailer.createTransport(smtpTransport({
host: configMail.host, //mail.example.com (your server smtp)
port: configMail.port, //2525 (specific port)
secureConnection: configMail.secureConnection, //true or false
auth: {
user: configMail.auth.user, //user#mydomain.com
pass: configMail.auth.pwd //password from specific user mail
}
}));
var mailOptions = {
from: configMail.email,
to: email,
subject: subject,
html: htmlcontent
};
transporter.sendMail(mailOptions, function(err, info){
transporter.close();
if(err) {
callback(err, info);
}
else {
callback(null, info);
}
});
}
The usage:
var Mail = require('../utils/Mail'); //require this module
Mail.send('[EMAIL TO SEND]', '[TITLE]', '[YOUR HTML TEMPLATE MAIL]', function(err, info) {
if(err) {
//error
}
else {
//Email has been sent and you can see all information in var info
}
});
On creating an email id in the domain (mostly through c panel), check the mail configurations for that mail address and look for SMTP details such as port, username, etc. Then enter those details in the nodemailer transporter. For example, my transporter details were:
const transporter = nodemailer.createTransport({
host: "mail.schoolprogramming.tech",
port: 465,
secure: true, // true for 465, false for other ports
auth: {
user: "demo#schoolprogramming.tech",
pass: "**************"
}
});