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

How to make sure `client.close()` doesn't throw an error? #48

Closed timonson closed 1 year ago

timonson commented 1 year ago

Hello!

Is there a way to check if I can invoke client.close() without causing a BadResource error if the connection had been closed already? This happens to me when client.send() throws an error because of bad input and I invoke client.close() afterwards. Apparently the connection will be closed automatically if client.send() throws an error.

How would you handle the following example? Do I even have to close the connection after each request? Maybe you could add an example to the readme?

Thanks!

export async function sendEmail(client: SMTPClient, sendConfig: SendConfig) {
  try {
    throwsSometimes()
    await client.send(sendConfig);
    await client.close();
  } catch (err) {
    await client.close();
  }
}
mathe42 commented 1 year ago

Will add the try in the lib so calling .close is allways safe.

If you have multiple E-Mails to send you should consider using the pool options (even with pool size 1) that way you can have connections that are alive longer / are closed automaticly by denomailer when idle. But they might not catch all errors and it is considered unstable (you also need the --unstable deno flag).

if .send throws there are some cases where we can not recover from but I think most cases should recover. If that is not the case please create an issue for that!

timonson commented 1 year ago

I think this has not been done yet, correct? I would really appreciate this change if possible. Thank you!

mathe42 commented 1 year ago

Was finishing my thesis and forget that 🙈...