baptisteArno / typebot.io

💬 Typebot is a powerful chatbot builder that you can self-host.
https://typebot.io
Other
7.52k stars 2.09k forks source link

Cannot get SMTP config to send email #942

Closed tecoad closed 1 year ago

tecoad commented 1 year ago

I have tried to setup several different SMTP providers to be able to send email:

Sendinblue (brevo), Postmark, Resend.com. My SMTP settings should be right since a was able to test through a SMTP connection testing system https://www.gmass.co/smtp-test.

My enviorment variables are:

SMTP_USERNAME=PM-T-outbound-_****
SMTP_PASSWORD=DQPOGSDOkszA2eItZ-naXmVW4Wl_*****
SMTP_HOST=smtp.postmarkapp.com
SMTP_PORT=587
SMTP_SECURE=false
SMTP_AUTH_DISABLED=false
NEXT_PUBLIC_SMTP_FROM='Test <test@myemail.dev>'

I cannot get it to work regardless of the service I use. All of them timeout, both locally and deployed (vercel) versions. All of them have the same error:

err:

 error>>>>> Error: queryA ETIMEOUT smtp-relay.sendinblue.com
builder:dev:     at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/callback_resolver:47:19) {
builder:dev:   errno: undefined,
builder:dev:   code: 'EDNS',
builder:dev:   syscall: 'queryA',
builder:dev:   hostname: 'smtp-relay.sendinblue.com',
builder:dev:   command: 'CONN'
builder:dev: }

And try and catch

builder:dev: [next-auth][error][SIGNIN_EMAIL_ERROR] 
builder:dev: https://next-auth.js.org/errors#signin_email_error Email(s) could not be sent {
builder:dev:   error: {
builder:dev:     message: 'Email(s) could not be sent',
builder:dev:     stack: 'Error: Email(s) could not be sent\n' +
builder:dev:       '    at Object.sendVerificationRequest (webpack-internal:///(api)/./src/features/auth/helpers/sendVerificationRequest.ts:20:15)\n' +
builder:dev:       '    at async Promise.all (index 0)\n' +
builder:dev:       '    at async email (/Users/matheus/Desktop/typebot.io/node_modules/.pnpm/next-auth@4.22.1_next@13.5.4_nodemailer@6.9.3_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/core/lib/email/signin.js:31:3)\n' +
builder:dev:       '    at async Object.signin (/Users/matheus/Desktop/typebot.io/node_modules/.pnpm/next-auth@4.22.1_next@13.5.4_nodemailer@6.9.3_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/core/routes/signin.js:115:24)\n' +
builder:dev:       '    at async AuthHandler (/Users/matheus/Desktop/typebot.io/node_modules/.pnpm/next-auth@4.22.1_next@13.5.4_nodemailer@6.9.3_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/core/index.js:260:26)\n' +
builder:dev:       '    at async NextAuthApiHandler (/Users/matheus/Desktop/typebot.io/node_modules/.pnpm/next-auth@4.22.1_next@13.5.4_nodemailer@6.9.3_react-dom@18.2.0_react@18.2.0/node_modules/next-auth/next/index.js:22:19)\n' +
builder:dev:       '    at async handler (webpack-internal:///(api)/./src/pages/api/auth/[...nextauth].ts:207:12)',
builder:dev:     name: 'Error'
builder:dev:   },
builder:dev:   providerId: 'email',
builder:dev:   message: 'Email(s) could not be sent'
builder:dev: }

I have already tried: Ports 2525, 25 and 587. Local and deployed

Also, its not clear for me the function of DEBUG=true env. Since enabling it changes not logs on the console.

Update 1:

Some behaviour that seems need to be more clarified:

So far, i had these envs:

NEXTAUTH_URL=http://localhost:3000/ NEXT_PUBLIC_VIEWER_URL=http://localhost:3001/

After some reasearch, even thought the logical would be this set to localhost, i changed to a deployed url, then set the NEXTAUTH_URL_INTERNAL=http://localhost:3000/

Now, i can see the message "A magic link email was sent. 🪄 Make sure to check your spam folder."

However email has not been sent (checked the logged messages from my postmark).

IMO, Documentation is unclear if NEXT_PUBLIC_VIEWER_URL and NEXTAUTH_URL should be deployed versions url. Github or others auth methods are working perfectly, even before NEXTAUTH_URL_INTERNAL env. This issue has been extremelly difficult to track as it seems a internal nodemailer issue and nothing seems to work. Any clue would be highly appreciated.

