Netlify email fetch returning error404 (or net::ERR_FAILED when / removed)

PLEASE help us help you by writing a good post!

Site name: lawrence1.netlify.app

When i use netlify dev the code works fine and I am able to send emails, however when I netlify builds it (through github) I get errors

What errors exactly?

I get the error “POST https://lawrence1.netlify.app/.netlify/functions/emails/contact-email net::ERR_FAILED” when I attempt to send using the code URL=${process/env.REACT_APP_URL},netlify/funcitons/emails/contact-email which results in the URL https://lawrence1.netlify.app/.netlify/functions/emails/contact-email when console logged.

I get the error “POST https://lawrence1.netlify.app/.netlify/functions/emails/contact-email 404” when I change the code to URL =${process.env.REACT_APP_URL}/.netlify/functions/emails/contact-email
This URL when console logged is https://lawrence1.netlify.app/.netlify/functions/emails/contact-email

The difference being the “/” before .netlify

Let’s try this some other way. Could you explain what exactly are you trying? Because, based on your setup, I feel you think you’re solving a problem, but you’re probably not.

I am attempting to send an email to myself via the sendgrid integration with netlify from my website

Hi @Lawrence1 , have you given this doc a read?

Also if its an issue with your code fan you please share your repo if possible?

Hey @hrishikesh @SamO, I think I’m seeing the same issue as OP.

I followed the email integration docs to write a function that makes a fetch request to ${process.env.URL}/.netlify/functions/emails/share to send an email via Sendgrid. The function is called from my frontend.

This works as expected on my machine (via netlify dev), but fails on the live site. The call to my function works fine, but the fetch to the email handler returns 404: Not found. I’ve tried calling the email handler’s endpoint from my browser and got the normal response – so it’s clearly being deployed, but my function for whatever reason can’t see it. This only started happening when I moved my site to a monorepo so I think it’s related to that, but I haven’t managed to find the cause.

Happy to share my repo / site details if needed.

I’ve done some debugging and the problem is not that my function can’t see the email handler, but that the email handler can’t see the email templates (The 404 I was seeing is actually Email templates directory ./emails does not exist from the email handler).

Hi @maxk so did you get this working or do you still need some help?

Still not working. Seems to me like the Netlify Email Integration isn’t bundling the email templates with the emails function the way it should. I’ve tried setting NETLIFY_EMAIL_DIRECTORY to different values (with and without my site’s base directory, with and w/o trailing slashes etc.) and including the templates manually using an included_files directive in my netlify.toml, but no luck. Any ideas?

I just tried getting the email templates included with my own function using:

[functions]
  included_files: ["emails/**"]

and confirming the directory exists via:

fs.existsSync(process.env.NETLIFY_EMAILS_DIRECTORY)

and that doesn’t work either – so maybe this isn’t a problem specific to the Netlify Emails Integration at all, and more of a problem with the included_files directive in monorepos in general? Or I’m still missing something obvious…

Hi @maxk,

Thanks for reporting this. Could I ask you confirm the following:

  • What framework are you using?
  • What version of the email plugin appears on your build log?
  • When running the email preview UI, does it alert you about any missing configuration?

If it looks like it’s an issue with finding the emails directory, it might help if you give us an idea of the folder structure so we can understand what the issue might might be.

Hey @thorleylt, thanks for the reply!

  • gatsby@5.4.2
  • @netlify/plugin-emails@1.0.0
  • When I go to localhost:8888/.netlify/functions/emails (no trailing slash) I see the normal preview and no errors, but on /.netlify/functions/emails/(with a trailing slash) I get “The email template does not exist in your specified directory ./emails” – weird.

My folder structure looks like this:

websites
├─ site-a
├─ site-b
    ├─ netlify.toml
    ├─ emails/share
    │   ├─ index.html
    ├─ netlify/functions
        ├─ sendShare.ts 

sendShare.ts is triggered from the frontend and makes a POST request to ${process.env.URL}/.netlify/functions/emails/share to send the email.

1 Like

That’s really helpful thank you. I’ve never seen this before but I’m going to try this out myself with this exact folder structure and see if I can replicate.

I’m sure it was just a typo on your previous message, but I saw you mention NETLIFY_EMAIL_DIRECTORY instead of NETLIFY_EMAILS_DIRECTORY. Just want to be extra sure that when you have tried different values without trailing slashes etc you are setting the correct variable.

In the meantime, I will carry on trying to reproduce on the assumption it’s not the above.

1 Like

Will double-check, and thank you so much for taking the time to look into this.

Hi, I’ve made some progress, and have redone how the function works. It is again working in development, however it is not working in build, with no errors returned. The link to my project repo is https://github.com/LawrenceWilliams00/portfolio
Specifically the /client folder, as the /server folder is no longer actually used for anything, and I run the netlify build through the /client as the main location

The link to your repo doesn’t work, but it sounds like you’re seeing the same issue as me. Let’s just hang back for a few days while @thorleylt et al are looking into it. (By the way sorry for hijacking your thread)

Hi @maxk Currently it looks like we are having some issues with the email integration and the Netlify adapter for Svelte Kit, meaning that the email function is being removed when the build is run. We are trying to identify the best way of fixing this and will let you know when we have a fix in place.

1 Like

Hey @SamO I’m using Gatsby, not Svelte Kit - are you saying the issues are related?

We were investigating an issue with Svelte Kit and the email integration but it appears that issue is unrelated to this one. I am able to reproduce this issue and I am currently looking into how we can fix it. Will keep you updated.

1 Like