Email Not Being Sent

default discord avatar
11 months ago
1 1

Bug Report

Payload's payload.sendEmail functionality not working.

Current Behavior

Email not being sent

Expected Behavior

Email Should be sent

Possible Solution

Steps to Reproduce

I can't say anything on payloads internal email configuration but the fs module error i'm getting can be reproduced with the below instructions.

  1. install the package: @sendgrid/mail
  2. then: sgMail.setApiKey(process.env.SENDGRID_API_KEY);

Detailed Description

I was trying to work out payload CMS's email feature but not able to make it work. First i tried the SMTP with SendInBlue service, added the SMTP in .env and then configured the payload.init({}) function as instructed in documentation. I was sending email once a user create a new user account - hook - beforeChange.

The email i tried to send does not recieved by the recipient but was there at SendInPlus Dashboard (not sure why). Then i tried the integration with SendGrid. Configured the paylaod as instructed in documentation but still it does not sent any email to recipient. After all that i tried to make it work manually as instructed in SendGrid Documentation using @sendgrid/mail NPM package, it only work 1 time and after that started giving some fs module errors.

These are the errors i'm getting in console!

ERROR in ./node_modules/@sendgrid/client/node_modules/@sendgrid/helpers/classes/attachment.js 9:11-24
Module not found: Error: Can't resolve 'fs' in 'C:\WebDevelopment\MyProjects\CMS\payload_auth_template\node_modules@sendgrid\client\node_modules@sendgrid\helpers\classes'

ERROR in ./node_modules/@sendgrid/mail/node_modules/@sendgrid/helpers/classes/attachment.js 9:11-24
Module not found: Error: Can't resolve 'fs' in 'C:\WebDevelopment\MyProjects\CMS\payload_auth_template\node_modules@sendgrid\mail\node_modules@sendgrid\helpers\classes'

I only have one collection configured Users.ts

  • discord user avatar
    Payload Team
    11 months ago

    OK so here's some info for you.

    Out of the box, Payload does not send email to your users' inboxes. It comes configured with Ethereal Email which allows you to test and debug your email, but to see the emails, you need to log into Ethereal Email. See the docs here regarding how you can log your Ethereal credentials to log in:

    Now, for configuring real transports. I'm going to guess that your SendInBlue configuration was wrong if the email showed up in the SendInPlus dashboard but not actually delivered. This could be due to MX records or similar not being correctly configured, causing your email to get either blocked or marked as spam. Email is a fickle beast, I'm afraid..

    Now, for the last fs issue. I took a look at your config and I know what's happening here. Basically, you're importing server-only modules into your Users collection, and server-only modules will break Webpack / the admin bundle unless you alias them.

    I'd put your whole beforeChange hook into a separate file, and then alias that file so it does not get included in the admin bundle. This will fix your issue.

    See the docs here for more:

    I'm gonna convert this over to a discussion but I am happy to continue helping here!

    2 replies
  • default discord avatar
    11 months ago

    Thanks for your reply, I have setup an alias as instructed in the documentation and now its working fine (emails are being sent to inbox) but i'm getting some error in terminal:

    webpack built e3cd7ce2359bb2c12536 in 2090ms
    WARNING in ./src/collections/Users.ts 57:10-19
    export 'sendEmail' (imported as 'sendEmail') was not found in './hooks/SendEmail' (possible exports: default)

    webpack compiled with 1 warning
    Watchpack Error (initial scan): Error: ENOTDIR: not a directory, scandir 'C:\WebDevelopment\MyProjects\CMS\payload_auth_template\src\collections\hooks\SendEmail.ts'
    [08:54:22] INFO (payload): Connected to Mongo server successfully!

    Everything is working fine but how can i get rid of there errors.


  • default discord avatar
    3 months ago
              alias: {
                SendEmail: mockModulePath,
                fs: mockModulePath

    This resolved similar error message for me.

Open the post
Continue the discussion in GitHub
Like what we're doing?
Star us on GitHub!


Connect with the Payload Community on Discord



Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.