EC-Nordbund / denomailer

A SMTP-Client implementation for deno (to send mails!)
https://deno.land/x/denomailer
MIT License
50 stars 16 forks source link

Cannot send email using Microsoft Outlook Servers (TCP stream is currently in use) #65

Closed nrallakis closed 1 year ago

nrallakis commented 1 year ago

Describe the bug

Can't send email using Microsoft SMTP server (smtp.office365.com). The only way I managed to send email was using the old library deno-smtp with the big caveat of not having StartTLS encryption.

To Reproduce

import {SMTPClient} from 'https://deno.land/x/denomailer/mod.ts'

export async function sendEmail(params: {to: string, subject: string, content: string, replyTo: string}) {
  const client = new SMTPClient({
    connection: {
      hostname: "smtp.office365.com",
      port: 587,
      tls: false,
      auth: {
        username: Deno.env.get('SMTP_USERNAME')!,
        password: Deno.env.get('SMTP_PASSWORD')!,
      },
    },
    debug: {
      log: true,
    }
  })

  try {
    await client.send({
      from: Deno.env.get('SMTP_FROM')!,
      to: params.to,
      subject: params.subject,
      content: params.content,
    })
  } catch (error: any) {
    console.log(error);
  }
  await client.close()
}

Expected behavior

A clear and concise description of what you expected to happen.

Logs

Provide the output of deno --version

deno 1.30.3 (release, x86_64-pc-windows-msvc)
v8 10.9.194.5
typescript 4.9.4