Update 2:

It seams that the issue I am having is the same as some people show in this recent discussion here, even though not presented much tracing details.

baptisteArno commented 1 year ago

Probably an issue with the single quote on NEXT_PUBLIC_SMTP_FROM

My config with Sendinblue:

SMTP_USERNAME=baptiste@typebot.io
SMTP_PASSWORD=...
SMTP_HOST=smtp-relay.sendinblue.com
SMTP_PORT=587
NEXT_PUBLIC_SMTP_FROM="'Typebot Notifications' <notifications@typebot.io>"
tecoad commented 1 year ago

I dont think that is the issue @baptisteArno, i have screenshot the video showing you setting up typebot from scratch:

  1. downloading repo
  2. testing smtp settings in external site
  3. installing and running on local using docker.

DO you see if I am doing anything wrong?

Error after timeout: Screenshot 2023-10-17 at 11 32 12

baptisteArno commented 1 year ago

Ok, I'm trying to reproduce with your creds

baptisteArno commented 1 year ago

I've tried on Typebot and on https://dnschecker.org/smtp-test-tool.php. It fails on both places.

As far as I know, your credentials don't work 🤔

MAS-CreativeLabs commented 1 year ago

me too I'm getting the same error when I try to send an invite

CleanShot 2023-10-18 at 02 05 00@2x

baptisteArno commented 1 year ago

Guys, this error means your SMTP credentials are not valid / working.

MAS-CreativeLabs commented 1 year ago

credentials

all of us?

MAS-CreativeLabs commented 1 year ago

I just checked my credentials and they are correctly working with other services (I use the same mail for other services) I don't understand. I there a way to have more verbose messages in the logs? I can't see anything that would help narrow my research...

baptisteArno commented 1 year ago

For gmail smtp it's another story: https://nodemailer.com/usage/using-gmail/

I suggest you use a dedicated SMTP service like AWS SES, Brevo or Sendgrid.

lazmo88 commented 1 year ago

I can confirm exactly the same issue with Postmark. Same creds are working on other services and also passes SMTP test with gmass.co/smtp-test

NEXT_PUBLIC_SMTP_FROM="'example' from@example.com"

(double quotes).

Did anyone facing this issue have any luck resolving this or more importantly, figured out the root cause?

Running Typebot on Docker.

@baptisteArno perhaps consider reopening this issue?

MAS-CreativeLabs commented 1 year ago

I can confirm exactly the same issue with Postmark. Same creds are working on other services and also passes SMTP test with gmass.co/smtp-test

NEXT_PUBLIC_SMTP_FROM="'example' from@example.com"

(double quotes).

Did anyone facing this issue have any luck resolving this or more importantly, figured out the root cause?

Running Typebot on Docker.

@baptisteArno perhaps consider reopening this issue?

Yeah @lazmo88 , I agree with you. this is an unresolved issue. I've been using the same exact credentials with other self-hosted services via docker containers flawlessly but when it comes to typebot it doesn't work. the problem is clearly linked to typebot but I don't know how to fix it.

lazmo88 commented 1 year ago

I can confirm exactly the same issue with Postmark. Same creds are working on other services and also passes SMTP test with gmass.co/smtp-test

NEXT_PUBLIC_SMTP_FROM="'example' from@example.com"

(double quotes).

Did anyone facing this issue have any luck resolving this or more importantly, figured out the root cause?

Running Typebot on Docker.

@baptisteArno perhaps consider reopening this issue?

Yeah @lazmo88 , I agree with you. this is an unresolved issue. I've been using the same exact credentials with other self-hosted services via docker containers flawlessly but when it comes to typebot it doesn't work. the problem is clearly linked to typebot but I don't know how to fix it.

I tried looking at the logs, but there was nothing there related to emails.

@MAS-CREATIVE-LABS what email provider do you use? I'm thinking it is about the .env variables and how these are formatted.

Postmark issues username/password in xxxx-yyyy-aaaa... format, could the hyphens be the issue? I'll try saving these creds within quotation marks and see if that helps. Other a bit unclear part is the EMAIL FROM config, can we just use email address or do we need to specify also the name? This double quotation wasn't really well documented.

Comparing typebot to other self hosted apps, often you can just define admin creds as env variables or you are prompted to create admin creds on when logging in the first time. Fully working SMTP isn't normally a requirement.

