Nodemailer - email send but not receive - email

I'm build an API with feathersjs and I need to send an email with an attachment.
The email seems to be send but I receive nothing.
In my mail.service.js
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: 'smtp.office365.com',
port: 587,
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'gil.felot#myaccount.com',
pass: 'MyPassWord'
}
});
// Check the connection to the service.
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take our messages');
}
});
Then in my hook
hook => {
const file = hook.params.file;
const email = {
from: 'gil.felot#myaccount.com', // sender address
to: 'mygmailaccount#gmail.com', // list of receivers
subject: 'Test Nodemailer', // Subject line
// text: req.body.text, // plaintext body
html: '<b>Hello world 🐴</b>', // html body
attachments: [
{
filename: file.originalname,
contents: new Buffer(file.buffer, 'base64'),
encoding: 'base64'
}
]
};
return hook.app.service('mail').create(email)
.then(function (result) {
console.log('Sent email', result);
}).catch(err => {
console.log(err);
});
}
then I got
Server is ready to take our messages
Sent email
Object {from: "gil.felot#myaccount.com", to: "mygmailaccount#gmail.com", subject: "Test Nodemailer", html: "Hello world 🐴"}
I have no idea how to check where the problem come from.

I was missing the from part while creating a mail while I was able to send mail via google smtp but my own smtp was failing with the above configuration
Was working with google
var mailOptions = { to: email, subject: 'Forgot Password', html: mailData };
Working with my smtp as well:
var mailOptions = { from: 'serverName.com', to: email, subject: 'Forgot Password', html: mailData };
Consider adding name while defining nodemailer configuration as well
let transporter = nodemailer.createTransport({
name: 'example.com' // <= Add this
host: 'smtp.example.email',
port: 587,

Ok I figure it out !
I needed to add the transporter.sendMail() inside the mail.class.js to trigger this action when I call hook.app.service('mail').create(email)
Working and the attachement file that is 0 byte in the mail but the good size inside my variable.

For me this was what I realized; if the html does not have the html tags, then the email is not sent. i.e.
This template will work
<html>
<body>
Hello and welcome
</body>
</html>
This template will not work:
<body>
Hello and welcome
</body>
This is especially when sending to office365, refer to this other question here:
Nodemailer doesn't send emails to outlook.office365 accounts

Related

Send emails with Nodemailer from custom domain email address (but the custom domain only forwards the emails) | Node.js

I want to send a email from my custom domain address office#mycompany.com but this mail address (that was set up using Shopify) is only forwarding the emails that come in. Is it possible to send a email from this mail address (like many applications do).
I googled around and found sth. that I have to add a DNS and a MX Record to my domain. Is that right and how can I send emails through those configurations?
let transporter = nodemailer.createTransport({
host: 'mycompany.com',
port: 465,
secure: true,
auth: {
user: 'office#mycompany.com',
pass: ?
}
});
let mailOptions = {
from: '"Mycompany" <office#mycompany.com>', // sender address
to: recipient, // list of receivers
subject: "Test", // Subject line
html: fileToSend // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
} else {
console.log('Message %s sent: %s', info.messageId, info.response);
}
});
Warmly regards

SMTP email slow to send with nodemailer and amazon SES

I'm trying to send emails inside next.js api functions. I have the following code:
import nodemailer, { SentMessageInfo } from "nodemailer";
export default async ({ to, subject, text, html }: Props) => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "email-smtp.us-east-1.amazonaws.com",
port: 465,
secure: true,
auth: {
user: process.env.AWS_SES_USERNAME,
pass: process.env.AWS_SES_PASSWORD,
},
});
console.log("Sending email to", to);
// send mail with defined transport object
let info: SentMessageInfo = await transporter.sendMail({
from: '"Me" <help#myemail.io>', // sender address
to,
subject,
text,
html: html,
});
console.log("Message sent: %s", info.messageId);
return info.messageId;
};
Sometimes this can take minutes to send but I have no idea why. How would one go about debugging this? Is it possible that my Amazon SES SMTP server is throttling me? I'm not even sending emails that frequently.

Using nodemailer with G suite

