Mail messages sent from nodemailer are not showing in user mail sent box - node.js

I am sending messages from my domain account but they are not showing in user(from options of nodemailer) sent box.But when sending messages from gmail service messages are showing in sent box of user.Am I missing something in below code?
var transport = nodemailer.createTransport({
host: "xxxx.domain.com",
auth: {
user: 'xyx',
pass: '123'
}
});
transport.sendMail(options, function (err, info) {
if (err) {
console.log(err)
}
console.log(info);
});

When you send a mail using a regular mail client, such as Thunderbird, it will send the mail to your SMTP server, which then relays the message to the receiving mail server (also via SMTP). The copy in your sent folder, however, is additionally saved on your mail server via IMAP. So your mail is actually send twice, once to the receivers mail server, and a copy is "send" to your own mail server.
When using nodemailer you only provide the credentials for your SMTP server, so the mail is only send without storing a copy in your sent directory. So this is basically working as intended.
I can think of two ways to save a copy of the mail in the sent directory:
Use an additional library, such as node-imap to imitate the behavior of a regular mail client and manually save a copy of the mail (e.g., node-imap has an append method to save new mails).
Add your own mail address as BCC to all outgoing mails and use some type of server side filtering to automatically move them to the sent folder. This is computationally less expensive for your application, but has the additional filtering requirement for the mail server.

Related

SMTP server using hmailserver. Host cannot be resolved to a type

I am trying to create a local SMTP server. I configured everything according to this: Link to set-up hmailserver
After that, I ran my diagnostics thing and it said host(in my case, noreply#bufferoverflow.com) cannot be resolved. I googled and read the documentation and question section of hmailserver Docs and found that I am missing a DNS configuration on my machine.
I followed this article to set-up the DNS ip to name: How to add ip domain mapping in windows
127.0.0.1 bufferoverflow.com
on my
C:\Windows\System32\drivers\etc>notepad hosts.ics
It still said host cannot be resolved to a type. I tried to see if the port was actually open and listening. So I did this command in CMD:
netstat -a
It's listening. I doubled checked everything but coudn't found where I was going wrong. I created a simple nodejs server and hosted it on port 80 at localhost and typed "bufferoverflow.com" in my browser it opened 127.0.0.1 aka localhost which is because it has an ip domain mapping I have given.
Now I am lost as why the hostname can't be resolved. My DNS is cool. Everthing is configured as docs says and the port is also open and listening. Please help me.
So based on what you said you created your own SMTP server which is nice and all to learn, but I would recommend using SendinBlue 3rd party service FREE and gives you 300 mails/day
Getting Started with sendinblue:
Link: https://www.sendinblue.com/
Go make and an account free of charge till you decide to pay you'll read there
Once your account is all set, and activated often SMTP service for ones account is not automatically activate so you'd have to email them and they literally respond within 24hr I LOVE THE SERVICE(I am not promoting nor sponsored to hype them or anything just saying the truth of what I have experienced).
So now at this point your account and SMTP service is activated, now question is how do you use sendinBlue?
In your dashboard on your Top right you will see your username
Click on that and a dropdown menu should appear
Then you want to go to SMTP & API
Once you then you will see a menu with API Keys on the left and SMTP on the right
You want to click on the SMTP.
Once you the you will see Login that would be the email you registered with and you will see Master password now that is what you use to authenticate yourself.
Implementation: Now you have everything sorted you know where you keys are and login
In .env file
sendinBlue__login=<Your email that your saw in that smtp tab>
sendinBlue__key= <Key you saw in that smtp tab>
In your file where you want to send email
const nodemailer = require('nodemailer');
router.post('/api/email', (request, response)=>{
const transporter = nodemailer.createTransport({
service:'SendinBlue',
auth:{
user:process.env.sendinBlue__login,
pass:process.env.sendinBlue__key
}
})
const mailOptions = {
from:process.env.sendinBlue__login
to:'example#gmail.com',
subject:'Order confirmation',
html:`
<h1>Confirmation of your received order</h1>
`
}
transporter.sendMail(mailOptions, (error, info)=>{
if (error){
console.log(error);
return response.status(500).json({msg:"Email failed to send probably network problems or SMTP not activated"});
}
return reponse.status(200).json({msg:'Email sent'});
})
})
Now this is how easy sendinBlue works just simple signup and account activation also SMTP activation if not activated from get go by email customer support
If that guide above did not make sense to you: Shameless plug
You can go checkout my video that I did and I was using sendinBlue with nodemailer for emailing it's a bit at the end
Link: https://youtu.be/5vWXbGIdDQw
Now If you want to send an email using noreply#domain.com with sendinBlue you do not even have to have the email activated but still can send
How you go about this is the way you craft your mailOptions
router.get("/api/test", (request, response) => {
const transpoter = nodemailer.createTransport({
service: "SendinBlue",
auth: {
user: process.env.sendinBlue__email,
pass: process.env.sendinBlue__key,
},
});
const mailOptions = {
from: "noreply#Test.com",
to: "juniorWebProjects#gmail.com",
subject: "Confirmation Order",
html: `
<h1>Order Received</h1>
`,
};
transpoter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err);
}
return console.log("Message sent");
});
});
See on my mailOptions I explicitly wrote myself the email that I want and when sendinBlue sends that email the receiver will see that is coming from noreply#Test.com

