Payload's payload.sendEmail functionality not working.
Email not being sent
Email Should be sent
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.
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
Github: https://github.com/shubhamwebdesign/payload_auth_starter
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:
https://payloadcms.com/docs/email/overview#mock-transport
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:
https://payloadcms.com/docs/admin/webpack#aliasing-server-only-modules
I'm gonna convert this over to a discussion but I am happy to continue helping here!
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.
Github: https://github.com/shubhamwebdesign/payload_auth_starter
alias: {
...config.resolve.alias,
SendEmail: mockModulePath,
fs: mockModulePath
},
This resolved similar error message for me.
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.