I have tried using nodemailer with gmail and it works. I tried to use the same code with my G suite name#mydomain.com email and there's bad credentials error. Is there an easy way to use nodemailer with G suite? because I want my business mail to be seen as sender.
this is the code I am using:
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'name#business.com',
pass: 'password'
}
});
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 <aegegwgwg#gmail.com>', // Something like: Jane Doe <janedoe#gmail.com>
to: dest,
subject: 'I\'M A PICKLE!!!', // email subject
html: `<p style="font-size: 16px;">Pickle Riiiiiiiiiiiiiiiick!!</p>
<br />
<img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
` // email content in HTML
};
// returning result
return transporter.sendMail(mailOptions, (erro, info) => {
if(erro){
return res.send(erro.toString());
}
return res.send('Sended');
});
});
});

Getting error sending email in nodejs with emailjs

[error: can't set headers after they are sent.]
createCredentials() is deprecated, use tls.createSecureContext instead
{[Error: bad response on command '-']
code:2
smtp : '550 5.3.4 Requested action not token; To continue sending messages,
please sign in to your account.\n}
I have been trying to send email in nodejs with emailjs and nodemailer but i keep on getting the error above.
transportEmail: email.server.connect({
user: "ghConnectUs#outlook.com",
password:"******",
host: "smtp-mail.outlook.com",
tls: {ciphers: "SSLv3"}
})
note: i have include all modules.
i'm hoping somebody can point me to the right path. i just want to send me using outlook or gmail in node app.
Below is the code for sending email from nodejs app (through Gmail):
Using Nodemailer v1.3.4:
var nodemailer = require("nodemailer");
var transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "email_id_of_gmail_account",
pass: "password_of_gmail_account"
}
});
var mailOptions = {
from: 'sender_email_id', // sender address
to: 'receiver_email_id, some_other_email_if_requierd', // list of receivers
cc: 'cc_email_id'
subject: 'subject text', // Subject line
text: 'body plain text', // plaintext body
html: '<b>body html</b>' // html body
};
var sendEMail = function () {
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
}
});
};
sendEmail();
Note: if you are using Nodemailer version 0.7 or lower, then transporter object will created like this:
var transporter = nodemailer.createTransport('SMTP', {
service: "Gmail",
auth: {
user: "email_id_of_gmail_account",
pass: "password_of_gmail_account"
}
});
Using emailJs v0.3.16:
var email = require("/node_modules/emailjs/email.js");
var server = email.server.connect({
user: "emailId_of_gmail_account",
password: "password_of_gmail_account",
host: "smtp.gmail.com",
ssl: true // in case outlook, use "tls: {ciphers: "SSLv3"}"
});
var message = {
text: "body text",
from: "senderName <sender's_email_id>",
to: "receiverName <receiver_email_id>",
subject: "subject text",
attachment: // optional
[
{data: "<html>i <i>hope</i> this works! html </html>", alternative: true},
{path: "path/to/file.zip", type:"application/zip", name:"renamed.zip"}
]
};
var sendEMail = function () {
server.send(message, function (err, message) {
console.log(err || message);
});
};
sendEmail();

node.js send email successful, but mail not found

I am using https://github.com/eleith/emailjs for my node.js application
After setting up the configuration properly, when I send email, I get the successful message in my log, but I do not see any mails either in the inbox or spambox :-(..
I using gmail, smtp.gmail.com, ssl:true, port:465.
email.send({...},function(err, message) {
if (err) {
console.log('Error sending email : ', err);
}
else {
console.log('Email SUCCESSFULLY sent', message);
}
^[[32m[2011-10-13 06:53:28.758] [INFO] console - ^[[39mEmail SUCCESSFULLY sent {
attachments: [],
html: null,
header:
{ 'message-id': '<1318488805460.5532#Abcd-PC>',
from: 'xxxxx#gmail.com',
to: 'yyyyy#gmail.com, ',
cc: 'zzzzz#gmail.com',
subject: 'Test mail from emailjs' },
content: 'text/plain; charset=utf-8',
text: 'Testing sending email' }
I use the following code for my emails in node.js (using emailjs)
var server = email.server.connect({
user: 'your#gmail.com',
password: 'gmailPass',
host: 'smtp.gmail.com',
ssl: true
});
server.send({
text: 'hello world',
from: 'obama#state.gov',
to: 'someone#else.com',
subject: 'just a subject here'
}, function(err, message) {
if (err) {
// err
} else {
// ok
}
});
Problem was comma(,) "yyyyy#gmail.com," in the 'to' email. I was trying to add multiple ids separated by commas(,) and that created the problem..
In the documentation, they have mentioned though
**"someone <someone#gmail.com>, another <another#gmail.com>"**
may be it expects the email format to be in the form they mentioned.