dword-design / nuxt-mail

Adds email sending capability to a Nuxt.js app. Adds a server route, an injected variable, and uses nodemailer to send emails.
Other
247 stars 18 forks source link

Using Gmail and getting connect ETIMEDOUT 173.194.76.108:465 #212

Closed molul closed 12 months ago

molul commented 1 year ago

When sending emails with nuxt-mail, I'm getting the error message "connect ETIMEDOUT 173.194.76.108:465" after a few seconds.

I followed what's said on the readme (https://github.com/dword-design/nuxt-mail#readme), so I created an app specific password in Gmail and I have this in my nuxt.config.ts (note that when I type I actually mean "blabla@gmail.com"):

modules: [
    [
      "nuxt-mail",
      {
        message: {
          to: <MY_GMAIL_ADDRESS>,
        },
        smtp: {
          service: "gmail",
          auth: {
            user: <MY_GMAIL_ADDRESS>, // same as the "message:to" field
            pass: "xxxx xxxx xxxx xxxx", // this is the app password format, although I've also tried removing the spaces, but no luck
          },
        },
      },
    ],

And this is the function I use for sending the email:

const mail = useMail();
mail.send({
    from: <MY_GMAIL_ADDRESS>, // same email address as in nuxt.config.ts
    subject: "Incredible",
    text: "This is an incredible test message",
  });

I've also tried this config in nuxt.config.ts, but no luck:

"nuxt-mail",
      {
        message: {
          to: <MY_GMAIL_ADDRESS>,
        },
        smtp: {
          host: "smtp.gmail.com",
          service: "gmail",
          auth: {
            user: <MY_GMAIL_ADDRESS>, // same as the "message:to" field
            pass: "xxxx xxxx xxxx xxxx",
          },
          port: 587,
          secure: false,
          requireTLS: true,
        },
      },

There must be something I'm missing (maybe using the same gmail address for sending and receiving is not correct?), but I can't figure out what. Any hint would be greatly appreciated :)

molul commented 1 year ago

Ok, I figured out a fix. I'll leave it here so maybe @dword-design updates the readme.md.

First of all, having the same email address for sender and recipient is NOT a problem. The problem is the needed Gmail configuration.

Also, I confirm that the app specific password must be typed as it is, with the format "xxxx xxxx xxxx xxxx" with spaces.

The config that worked for me was:

[
      "nuxt-mail",
      {
        message: {
          to: import.meta.env.VITE_SMTP_ADDRESS,
        },
        smtp: {
          host: "smtp.gmail.com",
          port: 587,
          secure: false,
          requireTLS: true,
          auth: {
            user: import.meta.env.VITE_SMTP_ADDRESS,
            pass: import.meta.env.VITE_SMTP_PASSWORD,
          },
        },
      },
    ],

On the other hand, I had the variables VITE_SMTP_ADDRESS and VITE_SMTP_PASSWORD defined in my .env file (and also in Vercel).

What is different from the info in the readme document: -I removed the line with "service: 'gmail'". I tested putting it back in, and the email wasn't sent again, so I guess this shouldn't be set in the config. -I added the lines host: "smtp.gmail.com", "port: 587", "secure: false" and "requireTLS: true".

This config sends the email in a couple of seconds, so I guess it's the way to go. Hope it helps :)

PS: one more thing that might be useful. This is my sendEmail function (for a form that has name, email and message fields):

mail.send({
    from: import.meta.env.VITE_SMTP_ADDRESS,
    subject: "[Web contact form] Message from " + name.value,
    replyTo: name.value + " <" + email.value + ">",
    text: message.value,
  });

For those who haven't worked with Nodemailer yet, with the "replyTo" field, although the email sender address is your own email address, when you click "Reply", the recipient of your reply will be the one who filled the contact form.

dword-design commented 12 months ago

@molul Ok cool thanks for investigating. Do you know if we need to set secure and requireTLS to false or does it also with without? Since I guess it would be cool to have a setup for Gmail without disabling security mechanisms. And does the app-specific password also work without spaces? Since the spaces feel odd to me and I also used it without spaces. Still weird that the service: 'gmail' doesn't work for you since I'm using it too.

molul commented 12 months ago

Hi there! I tried those two things:

-Removing both "secure" and "requireTLS" lines doesn't seem to affect. The email is correctly sent too. I got those lines from some example (in stackoverflow, I think), but didn't try without them when I saw the email was finally being sent.

-And yes, the app-specific password works without spaces.

So this config should be enough to get emails being sent:

[
      "nuxt-mail",
      {
        message: {
          to: import.meta.env.VITE_SMTP_ADDRESS,
        },
        smtp: {
          host: "smtp.gmail.com",
          port: 587,
          auth: {
            user: import.meta.env.VITE_SMTP_ADDRESS,
            pass: import.meta.env.VITE_SMTP_PASSWORD,
          },
        },
      },
    ],

Hope it helps!

dword-design commented 12 months ago

@molul I documented this option of configuring Gmail.