🐛 BUG: mailgun.js able send email in localhost, but failed in Cloudflare page and it returns Cannot convert object to primitive value
Which Cloudflare product(s) does this pertain to?
Pages
What version(s) of the tool(s) are you using?
3.17.1 [wrangler], 3.8.2 [nuxt], 8.0.6 [mailgun.js]
What version of Node are you using?
21.2.0
What operating system are you using?
Windows
Describe the Bug
I have a issue related to mailgun.js. I tried to send an email locally, and it works perfectly. However, on Cloudflare Page, it returns a status code 500 and an error message: 'Cannot convert object to primitive value.' Could you help me identify which part is causing the issue?
here is my code:
import Mailgun from 'mailgun.js'
import FormData from 'form-data'
export default defineEventHandler(async (event: any) => {
const mailgun = new Mailgun(FormData)
const mg = mailgun.client({ username: 'api', key: process.env.MAILGUN_KEY || '' })
const body = await readBody(event)
const data = {
from: `${body.name} <${body.email}>`,
to: process.env.MAIL_TO,
subject: body.subject,
text: body.message,
html: `<html><body><p>Name : ${body.name}</p><p>Email : ${body.email}</p><p>Phone : ${body.phoneNumber}</p><p>Message : ${body.message}</p></body></html>`
}
return mg.messages
.create(process.env.MAIL_DOMAIN || '', data)
.then((response: any) => {
return response
})
.catch((err: any) => {
throw createError({
statusCode: err
})
})
})
and here is my package.json
{
"name": "nuxt-app",
"private": true,
"type": "module",
"scripts": {
"build": "NITRO_PRESET=cloudflare-pages nuxt build",
"dev": "nuxt dev --port=3002",
"generate": "nuxt generate",
"preview": "nuxt preview",
"postinstall": "nuxt prepare",
"pages:dev": "wrangler pages dev --compatibility-date=2023-11-21 --proxy 3000 -- npm run dev",
"pages:deploy": "npm run build && wrangler pages deploy ./dist"
},
"devDependencies": {
"@babel/eslint-parser": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"@nuxt/devtools": "latest",
"@nuxtjs/axios": "^5.13.6",
"@nuxtjs/eslint-config-typescript": "^12.1.0",
"@nuxtjs/eslint-module": "^4.1.0",
"@types/js-cookie": "^3.0.6",
"eslint": "^8.54.0",
"eslint-plugin-nuxt": "^4.0.0",
"eslint-plugin-vue": "^9.18.1",
"eslint-plugin-vuetify": "^2.1.0",
"form-data": "^4.0.0",
"js-cookie": "^3.0.5",
"libphonenumber-js": "^1.10.51",
"nuxt": "^3.8.2",
"nuxt-security": "^0.14.4",
"sass": "^1.69.5",
"vite-plugin-eslint": "^1.8.1",
"vite-plugin-vuetify": "^1.0.2",
"vue": "^3.3.8",
"vue-router": "^4.2.5",
"vuetify": "^3.4.3",
"wrangler": "^3.17.1"
},
"dependencies": {
"@mdi/font": "^7.3.67",
"@stripe/stripe-js": "^2.2.1",
"@types/cookie-parser": "^1.4.6",
"@types/graylog2": "^0.2.5",
"dotenv": "^16.3.1",
"graylog2": "^0.2.1",
"mailgun.js": "^8.0.6",
"vee-validate": "^4.12.2",
"vue-recaptcha": "^2.0.3",
"vuex": "^4.0.2"
}
}
Please provide a link to a minimal reproduction
No response
Please provide any relevant error logs
No response
Currently running in exactly the same issue. @joelee1992 did you find a solution to this problem?
It depends how you're running it locally: if with nuxt (like younpm run dev/preview), then your Pages app is running in node.js and this could be a node compat issue with Cloudflare's runtime (workerd); if with wrangler pages dev (like npm run pages:dev), then there is some other discrepancy with our local and deployed environments which we'll look to fix.
Can you confirm how you ran it locally please?
Also experiencing with SES
Edit: checking out https://github.com/cloudflare/workers-sdk/issues/2081
This is a limitation of the Workers runtime, which doesn’t run your Worker in Node.js, which the Mailgun SDK expects.
We've seen various issues with Mailgun.js in the past (see https://github.com/cloudflare/workers-sdk/issues/1613, for instance), and people have had success with using the Mailgun REST API directly, which is what we'd recommend in this case. With a quick search I found a blog post that seems to have an example of how to make that work in Workers (although fair warning I haven't actually tried to run that code): https://guido.io/posts/sending-email-from-cloudflare-workers/