payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.09k stars 1.44k forks source link

Email Not Being Sent #1192

Closed shubhamkashyapdev closed 1 year ago

shubhamkashyapdev commented 1 year ago

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 Github: https://github.com/shubhamwebdesign/payload_auth_starter

jmikrut commented 1 year 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:

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!