Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use/import in /server/api ? #249

Closed
Yves852 opened this issue Sep 4, 2024 · 1 comment · May be fixed by #251
Closed

How to use/import in /server/api ? #249

Yves852 opened this issue Sep 4, 2024 · 1 comment · May be fixed by #251

Comments

@Yves852
Copy link

Yves852 commented Sep 4, 2024

Hello.

I'm trying to use nuxt-mail server-side with Nuxt but I can't figure out how to import it . Working as expected in client but I would prefer avoid it. Is the project allowing it?

Here is my basic test api:

// server/api/test-mail.js

// import { useMail } from "#imports"
// import useEmail from 'nuxt-mail'
import { useEmail } from 'nuxt-mail'

export default defineEventHandler(async (event) => {
  const mail = useMail()
  // const body: any = await readBody(event)
  // sanitize sanitize sanitize sanitize sanitize sanitize sanitize
  console.log('Test email')
  try {
      mail.send({
        config: 'contact',
        from: 'John Doe',
        subject: 'Test nuxt-mail',
        text: 'This is an incredible test message',
      })
  }
  catch(e) {
    console.error(e)  // [nuxt] [request error] [unhandled] [500] useMail is not defined
    throw createError({
      statusCode: 500,
    })
  }
})
// Client side
<script lang="ts" setup>
const { error, execute, status } = await useFetch((): string => '/api/test-mail', {
  immediate: false,
})

// Works
/*
const mail = useMail()
function send() {
  try {
    mail.send({
      from: 'John Doe',
      subject: 'Test nuxt-mail',
      text: 'This is an incredible test message',
    })
  }
  catch (e) {
    console.error(e)
  }
}
*/
</script>

<template>
  <div>
    <button @click="execute">
      Send email
    </button>
    <div>{{ status }}</div>
    <div v-if="error">
      {{ error }}
    </div>
  </div>
</template>

Thanks.

@Yves852
Copy link
Author

Yves852 commented Sep 5, 2024

Figured out you recommend Nodemailer for server side in pending PR #112.

Closing issue.

@Yves852 Yves852 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 5, 2024
@dword-design dword-design linked a pull request Sep 27, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant