Trying to set up a contact form with nodemailer. Here's what's in my app.js:
// EMail configuration
var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "myemailaddress",
pass: "xxx"
}
});
// CONTACT FORM
app.get('/contact', function (req, res) {
res.render("contact");
});
app.post('/contact', function (req, res) {
var mailOptions = {
from: req.body.email, // sender address
to: "myemailaddress", // list of receivers
subject: req.body.subject, // Subject line
text: req.body.message, // plaintext body
}
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // shut down the connection pool, no more messages
});
res.render("contact", { success: "building web app" });
});
And my contact.jade template looks like this:
form#contact-form(action="/contact", method="POST")
div.span5
p Full name:
input#name(type="text", name="name")
p Email address:
input#email(type="email", name="email")
p Subject:
input#subject(type="text", name="subject")
p Message:
textarea#message(type="text", name="message", rows="5")
p: button(type="submit") Send message
The email now works, but comes from myemailaddress rather than the one I enter into the email field on the template. Any ideas
Gmail and many other email services don't allow you to send messages with various FROM field.
you can use postmark, they provide an excellent api for sending emails and there is a node module for it (https://npmjs.org/package/postmark)
Related
First time trying to get nodemailer to work, and it does work, but only if the sending email address is my email address... I currently have this code:
app.post('/', function (req, res) {
const transporter = nodemailer.createTransport({
service: 'Hotmail',
auth: {
user: 'myemail#hotmail.co.uk',
pass: 'XXXX'
}
});
const mailOptions = {
from: req.body.address, // sender address
to: 'myemail#hotmail.co.uk', // list of receivers
subject: req.body.address,
html: req.body.message // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log(req.body);
console.log('Message %s sent: %s', info.messageId, info.response);
res.render('index');
});
If i put in a random email address in the form and send it, I get an Error: Message Failed. Not sure if i'm missing something really obvious with this but at the moment the sender address has to match the receiver (or it could be my auth.user email) for it to work.
Has anyone had trouble with this before / can shine some light on this? Thanks
So i think i've fixed it. It doesn't seem to let emails be sent from someone who isn't authorised, so I have set the sender and receiver to my email address, and used the users email address from the form in the body of the email.
const mailOptions = {
from: 'myemail#hotmail.co.uk', // sender address
to: 'myemail#hotmail.co.uk', // list of receivers
subject: req.body.address,
text: `${req.body.address} - ${req.body.message}` // html body
};
I'm using Express for my website, and I've just created a contact form with nodemailer. Now I want to use a flash message to show on the contact page after the form is submitted and redirected back to the contact page.
But I'm struggling on how to use this
This is where I post the form with Nodemailer:
router.get('includes/contact', function(req, res) {
res.render('contact',{title:'Contact'});
});
//route to send the form
router.post('/contact/send', function(req, res) {
var transporter = nodeMailer.createTransport({
service : 'Gmail',
auth : {
user: xxxxxxx,
pass: xxxxxxx
}
});
var mailOptions = {
from: req.body.name + ' <' + req.body.email + '>',
to: 'xxxxxx#xxx.com',
subject:'Test subject',
text:'Test from '+ req.body.name+' Email: '+req.body.email+'Test: '+req.body.website+'Message: '+req.body.message,
html:'<p><ul><li>Test from Naam: '+req.body.name+'</li><li>Email: '+req.body.email+'</li><li>Test: '+req.body.website+'</li><li>Message: '+req.body.message+'</li></ul>'
};
transporter.sendMail(mailOptions, function (err, info) {
if(err) {
console.log(err);
res.redirect('/#contact');
} else {
console.log('Message send');
res.redirect('/#contact');
}
});
});
And my contactform:
.wrap
.contact-block
.title Contact
form(method="post", action="contact/send")
label Name:
input(type="text", name="name", placeholder="Voer uw naam in")
label Email:
input(type="email", name="email", placeholder="Voer uw emailadres in")
label Soort website
select(type="select", name="website")
option Website
option CMS Website
option Webshop
label Bericht:
textarea(name="message", placeholder="Bericht")
button.btn-regular(type="submit") Verzenden
While calling api for submitting the form on this api '/contact/send'. change code as following:
transporter.sendMail(mailOptions, function (err, info) {
if(err) {
console.log(err);
res.status(500).send({'status':1001});
} else {
console.log('Message send');
res.status(200).send({'status':1000});
}
});
This will return json as response with appropriate http status.Than according to your status in response you can show hide flash message.No need to redirect on contact submit form.
I'm trying to setup a simple contact form using nodemailer. All i'm trying to do is send an email to my email from the contact form. My email is "myemail#mac.com"
When I do my axios post I get an error in the data object saying: 550 5.7.0 From address is not one of your addresses. but yet the status:200 and statusText:"OK"
When I use the same from email as the icloud email "myemail#mac.com" then it works ? I dont see anything where it says you have to use the same from address as the service address ?
Axios post:
const request = axios.post('http://localhost:3002/send', {'name':John Doe,'email':meme#gmail.com'});
request.then((result)=>{
console.log("request = ", result);
});
Error message from console.log("request = ", result);
error:{
code:"EMESSAGE",
command:"DATA",
response:"550 5.7.0 From address is not one of your addresses.",
responseCode:550
}
nodemailer is my node.js
const transporter = nodemailer.createTransport({
service: "iCloud",
auth: {
user: "myemail#mac.com",
pass: "myemailpassword"
}
})
app.use('/send', function(req, res){
var message = {
from: req.body.email,
to: 'myemail#mac.com',
subject: 'Message From Portfolio Contact Form',
//text: 'Plaintext version of the message',
html: '<p>'+req.body.description+'</p>'
};
transporter.sendMail(message, function(error, info){
if(error){
res.json({error: error});
}else{
res.json({success: info.response});
};
});
})
You configure nodemailer transport with iCloud Service and you are trying to send a mail with a gmail adresse.
from: req.body.email, // meme#gmail.com
to: 'myemail#mac.com'
Which logically produces the error:
response:"550 5.7.0 From address is not one of your addresses."
You probably want to do the inverse.
I am getting Recipient Error: Can't send mail - no recipient defined error while sending an email using npm node mailer. I am consulting the example as in the documentation. consider abc#gmail.com with my real email.
var emailAddress = "abc#gmail.com";
mailOptions = {
from: "Admin <abc#gmail.com>", // sender address
to: emailAddress, // list of receivers
subject: "Hello", // Subject line
text: "hello", // plaintext body
html: "<b>Hello</b>" // html body
}
smtpTransport = nodeMailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "abc#gmail.com",
pass: "123"
}
});
smtpTransport.sendMail(mailJson, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
// if you don't want to use this transport object anymore, uncomment following line
smtpTransport.close(); // shut down the connection pool, no more messages
});
The following advise would be useful (for others users):
mailJson is your configuration called mailOptions.
Would return in a promise your result, which is so useful when you're sending an email in an async code.
So, your code would be the following:
mailOptions = {
from: "Admin <abc#gmail.com>", // sender address
to: emailAddress, // list of receivers
subject: "Hello", // Subject line
text: "hello", // plaintext body
html: "<b>Hello</b>" // html body
}
smtpTransport = nodeMailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "abc#gmail.com",
pass: "123"
}
});
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (error, info) => {
error ? reject(error) : resolve(info);
});
});
});
If no solution work for you
Go to that link and active less secure than try to send mail
enter link description here
After watching this video http://vimeo.com/26963384 on vimeo which was about how kue works,i have to ask how the code worked without installing any package to help send emails like node mailer.
Does the latest version of node js come with the capability to send emails?.
The code used looks like
jobs.create('email', {
title: 'welcome email for tj'
, to: 'tj#learnboost.com'
, template: 'welcome-email'
}).save();
In the presentation,no package to send emails was added.
var nodemailer = require('nodemailer');
// create SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'xxx#gmail.com',
pass: '******'
}
});
// transporter object for all e-mails
var mail = {
from: 'XXX XXXX <XXX#gmail.com>', // sender address
to: 'XXX#hotmail.com, XXX#gmail.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(mail, function (error, info) {
if (error) {
return console.log('Error : ' + error);
}
console.log('Mail sent: ' + info.response);
});