baptisteArno commented 1 year ago

What error do you see?

lazmo88 commented 1 year ago

What error do you see?

There's no errors in the log, but emails are not sent so I can't login. Is there a way to enable debugging for emails?

baptisteArno commented 1 year ago

Can you share the credentials to baptiste@typebot.io so that I try to reproduce?

lazmo88 commented 1 year ago

Can you share the credentials to baptiste@typebot.io so that I try to reproduce?

Sent ☑

baptisteArno commented 1 year ago

That worked for me. I received a login email 🤔

Is your VPS allowed to send email?

lazmo88 commented 1 year ago

That worked for me. I received a login email 🤔

Is your VPS allowed to send email?

I'm running Typebot on local server, I am using the same SMTP credentials for other locally hosted apps and they work fine (n8n, Chatwoot, both running on same Docker VM).

Quite puzzling to be honest. Did you test the creds on latest Docker image?

Edit: As other apps are sending emails with same creds from same server, I doubt it's a ISP/port issue.

lazmo88 commented 1 year ago

@tecoad @MAS-CREATIVE-LABS can you please advise your deployment method? Did you use Docker or other way of deploying the app?

@baptisteArno had no issues using the same creds, so question arises if there's an issue with the Docker compose config?

baptisteArno commented 1 year ago

Can you share your docker-compose file so that I try to exactly reproduce your setup?

lazmo88 commented 1 year ago

Can you share your docker-compose file so that I try to exactly reproduce your setup?

version: '3.3'

volumes:
  db-data:

services:
  typebot-db:
    image: postgres:14-alpine
    restart: always
    volumes:
      - db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=typebot
      - POSTGRES_PASSWORD=typebot

  typebot-builder:
    image: baptistearno/typebot-builder:latest
    restart: always
    depends_on:
      - typebot-db
    ports:
      - '8080:3000'
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    env_file: .env

  typebot-viewer:
    image: baptistearno/typebot-viewer:latest
    restart: always
    ports:
      - '8081:3000'
    env_file: .env
lazmo88 commented 1 year ago

And .env file

# Make sure to change this to your own random string of 32 characters (https://docs.typebot.io/self-hosting/docker#2-add-the-required-configuration)
ENCRYPTION_SECRET=XXX
DEBUG=true
DATABASE_URL=postgresql://postgres:typebot@typebot-db:5432/typebot

NEXTAUTH_URL=typebot.local:8080
NEXT_PUBLIC_VIEWER_URL=bot.local:8081

ADMIN_EMAIL=XXX@XXX.COM
# For more configuration options check out: https://docs.typebot.io/self-hosting/configuration

# Email Auth v1
SMTP_USERNAME=XXX-XXX-XXX-XXX
SMTP_PASSWORD=XXX-XXX-XXX-XXX
SMTP_HOST=smtp.postmarkapp.com
SMTP_PORT=587
NEXT_PUBLIC_SMTP_FROM="'Typebot' <typebot@XXX.XXX.COM>"
SMTP_SECURE=true
SMTP_AUTH_DISABLED=false
baptisteArno commented 1 year ago

For port 587 you should not set SMTP_SECURE to true. Do you confirm that you've also tried without SMTP_SECURE variable?

lazmo88 commented 1 year ago

For port 587 you should not set SMTP_SECURE to true. Do you confirm that you've also tried without SMTP_SECURE variable?

I have tested both, but isn't port 587 TLS/STARTTLS? So, secure flag should be true or am I missing something here?

I have also tested alternative ports specified by Postmark 25/2525/587.

To clarify, I have tried registering and logging in with the specified admin email, but none triggers email.

To be honest, I think there should be option to specify admin login in the env file and configuration of SMTP could be done on the admin dashboard with proper error/debug details if adding SMTP fails.

While above is quite significant change to whole config flow, there should be at minimum option to have more detailed debug logs available as its impossible to troubleshoot this issue without.

baptisteArno commented 1 year ago

So the issue might come from:

NEXTAUTH_URL=typebot.local:8080
NEXT_PUBLIC_VIEWER_URL=bot.local:8081

Where does it come from, how did you set up these domains?

lazmo88 commented 1 year ago

So the issue might come from:

NEXTAUTH_URL=typebot.local:8080
NEXT_PUBLIC_VIEWER_URL=bot.local:8081

Where does it come from, how did you set up these domains?

