英文:
When I try to import a function with npm modules that I exported, I get "Class extends value undefined is not a constructor or null" in sveltekit
问题
我正在尝试使用 SvelteKit 和 npm 模块 "nodemailer" 在我的表单中发送带有数值的电子邮件,但当我导入该函数时,出现以下错误: "Class extends value undefined is not a constructor or null"。
这是包含该函数的 mail.js 文件:
import nodemailer from "nodemailer";
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mymail',
pass: 'mypass'
}
});
export function sendEmail(text) {
const mailOptions = {
from: "from",
to: "to",
subject: "test",
text
}
transporter.sendMail(mailOptions)
}
这是我的位于 /contact 文件夹中的 +page.svelte:
<script>
import Navbar from "../Navbar.svelte";
import Footer from "../Footer.svelte";
import {sendEmail} from "./mail.js";
//sendEmail("message")
</script>
<Navbar/>
<form id="contact-form">
<textarea id="message" cols="30" rows="10"></textarea>
<button type="submit">Send</button>
</form>
<Footer/>
奇怪的事情发生在当我访问页面时,出现 500 错误,并附有我提供的描述,但当取消注释 sendEmail() 函数时,它实际上会发送邮件。
我想要使用表单中的数值发送我的邮件。
英文:
I am trying to send emails with values in my form with sveltekit using npm module: "nodemailer", but when I import the function I have I get this error: "Class extends value undefined is not a constructor or null"
This is the mail.js with the function:
import nodemailer from "nodemailer"
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'mymail',
pass: 'mypass'
}
});
export function sendEmail(text) {
const mailOptions = {
from: "from",
to: "to",
subject: "test",
text
}
transporter.sendMail(mailOptions)
}
and this is my +page.svelte in /contact folder:
<script>
import Navbar from "../Navbar.svelte";
import Footer from "../Footer.svelte";
import {sendEmail} from "./mail.js";
//sendEmail("message")
</script>
<Navbar/>
<form id="contact-form">
<textarea id="message" cols="30" rows="10"></textarea>
<button type="submit">Send</button>
</form>
<Footer/>
The weird stuff happens, when I go on the page, I get 500 error with the description I provided, but when the sendEmail() function is uncommented, it actually sends the email on mine.
I want to send my mail with the values from the form.
答案1
得分: 1
So after digging deep into the answer provided by Tobi Obeck, I finally got it right. I still don't know why the script has been executed, but I do understand why the page hit 500 server error everytime I tried to post the form. I am sending my code for everyone that is out there, here's my solution:
import nodemailer from "nodemailer"
var transporter = nodemailer.createTransport( {
service: "gmail",
auth: {
user: "mymail",
pass: "mypass"
}
})
/** @type {import('./$types').Actions} */
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
const email = data.get("email")
const message = data.get("message")
const mailOptions = {
from: email,
to: "tomail",
subject: `subject`,
text: message
}
await transporter.sendMail(mailOptions)
}
}
Please note that I didn't translate the code parts, as you requested.
英文:
So after digging deep into the answer provided by Tobi Obeck, I finally got it right. I still don't know why the script has been executed, but I do understand why the page hit 500 server error everytime I tried to post the form. I am sending my code for everyone that is out there, here's my solution:
import nodemailer from "nodemailer"
var transporter = nodemailer.createTransport( {
service: "gmail",
auth: {
user: "mymail",
pass: "mypass"
}
})
/** @type {import('./$types').Actions} */
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
const email = data.get("email")
const message = data.get("message")
const mailOptions = {
from: email,
to: "tomail",
subject: `subject`,
text: message
}
await transporter.sendMail(mailOptions)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论