Provide the output of your code snippet (with debug.log set to true see hhttps://github.com/EC-Nordbund/denomailer#options )

Listening on http://localhost:8000/
used resolved config
.debug
┌───────────────┬────────┐
│ (idx)         │ Values │
├───────────────┼────────┤
│ log           │ true   │
│ allowUnsecure │ false  │
│ encodeLB      │ false  │
│ noStartTLS    │ false  │
└───────────────┴────────┘
.connection
┌──────────┬─────────────────────────────────────────────────────────────┐
│ (idx)    │ Values                                                      │
├──────────┼─────────────────────────────────────────────────────────────┤
│ hostname │ "smtp.office365.com"                                        │
│ port     │ 587                                                         │
│ tls      │ false                                                       │
│ auth     │ '{"username":"con*****","password":"Ky*****"}' │
└──────────┴─────────────────────────────────────────────────────────────┘
.pool
undefined
220 VI1PR08CA0133.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 12 Feb 2023 13:28:12 +0000
┌───────┬──────────────────────┐
│ (idx) │ Values               │
├───────┼──────────────────────┤
│     0 │ "EHLO"               │
│     1 │ "smtp.office365.com" │
└───────┴──────────────────────┘
250-VI1PR08CA0133.outlook.office365.com Hello [85.73.133.162]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250 SMTPUTF8
┌───────┬────────────┐
│ (idx) │ Values     │
├───────┼────────────┤
│     0 │ "STARTTLS" │
└───────┴────────────┘
BadResource: TCP stream is currently in use
    at async Object.startTls (deno:ext/net/02_tls.js:89:42)
    at async SMTPClient.#prepareConnection (https://deno.land/x/denomailer@1.5.3/client/basic/client.ts:337:20)
    at async https://deno.land/x/denomailer@1.5.3/client/basic/client.ts:38:7
    at async SMTPClient.send (https://deno.land/x/denomailer@1.5.3/client/basic/client.ts:57:5)
    at async sendEmail (file:///home/deno/functions/add-lead/send_email.ts:20:5)
    at async Server.<anonymous> (file:///home/deno/functions/contact-email/index.ts:14:5)
    at async Server.#respond (https://deno.land/std@0.177.0/http/server.ts:299:18)
error: Uncaught (in promise) BadResource: Bad resource ID
    at async Object.write (deno:ext/web/06_streams.js:877:11)
Watcher Process finished. Restarting on file change...
Stopped serving supabase\functions\contact-email
(base) PS D:\Development\coolcar_backend\supabase> supabase functions serve contact-email --env-file ./supabase/.env.local
Starting supabase\functions\contact-email
Serving supabase\functions\contact-email
Watcher Process started.
Check file:///home/deno/functions/contact-email/index.ts
Hello from Functions!
Listening on http://localhost:8000/
used resolved config
.debug
┌───────────────┬────────┐
│ (idx)         │ Values │
├───────────────┼────────┤
│ log           │ true   │
│ allowUnsecure │ false  │
│ encodeLB      │ false  │
│ noStartTLS    │ false  │
└───────────────┴────────┘
.connection
┌──────────┬─────────────────────────────────────────────────────────────┐
│ (idx)    │ Values                                                      │
├──────────┼─────────────────────────────────────────────────────────────┤
│ hostname │ "smtp.office365.com"                                        │
│ port     │ 587                                                         │
│ tls      │ false                                                       │
│ auth     │ '{"username":"co*****","password":"Ky******"}' │
└──────────┴─────────────────────────────────────────────────────────────┘
.pool
undefined
220 VI1PR08CA0155.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 12 Feb 2023 13:33:38 +0000
┌───────┬──────────────────────┐
│ (idx) │ Values               │
├───────┼──────────────────────┤
│     0 │ "EHLO"               │
│     1 │ "smtp.office365.com" │
└───────┴──────────────────────┘
250-VI1PR08CA0155.outlook.office365.com Hello [85.73.133.162]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
┌───────┬────────────┐
│ (idx) │ Values     │
├───────┼────────────┤
│     0 │ "STARTTLS" │
└───────┴────────────┘
BadResource: TCP stream is currently in use
    at async Object.startTls (deno:ext/net/02_tls.js:89:42)
    at async SMTPClient.#prepareConnection (https://deno.land/x/denomailer@1.5.3/client/basic/client.ts:337:20)
    at async https://deno.land/x/denomailer@1.5.3/client/basic/client.ts:38:7
    at async SMTPClient.send (https://deno.land/x/denomailer@1.5.3/client/basic/client.ts:57:5)
    at async sendEmail (file:///home/deno/functions/_shared/send_email.ts:20:5)
    at async Server.<anonymous> (file:///home/deno/functions/contact-email/index.ts:14:5)
    at async Server.#respond (https://deno.land/std@0.177.0/http/server.ts:299:18)
error: Uncaught (in promise) BadResource: Bad resource ID
    at async Object.write (deno:ext/web/06_streams.js:877:11)
mathe42 commented 1 year ago

Please use TLS or downgrade to 1.4.0 for now (see also #59)

mathe42 commented 1 year ago

(and if your username for the email is "contact@coolcar.gr" and your password starts with "Ky" please change it as you exposed it in your issue)

nrallakis commented 1 year ago

Oops you're right, I will change that! Thank you very much. It works fine after downgrading. I didn't have the option for TLS, so downgrading was the only option. Should have asked a little earlier :P

mathe42 commented 1 year ago

I just found a fix for that. Released in Version 1.6.0!

nrallakis commented 1 year ago

I tried the new version and it still doesn't work. But with another error. Here is the log:

Listening on http://localhost:8000/
used resolved config
.debug
┌───────────────┬────────┐
│ (idx)         │ Values │
├───────────────┼────────┤
│ log           │ true   │
│ allowUnsecure │ false  │
│ encodeLB      │ false  │
│ noStartTLS    │ false  │
└───────────────┴────────┘
.connection
┌──────────┬─────────────────────────────────────────────────────────────┐
│ (idx)    │ Values                                                      │
├──────────┼─────────────────────────────────────────────────────────────┤
│ hostname │ "smtp.office365.com"                                        │
│ port     │ 587                                                         │
│ tls      │ false                                                       │
│ auth     │ '{"username":"********","password":"********"}' │
└──────────┴─────────────────────────────────────────────────────────────┘
.pool
undefined
220 VE1PR08CA0001.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 12 Feb 2023 21:49:14 +0000
┌───────┬──────────────────────┐
│ (idx) │ Values               │
├───────┼──────────────────────┤
│     0 │ "EHLO"               │
│     1 │ "smtp.office365.com" │
└───────┴──────────────────────┘
250-VE1PR08CA0001.outlook.office365.com Hello [85.73.133.162]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8
┌───────┬────────────┐
│ (idx) │ Values     │
├───────┼────────────┤
│     0 │ "STARTTLS" │
└───────┴────────────┘
BadResource: Bad resource ID
    at async Object.startTls (deno:ext/net/02_tls.js:89:42)
    at async SMTPClient.#prepareConnection (https://deno.land/x/denomailer@1.6.0/client/basic/client.ts:339:20)
    at async https://deno.land/x/denomailer@1.6.0/client/basic/client.ts:38:7
    at async SMTPClient.send (https://deno.land/x/denomailer@1.6.0/client/basic/client.ts:57:5)
    at async sendEmail (file:///home/deno/functions/_shared/send_email.ts:20:5)
    at async Server.<anonymous> (file:///home/deno/functions/contact-email/index.ts:14:5)
    at async Server.#respond (https://deno.land/std@0.177.0/http/server.ts:299:18)
error: Uncaught (in promise) Error: invalid cmd
      throw new Error(`invalid cmd`);
            ^
    at SMTPConnection.assertCode (https://deno.land/x/denomailer@1.6.0/client/basic/connection.ts:76:13)
    at SMTPConnection.writeCmdAndAssert (https://deno.land/x/denomailer@1.6.0/client/basic/connection.ts:132:10)
mathe42 commented 1 year ago

Sorry 🙈 Bad testing by me... Creating a new issue for that.

nrallakis commented 1 year ago

No problem, I will use 1.4.0 till you fix it. Thanks!