Receive an email when Firebase Realtime DB gets new data from Dialogflow

I want to receive an email when my Firebase Realtime database gets new data (user phone).
Data are sent to Firebase with Dialogflow inline editor.
I found that I have to use nodemailer and update my package.json with depedencies.
I found an example but it sends the email to the user.
I didn't succeed to adapt it to send me an email (not to the user) when he fills a new data with Firebase function (with onWrite()).
In the example you reference, lines 42 - 45 should allow you to change the recipient's email address:
const mailOptions = {
from: '"Spammy Corp." <noreply#firebase.com>',
to: val.email,
};
What happens when you pass in your own email as a string in the mailOptions.to object?

Mailgun message going into outlook spam debug

I am sending emails from users via mailgun to another users email inbox. The reply address is a generated address e.g. HkMv_j9QG#mydomain.com which is then routed back into the application.
I have checked an email using http://www.mail-tester.com/ and I got 10/10. However, I am getting put into the "Junk Email" folder in outlook (Gmail seems fine).
I am using mailgun-js and mailcomposer to send the MIME emails with the following code:
export const sendHtmlEmail = ({ from, to, replyTo, subject, html, inReplyTo }) => {
return new Promise((resolve, reject) => {
const mail = mailcomposer({
from,
to,
subject,
html,
text: htmlToText.fromString(html)
})
mail.build((error, message) => {
if (error) {
reject(error)
return
}
let data = {
to,
message: message.toString("ascii")
}
if(replyTo) {
data['h:Reply-To'] = replyTo
}
if(inReplyTo) {
data['h:In-Reply-To'] = inReplyTo
data['h:References'] = inReplyTo
}
mailgun.messages().sendMime(data, (error, body) => {
if (error) {
reject(error)
return
}
resolve(body)
})
})
})
}
Is there any way I can debug why I am getting put into outlook's spam folder? Or does anyone know of any additional headers I need to add?
I have a green tick on all the DNS settings - bar the "Optional DNS Records" according to their documentation:
The CNAME record is necessary for tracking opens, clicks, and unsubscribes.
Domain Verification & DNS
Return path sender score
What does the section Domain Verification & DNS in Mailgun show you?
Do you have the MX records set?
i had the same problem with sendgrid
Some of the mail providers allow you to use a dedicated ip, its generaly in premium / business plan, it prevents other people to use this ip to send "junk mail".
If you send it directly from your ip, check your sender score with https://senderscore.org/lookup.php
You seem savvy and have reviewed https://help.mailgun.com/hc/en-us/articles/202741290-Email-deliverability-tips, no doubt. Assuming that is the case, you will start to enter the tricky realm of "why is it spam?", not "what is wrong with Mailgun?".
Does your domain have spf/txt records? Do those records have mailgun added to them? Are you sending to an ATT/SBCglobal address (that ISP is notorious for blocking everything from mailgun). Can you get a dedicated IP from mailgun? These are things that enhance the reputation of your sending.
You may need to work with mailgun support on this specific issue.
Since you indicate that "gmail is fine" I am going to now turn to your question about Outlook. This program has its own default spam handling, as do programs like Mac Mail. For outlook you can manually add your system to the "safe senders" (https://blogs.technet.microsoft.com/office_for_mac_support_team_blog/2015/06/19/junk-mail-in-outlook-for-mac-for-office-365/) but that is not a global solution. These settings do not help all the other users in the world who have outlook so you will need to go back to steps that improve your reputation as a sender. That sends you back to working with Mailgun to improve your reputation.

Bulk email sending usiing node.js

I am trying to make a small dashboard where i can send bulk email using my own SMTP servers. I want to use node for this, can anyone guide from where to start i want to send mails from different SMTP servers.
A most common way to send email in Node is using Nodemailer. It has an excellent documentation.
You can use it to send email using any SMTP servers and there are a lot of preconfigured ways to send using Gmail or other specialized transports.
The available transports are - from the README:
nodemailer-mailgun-transport for sending messages through Mailgun's Web API
nodemailer-mandrill-transport for sending messages through Mandrill's Web API
nodemailer-pickup-transport for storing messages to pickup folders
nodemailer-sailthru-transport for sending messages through Sailthru's Web API
nodemailer-sendgrid-transport for sending messages through SendGrid's Web API
nodemailer-sendmail-transport for piping messages to the sendmail command
nodemailer-ses-transport for sending messages to AWS SES
nodemailer-sparkpost-transport for sending messages through SparkPost's Web API
nodemailer-stub-transport is just for returning messages, most probably for testing purposes
nodemailer-wellknown for sending messages through one of those many supported services
nodemailer-postmark-transport for sending messages through Postmark's Web API
add yours (see transport api documentation here)
Here is a simple usage example in the Nodemailer GitHub repo
var nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass#smtp.gmail.com');
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Fred Foo 👥" <foo#blurdybloop.com>', // sender address
to: 'bar#blurdybloop.com, baz#blurdybloop.com', // list of receivers
subject: 'Hello ✔', // Subject line
text: 'Hello world 🐴', // plaintext body
html: '<b>Hello world 🐴</b>' // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
See:
https://nodemailer.com/
https://github.com/nodemailer/nodemailer
For bulk mailing it's much better to use a service like Mailgun or Mandrill because doing bulk mailing yourself using SMTP it's a lot of hassle to make sure that your emails are going through spam filters and that you are not blacklisted for sending too much email, that you don't exceed any limits of your ISP etc. Sending emails is more complicated than people usually think and with prices like $0.0001 per email in Mailgun it's also dirt cheap.

How to send personal messages using socket io and node express

im implementing a chat system where you can send personal messages to a particular person connected to a shocket.
first im putting the username as a key along with the shocket.id to a jason called "connectedUser".
socket.username=loggeduser;
connectedUser[socket.username]=socket.id;
Then im using this code send a personal message
socket.broadcast.to(connectedUser[usersnametobesent]).emit('chat', { message:result});
this works perfectly but whenever i use this it only shows the message on receiver's side but not the clients. i need it to be shown in both sender and receiver's side.
You will need to emit the same thing to the sender.
socket.emit('chat', {});
That function will only send to just that socket.
Why not just use some DOM manipulation to add the chat message to the clients side. This will also add a better user experience for if say there was bad lag for whatever reason, your sender would see the message he sent instantly and not rely on waiting to see his message return from the socket?
Here is a nice little cheat sheet for sockets:
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('message', 'nice game');
// sending to all clients in 'game' room(channel), include sender
io.in('game').emit('message', 'cool game');
// sending to sender client, only if they are in 'game' room(channel)
socket.to('game').emit('message', 'enjoy the game');
// sending to all clients in namespace 'myNamespace', include sender
io.of('myNamespace').emit('message', 'gg');
// sending to individual socketid
socket.broadcast.to(socketid).emit('message', 'for your eyes only');
Credit to https://stackoverflow.com/a/10099325
To answer your comment:
In the case of sockets, if you're sending a chat message say from a client so we got client1 and client2
Assuming you have a chat window you probably have some divs and maybe some lists like ul
Client1 sends to Client2 ---> "Hello"
Client1 should immediately see the message "Hello" by using simple Dom stuff like creating a new div / li and appending it to the chat window maybe with some sort of ID to keep track of messages.
Then if the message fails to send you can find that message by id that failed, remove it and maybe append an error message instead that says whatever, message failed to send.
Meanwhile client2 is none the wiser a message had ever been sent
If you use sockets to populate messages for both users then you might run into a case where
Client1 sends to Client2 ----> "Hello"
Now maybe your server has a hiccup or the client lost connection for a second for whatever reason, he doesn't see his message yet and goes oh maybe it didn't send so he goes
Client1 sends to Client2 ----> "Hello"
Client1 sends to Client2 ----> "Hello"
Client1 sends to Client2 ----> "Hello"
Still nothing, he does it 20 more times.
Suddenly the server or whatever unlocks and sends 300 hello messages both clients are spammed.
Sorry for formatting I'm on mobile.
Simply,
This is what you need :
io.to(socket.id).emit("event", data);
whenever a user joined to the server,socket details will be generated including ID.This is the ID really helps to send a message to particular people.
first we need to store all the socket.ids in array,
var people={};
people[name] = socket.id;
here name is the reciever name. Example:
people["ccccc"]=2387423cjhgfwerwer23;
So, now we can get that socket.id with the reciever name whenever we are sending message:
for this we need to know the recievername.You need to emit reciever name to the server.
final thing is:
socket.on('chat message', function(data){
io.to(people[data.reciever]).emit('chat message', data.msg);
});
Hope this works well for you.!!Good Luck

Resources