The local domains are setup with local DNS server and pointing to the Docker VM. No reverse proxy in the between.

lazmo88 commented 1 year ago

So the issue might come from:

NEXTAUTH_URL=typebot.local:8080
NEXT_PUBLIC_VIEWER_URL=bot.local:8081

Where does it come from, how did you set up these domains?

Ok, so updating the urls fixed the issue!

Any thoughts why local:port urls didn't work?

baptisteArno commented 1 year ago

Oh my bad, the format also is also invalid, it needs to start with http:// or https://. That could be the issue?

MAS-CreativeLabs commented 12 months ago

To be honest, I think there should be option to specify admin login in the env file and configuration of SMTP could be done on the admin dashboard with proper error/debug details if adding SMTP fails.

Agreed. that's definitely the way to go. Obviously!

MAS-CreativeLabs commented 12 months ago

@tecoad @MAS-CREATIVE-LABS can you please advise your deployment method? Did you use Docker or other way of deploying the app?

@baptisteArno had no issues using the same creds, so question arises if there's an issue with the Docker compose config?

I apologize for the delayed reply, I was on a trip. her's my configuration: I'm hosting typebot on a Synology NAS with a reverse proxy (the same exact test machine I use for all my docker in my testing environment. I used portainer for the deployment of typebot and I followed the instructions from the official documentation.

Docker compose file :

`version: '3.3'

volumes: db-data:

services: typebot-db: image: postgres:14-alpine restart: always volumes:

.env file:

`ENCRYPTION_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXX

DATABASE_URL=postgresql://postgres:typebot@typebot-db:5432/typebot

NEXTAUTH_URL=https://typebot.mydonmain.com

NEXT_PUBLIC_VIEWER_URL=https://bot.mydonmain.com

ADMIN_EMAIL=XXXXX@gmail.com

NEXTAUTH_URL_INTERNAL=http://localhost:3000

DEFAULT_WORKSPACE_PLAN=UNLIMITED

DISABLE_SIGNUP=false

NEXT_PUBLIC_BOT_FILE_UPLOAD_MAX_SIZE=250

SMTP_USERNAME=XXXXX@XXXXX.com

SMTP_PASSWORD=XXXXXXXXXXXXXXXXXXXX

SMTP_HOST=ssl0.ovh.net

SMTP_PORT=465

SMTP_SECURE=true

NEXT_PUBLIC_SMTP_FROM="'Typebot Notifications' XXXXX@XXXXX.com"

GOOGLE_CLIENT_ID=XXXXXXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com

GOOGLE_CLIENT_SECRET=XXXXX-XXXXXXXXXX_XXXXX

GITHUB_CLIENT_ID=XXXXXXXXXXXXXXXXXXXXXXXXX

GITHUB_CLIENT_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

FACEBOOK_CLIENT_ID=XXXXXXXXXXXXXXXXXXXX

FACEBOOK_CLIENT_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

NEXT_PUBLIC_GIPHY_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXX

NEXT_PUBLIC_UNSPLASH_APP_NAME=Typebot

NEXT_PUBLIC_UNSPLASH_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXX

DEBUG=true`

CleanShot 2023-11-12 at 02 08 52@2x

MAS-CreativeLabs commented 12 months ago

@baptisteArno I have 10 other Docker containers using the same credential flawlessly, the issue is only with Typebot. As suggested by @lazmo88 that settings should be taking place in the admin dashboard

ramstein74 commented 8 months ago

i have the same issue No pending migrations to apply. â–² Next.js 14.1.0

but if i telnet to my smtp server from the docker machine i can send a test email with the same credentials

baptisteArno commented 8 months ago

Make sure your VPS has the SMTP port opened

salah-l commented 5 months ago

@tecoad You should add this to nextjs.config process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

juliancesar commented 5 months ago

@salah-l , can i use this variable (NODE_TLS_REJECT_UNAUTHORIZED) in .env file?

My error is: [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames: Host: mail.XXXX.online. is not in the cert's altnames: DNS:linux2.XXXX.com.br

salah-l commented 5 months ago

@juliancesar

In node, the NODE_TLS_REJECT_UNAUTHORIZED environment variable controls whether or not the application will reject self-signed or otherwise untrusted certificates during SSL/TLS

By setting this variable to 0, you are instructing node to bypass the usual certificate validation process, allowing connections to proceed even if the certificates are not trusted.

You should know that this is not suitable for production environments due to security risks. It should only be used in development environments.

If you want to use this setting in a .env file, you should load the .env file at the top of main app file, which is why i suggested to add it directly to nextjs config and avoid the hustle.

Alternatively, you can set the environment variable directly when running the app from the command line like this NODE_TLS_REJECT_UNAUTHORIZED=0 pnpm dev

this way, no need to update the config file

Nitishkumar63 commented 4 months ago

Error sending email: Error: queryA ETIMEOUT smtp.gmail.com at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/callback_resolver:45:19) { errno: undefined, code: 'EDNS', syscall: 'queryA', hostname: 'smtp.gmail.com', command: 'CONN' } node:_http_outgoing:659 throw new ERR_HTTP_HEADERS_SENT('set'); ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (node:_http_outgoing:659:11) at ServerResponse.header (C:\Users\nitis\Desktop\chat-app\backend\node_modules\express\lib\response.js:795:10) at ServerResponse.send (C:\Users\nitis\Desktop\chat-app\backend\node_modules\express\lib\response.js:175:12) at SendMail (C:\Users\nitis\Desktop\chat-app\backend\model\send.module.mail.js:28:21) { code: 'ERR_HTTP_HEADERS_SENT' }

same error

mikeleblank commented 3 months ago

@juliancesar

In node, the NODE_TLS_REJECT_UNAUTHORIZED environment variable controls whether or not the application will reject self-signed or otherwise untrusted certificates during SSL/TLS

By setting this variable to 0, you are instructing node to bypass the usual certificate validation process, allowing connections to proceed even if the certificates are not trusted.

You should know that this is not suitable for production environments due to security risks. It should only be used in development environments.

If you want to use this setting in a .env file, you should load the .env file at the top of main app file, which is why i suggested to add it directly to nextjs config and avoid the hustle.

Alternatively, you can set the environment variable directly when running the app from the command line like this NODE_TLS_REJECT_UNAUTHORIZED=0 pnpm dev

this way, no need to update the config file

My certificates are correct but it still didn't work, I disabled node tls and it worked perfectly.

salah-l commented 3 months ago

Error sending email: Error: queryA ETIMEOUT smtp.gmail.com at QueryReqWrap.onresolve [as oncomplete] (node:internal/dns/callback_resolver:45:19) { errno: undefined, code: 'EDNS', syscall: 'queryA', hostname: 'smtp.gmail.com', command: 'CONN' } node:_http_outgoing:659 throw new ERR_HTTP_HEADERS_SENT('set'); ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (node:_http_outgoing:659:11) at ServerResponse.header (C:\Users\nitis\Desktop\chat-app\backend\node_modules\express\lib\response.js:795:10) at ServerResponse.send (C:\Users\nitis\Desktop\chat-app\backend\node_modules\express\lib\response.js:175:12) at SendMail (C:\Users\nitis\Desktop\chat-app\backend\model\send.module.mail.js:28:21) { code: 'ERR_HTTP_HEADERS_SENT' }

same error

You should use a dedicated SMTP service like @baptisteArno suggested, I think there's an issue with gmail. You can try Brevo they have free service up to 300 email/day

anzacloud commented 3 months ago

Solution that worked for me: When setting NEXT_PUBLIC_SMTP_FROM ensure you have it in the format SENDER_NAME< emailaddress > e.g. NEXT_PUBLIC_SMTP_FROM=Example Support < example@support.com >

mario-evangelista-silva commented 1 month ago

NEXTAUTH_URL and NEXTPUBLIC VIEWER URL I put the same name and domain. My ".env" that works is the following:

ENCRYPTION_SECRET=ZfIOlwkCcCS91euNCV8fRsJi4WAHONO3 DATABASE_URL=postgresql://postgres:typebot@typebot-db:5432/typebot NODE_OPTIONS=--no-node-snapshot

NEXTAUTH_URL=https://hostabc.yourdomain.com NEXT_PUBLIC_VIEWER_URL=https://hostabc.yourdomain.com

DEFAULT_WORKSPACE_PLAN=UNLIMITED

ADMIN_EMAIL=youruser@yourdomain.com SMTP_USERNAME=youruser@yourdomain.com SMTP_PASSWORD=xbxbxbxbxbxb SMTP_HOST=yoursmtp server SMTP_PORT=465 SMTP_SECURE=true NEXT_PUBLIC_SMTP_FROM=youruser@yourdomain.com