Does Vercel Allow Mailing Systems?
Unanswered
Channel catfish posted this in #help-forum
Channel catfishOP
Hey everyone, i'm not sure if i'm missing something here, but essentially what i'm attempting to do is, once the user has paid, the following post request will be expected:
app.post('/after-payment-form', csrfProtection, async (req, res) => {
console.log('Request Headers:', req.headers);
console.log('Request Body:', req.body);
console.log('CSRF Token Received:', req.body._csrf);
const { error, value } = formSchema.validate(req.body);
if (error) {
console.error('Validation Error:', error.details);
return res.status(400).send(error.details.map(detail => detail.message).join(", "));
}
if (value.links && !isSafeUrl(value.links)) {
return res.status(400).send('Invalid URL in links field');
}
const emailData = {
from: 'no-reply@company.com',
to: value.email,
subject: 'Payment Confirmation',
html: '<p>Hey! Payment has been received.</p>'
};
const OfficialDataSender = {
from: 'no-reply@companyadmin.com',
to: "myemail.com",
subject: 'New Data.',
html: '<p>New form data:</p><pre>' + JSON.stringify(value, null, 2) + '</pre>'
};
try {
await sendEmail(emailData);
await sendEmail(OfficialDataSender);
res.status(200).json({ message: 'Form and email processed successfully', formData: value });
} catch (error) {
console.error('Failed to send email:', error);
res.status(500).send('Failed to send email');
}
});
module.exports = app;5 Replies
Channel catfishOP
(The sendEmail function is called from another file which is my nodemailer but essentially a quick overview looks like this) :
So whats the issue exactly? : Thing is, this works just fine locally, the email does get sent just fine when hosted locally (not with vercel dev since it gives me some cors issues for some reasons so i used node instead). However on my "production" app that is hosted on vercel itself, everything goes exactly as expected, it even says the email was sent just fine when i tried to log it:
Tysm!
const sendEmail = async (emailData) => {
let transporter = await createTransporter();
const mailOptions = {
from: 'no-reply@somethingidk.com <myemail@gmail.com>',
to: emailData.to,
subject: emailData.subject,
text: emailData.text,
html: emailData.html,
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
};So whats the issue exactly? : Thing is, this works just fine locally, the email does get sent just fine when hosted locally (not with vercel dev since it gives me some cors issues for some reasons so i used node instead). However on my "production" app that is hosted on vercel itself, everything goes exactly as expected, it even says the email was sent just fine when i tried to log it:
Form Submission Success:
Object { message: "Form and email processed successfully", formData: {…} } however the email doesn't get sent at all. It works locally, the email does get sent, but on the actual app on vercel it doesn't, and i'm not quite sure why tbh, was wondering if anyone could help out, maybe it's a hidden issue? I've checked the requests in the logs and everything looked fine.Tysm!
Sun bear
are you hosting a mailserver? if you are the email should go through as I don't know of any restrictions vercel would have on smtp requests.
@Sun bear are you hosting a mailserver? if you are the email should go through as I don't know of any restrictions vercel would have on smtp requests.
Channel catfishOP
Thanks for the answer, i did some more research, technically nodemailer can work on a basic setup but it's preferable to use a service that works well with serverless functions, i'll give SendGrid a try. https://www.reddit.com/r/nextjs/comments/14jq88e/can_i_use_nodemailer_in_vercel/
Sun bear
I have somewhat of a similar system. Mailserver hosted on a VPS, contact form on a next.js website hosted on vercel and a nodemailer server action that sends me an email when someone fills the form and everything works perfectly. I don't know why it does not work for you...