sending emails in node.js / nodemailer - node.js

I tried to use nodemailer to send emails using my gmail account.
However, Google rejects my login as suspicious and thinks I am a hacker.
I tried Yahoo which does send the email.
My questions are:
1) How can I configure nodemailer to send emails thru gmail
2) Standard/Reliable email library in node.js community with good support that can be used in production.

I was recently playing around with this as well, and the error I received from Google was simply stating that I needed to create an application specific password. Try following the instructions here.
As for your second question, I do not know of any reasons as to why nodemailer can't be used in production.

I used emailjs, and haven't had that issue. Not sure if that's because it's email.js or because it's Google Apps vs Gmail, or maybe Google is just less suspicious with this app for some reason. Maybe useful to try to triangulate:
$> npm install emailjs
emailjs = require('emailjs');
...
var server = emailjs.server.connect({
user:"myname#mygoogleapp.com",
password:"Secret#!1",
host:"smtp.gmail.com",
ssl:true
});
server.send({
text: message
from:"Display name <return#mydomain.com>",
to:email,
subject:"Subject"
},
function (err, message) { ... }

Related

Connecting Heroku Webhooks with Discord

I am trying to have updates to my heroku app sent to a Discord channel via webhook. However, the delivery attempts fail. I've double checked the Payload URL and it is correct. Not sure how to proceed from here.
Heroku's webhook format is not compatible with Discord so you can't just put a Discord webhook URL into Heroku. You need a middle-man server to receive events from Heroku, and construct and send corresponding messages to Discord.
Found this JS code, should work (change params variable and where it says webhook to your webhook url)
let x = new XMLHttpRequest();
x.open("POST", `<webhook link>`);
x.setRequestHeader('Content-type', 'application/json');
let params = {
username: "Webhook Bot",
content: "<message content as a string>"
}
x.send(JSON.stringify(params));
i should mention that to mention a channel instead of #channel-name you'll have to use <#channelid>, for example <#1234567890> instead of #foo-bar
(this is my first post, sorry if it's a bit bad)
Without being able to see your code and the request structure you are using it will be hard to determine where the issue is coming from exactly, but one thing you might what to check is how you are sending the JSON payload to the Discord webhook URL.
Discord does not seem to accept the request unless you specify that the body of the payload is JSON. I discovered this because of an application I am working on currently. I know this answer is coming significantly after the fact, but it might help someone else down the line!

Authentication errors when sending intermittent emails from gmail using Heroku / node.js / emailjs

I am running a scraper app on Heroku that sends emails every once in a while. Sometimes it'll be every 2 minutes, sometimes it won't send anything for 2 days-depends on what the scraper finds. I am running into continual errors with email authentication. I have tried configuring the email with nodemailer and emailjs. Both run into the same issue. I am wondering if it is because of the way Heroku servers are distributed?
I have taken the following steps:
1) unlocked the account at: https://accounts.google.com/DisplayUnlockCaptcha
2) allowed less secure apps here: https://myaccount.google.com/lesssecureapps
With both nodemailer and emailjs, I notice that it works fine for a couple hours but then if no emails are sent for a day, as soon as it tries to send another one I receive an authentication error. Here is the code using emailjs that works for a couple hours then fails with an authentication error:
var server = email.server.connect({
user: process.env.CRAWLER_MAIL,
password:process.env.CRAWLER_PWD,
host: "smtp.gmail.com",
ssl: true
});
var message = {
text: "Placeholder",
from: 'NAME <'+ process.env.CRAWLER_MAIL+'>',
to: eachEmail[0],
subject: emailSubject,
attachment:
[
{data:emailBody, alternative:true}
]
};
server.send(message, function(err, message) { console.log(err || message); });
And here is the error from the Heroku log:
Error: authorization.failed (bad response on command '[redacted]': -5.7.14 https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=[redacted]-)
I don't use 2-step verification for this account, so I don't think I need an app password. Any other ideas about how I can get this to consistently work?

how to setup twilio sms api in ionic angular?

#types/twilio
ran the following code and
installed twilio npm package
npm install twilio
In my provider i have imported by specifying
import * as twilio from 'twilio'
when i run by ionic serve --verbose
my console show an error that it cannot find module '../../package.json'
eventhough the library file has one, I will attach an image for reference
Twilio developer evangelist here.
The main Twilio Node.js module is not intended for use in a front end application so is unlikely to work well for you there. You are most likely getting that error as the Twilio module relies on some core Node modules that are unavailable in a browser environment.
It is not a good idea to try to use Twilio APIs from the client side as you expose your account credentials which would allow a malicious attacker to use your account for their own purposes.
Instead, we recommend you build a server that interacts with the Twilio API and that you make calls to from your own front end. If you are trying to use any of the Twilio client side SDKs, like Twilio Video, Twilio Chat or Twilio Sync, then you should install their respective modules twilio-video, twilio-chat and twilio-sync. You will still need a server side component that can generate access tokens for those services though.
Edit
In Node.js I just did a quick test for importing the Twilio module using TypeScript 2.6.2. This worked for me:
import { Twilio } from 'twilio';
const client = new Twilio("MY_ACCOUNT_SID", "MY_AUTH_TOKEN");
client.messages.each({ limit: 10 }, function(message) {
console.log(message.body);
});
Could you try this format for importing?
import { * as twilio } from 'twilio'
As philnash said, Twilio is not designed to run clientside. I tried to do the same thing you were doing. And you just have to have your Twilio run on a server and have your angular connect to that server to run it. Easiest way. Send a POST to your server via ajax and then have it run what you need.

NODEJS mail sending by nodemailer - looking for the pre version 3 option

I am a bit confused about nodemailer as a simple standard solution for node JS mailing.
I see the current nodemailer ver 3.x.x costs over $800(?!?!) and since I am building a module in an my MVP, to send an email once in a while, seems to me that I need another solution.
so:
how do I setup an older nodemailer version (I guess pre version 3.x.x)?
Any examples of how to use the older version code?
Other working options? I tried "mailover" with gmail and gmail rejects it will alerting on "Less secure app... because it has known security problems or is out of date..." issue.
Thanks in advance.
You can use this command to install the old nodemailer version
npm install nodemailer#2.0.0 --save
Then start to code by importing nodemailer module
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport();
transporter.sendMail({
   from: 'sender#address',
   to: 'receiver#address',
   subject: 'hello',
   html: '<b>hello world!</b>'
   text: 'hello world!'
});

How to add an email library in an existing nodejs server

I have a web server which is implemented using NodeJs. Now I want to add a functionality which will send a group of people email alerts if an event occurs. But I do not know how to add this feature in the existing code. I researched online and found sendgrid-nodejs but I do not know if that would work or not. I am not sure how to start. Please help.
You might want to check out nodemailer module.
The website has many different examples which you can follow.
Here is a one of those examples:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport();
transporter.sendMail({
from: 'sender#address',
to: 'receiver#address',
subject: 'hello',
text: 'hello world!'
});

Resources