I am trying to make a contact page with react and I'm struggling with sending the e-mail part.
I'm trying to use nodemailer, and my code for that is:
var nodemailer = require('nodemailer');
var xoauth2=require('xoauth2');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
xoauth2:xoauth2.createXOAuth2Generator({
user: 'mymail#gmail.com',
clientId: '',
clientSecret: '',
refreshToken:''
})
}
});
var mailOptions = {
from: 'Name <mymail#gmail.com>',
to: 'mymail#gmail.com',
subject: 'Sending Email to test Node.js nodemailer',
text: 'That was easy to test!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent');
}
});
I have put the clientId, clientSecret and refreshToken from google API and oauth2 playground and enabled the non secure apps thing. But when I'm trying to send the e-mail I get
TypeError: net.isIP is not a function
EDIT: I have tried adding after service: 'gmail'
type: 'SMTP',
host: 'smtp.gmail.com',
Still not working
I was facing the same issue when trying to implement the nodemailer code client side. The reason was because nodemailer doesn't seem to work in the browser (only in node). Moving it serverside (into an express app) solved the issue.
This post regarding the same error (but affecting a different library) also suggests that running the files in the browser is the problem: http://www.ganzhoupress.com/github_/cypress-io/cypress/issues/1981
Hope this helps.
replace ==>
var nodemailer = require('nodemailer');
with this ==>
var nodemailer = window.require('nodemailer');
this will solve your problem.
Related
I am working on a small service that allows users to send emails in a similar fashion to mailchimp.
How is mailchimp able to send an email from my account without asking me for my password but by simply sending me a confirmation email?
I was wondering if it was possible to replicate this effect using something like nodemailer?
Previously I wrote some code
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'test#gmail.com',
pass: 'APP_PASS'
}
});
var mailOptions = {
from: 'test2#gmail.com',
to: 'recipent#gmail.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);
}
});
I was hoping that the "from" part would say for example "test2#gmail.com" but it didn't, for understandable reason.
How do I prompt a user to giving my app access to sending emails from there account or on behalf of their account and then put it into nodemailer, or if nodemailer can not handle this, what could be a similar approach?
I am using nodemailer to send emails to user with office 365 account email and password are all correct but every time i am getting error - Authentication unsuccessful
Error: Invalid login:Authentication unsuccessful[BL0PR01CA0033.prod.exchangelabs.com]
code: 'EAUTH',
response: '535 5.7.3 Authentication unsuccessful [BL0PR01CA0033.prod.exchangelabs.com]',
response Code: 535,
command: 'AUTH LOGIN'**
You have to enable SMTP login for the O365 mail box or user in the admin settings
Go to Mail Settings
Turn On Authenticated SMTP
once that is done use
var transport = nodemailer.createTransport({
service: "Outlook365",
auth: {
user: 'O365email',
pass: 'O365password'
},
});
var mailOptions = {
from: 'o365email',
to: 'exaple#gmail.com', // list of receivers
subject: "Password reset requested for your account", // Subject line
text: 'reset password',
html: "<h1>Mail Testing</h1>" // html body
};
transport.sendMail(mailOptions, function(error, response){
if(error){
resp.status(500);
resp.send(error);
}else{
resp.send({message:'done'});
}
});
I have faced the same problem and after a lot of research and following few document. I came to this solution that
As an Office365 (now Microsoft365) customer I need to go to portal.azure.com, then "Manage", "Properties" and set "Manage Security defaults" to "no". I think this is messed up, but it works.
https://learn.microsoft.com/en-us/azure/active-directory/fundamentals/concept-fundamentals-security-defaults
https://github.com/nodemailer/nodemailer/issues/1071
Here is the setting what I am using
host: "domain.com",
port: 587,
secure: false,
auth: {
user: "email#domain.com",
pass: "password"
},
tls: { ciphers: 'SSLv3' },
service: "Outlook365",
The easy way to fix this is here: https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission#use-the-microsoft-365-admin-center-to-enable-or-disable-smtp-auth-on-specific-mailboxes
Under the title: Use the Microsoft 365 admin center to enable or disable SMTP AUTH on specific mailboxes
You need to enable SMTP AUTH for the email you are adding creds for.
In my case, I logged on to my outlook email account and changed the POP settings to let devices and apps use POP, I also verified my account.
Everything worked well afterward.
I need to send emails from my application for any exceptions, so I am using nodemailer for that but I cannot provide username and password for the account and without which It is not letting me send any emails. Any solutions? This is how my code looks as of now:
import nodemailer from 'nodemailer';
//Create reusable transport object using the SMTP transport
let transporter = nodemailer.createTransport({
host: 'smtp-mail.outlook.com',
port: 587,
secureConnection: false,
requireTLS: true,
tls: {
// do not fail on invalid certs
rejectUnauthorized: false
}
});
module.exports = function sendEmail(to, subject, message) {
let mailOptions = {
from: '********',
to,
subject,
html: message
};
transporter.sendMail(mailOptions, function(error) {
if (error) {
console.log(error);
}
});
};
I don't quite know about nodemailer, but with sendmail, you can send emails without a SMTP server, but for that to work you have to use dkim authentication, for short your email must be sent from the same domain as the email you're trying to send, take a look at this article, hope that helps.
I'm a very beginner backend developer, so my apologies if this question is rather novice. For a certain javascript file, the script performs perfectly when I execute it using
$ node hello.js
However, I wish to execute this javascript code through a button click on my form. Encapsulating this code into a function and calling the function seems to result in an error every single time.
tls.connect is not a function
Is there a way to manually reproduce the terminal command $ node hello.js from javascript? Any solutions or helpful links would be greatly appreciated.
For some context, the code in question is meant to forward an email with a message to a single recipient when activated. I am using nodemailer to achieve this alongside a server hosted on DigitalOcean. The webapp is built using React.
sendMyMail(event) {
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
let transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
secure: false,
port: 25,
auth: {
user: 'myemail#gmail.com',
pass: 'mypass'
},
tls: {
rejectUnauthorized: false
}
}));
let HelperOptions = {
from: '"Contact Form" <myemail#gmail.com',
to: 'myforward#gmail.com',
subject: 'Hello World',
text: 'Hello World 2.0'
};
transporter.sendMail(HelperOptions, (error, info) => {
if (error) {
console.log("Unable to send mail!");
return console.log(error);
}
console.log("The message was sent!");
console.log(info);
});
}
You will need a node server in order to send an email via the script you wrote. The script must be invoked in order to send the mail. In order to invoke that particular script, you'll need a server that will serve that script when route (which handles the script invocation) is encountered.
For details, see: https://appdividend.com/2017/08/11/send-email-in-node-js/#
I'm trying few days to composing mail sending with node.js to gmail with no success :-(
at the beginning of tries I try to send form submittion to my gmail but first I need to understand how do I sent simple mail from body to gmail,
perhaps I wrong with syntax or missing something?
actually I uses "heroku" as storage and domain for my site
I tried several plugins (such as mailgun, send grid and more) but the process was too complicated integrate their API's into my site.
actually,
I find this article in stack overflow that describe how to send the via nodemailer in relation to my error - URL: node.js nodemailer gmail error
when i copy the answer1 to my code i still receiving error.
I' also pass trough all gmail setup how to turn off security block for less secured apps but i'm still receiving error.
all the requires installed over my npm
npm install --save
** code **
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
function handleSayHello(req, res) {
var transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: 'myGmail#gmail.com', // my mail
pass: 'myPassword'
}
}));
var mailOptions = {
from: 'myGmail#gmail.com', // sender address
to: 'myGmail#gmail.com', // list of receivers
subject: 'Email Example' // Subject line
//text: text //, // plaintext body
//html: '<b>Hello world ?</b>' // You can choose to send an HTML body instead
};
transport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
res.json({
yo: 'error',
err: error
});
} else {
console.log('Message sent: ' + info.response);
res.json({
yo: info.response,
info: info
});
};
});
}
router.get('/', function (req, res, next) {
handleSayHello(req, res);
});
then i receive this error:
{
"yo": "error",
"err": {
"code": "EAUTH",
"response": "534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtx\n534-5.7.14 e8jnXXDaDg-dpFfc3H5ljeaBdmnxbXBDXMh-aXS-mmV4gQsXMSZLiiJHDXuOr3wy-IR2Zp\n534-5.7.14 xnznWLf5y5e3xTbJNlZBNcxBMajM9-SFQGy1MQM2XZRpHgtywuDtEj5iPiP0b2T6Wjbsxg\n534-5.7.14 hgehfzufG6h13qhjQK5IgRPNQsSICRQBtRCl3E1J62wFo8bnvZv4peY5aK55JMpwhSavJb\n534-5.7.14 ho-b9ExGGsXFmw_Er6lc8m3vCmO_Q> Please log in via your web browser and\n534-5.7.14 then try again.\n534-5.7.14 Learn more at\n534 5.7.14 https://support.google.com/mail/answer/78754 t35sm887733qtc.40 - gsmtp",
"responseCode": 534,
"command": "AUTH PLAIN"
}
}
if someone can take my code and fix it (if needed) and explain simply what do i need to modify in my google account I'll be great-full so much!
to allow from google/gmail, you should follow these links and allow to less secure apps access:
unlock account
and
Less secure apps
Let me know if still have issue, I'll check/debug your code.