编写在按下列表视图的按钮时发送电子邮件的代码可以使用以下步骤:
<!DOCTYPE html>
<html>
<head>
<title>发送邮件</title>
<script>
function sendEmail() {
// 获取输入的收件人、主题和内容
var recipient = document.getElementById("recipient").value;
var subject = document.getElementById("subject").value;
var content = document.getElementById("content").value;
// 创建邮件链接
var mailtoLink = "mailto:" + recipient + "?subject=" + encodeURIComponent(subject) + "&body=" + encodeURIComponent(content);
// 打开默认邮件客户端发送邮件
window.location.href = mailtoLink;
}
</script>
</head>
<body>
<label for="recipient">收件人:</label>
<input type="email" id="recipient" required><br><br>
<label for="subject">主题:</label>
<input type="text" id="subject" required><br><br>
<label for="content">内容:</label><br>
<textarea id="content" rows="5" cols="50" required></textarea><br><br>
<button onclick="sendEmail()">发送邮件</button>
</body>
</html>
在上述代码中,我们使用了mailto
链接来创建一个包含收件人、主题和内容的邮件链接。然后,通过将该链接赋值给window.location.href
,我们可以打开默认的邮件客户端并自动填充相关信息。
const nodemailer = require("nodemailer");
// 创建邮件传输对象
let transporter = nodemailer.createTransport({
host: "smtp.example.com",
port: 587,
secure: false,
auth: {
user: "your-email@example.com",
pass: "your-password"
}
});
// 定义邮件内容
let mailOptions = {
from: "your-email@example.com",
to: "recipient@example.com",
subject: "邮件主题",
text: "邮件内容"
};
// 发送邮件
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("发送邮件失败:" + error);
} else {
console.log("邮件已发送:" + info.response);
}
});
在上述代码中,我们使用了Nodemailer库来发送电子邮件。首先,我们创建了一个邮件传输对象,其中包含邮件服务器的配置和认证凭据。然后,我们定义了邮件的内容,包括发件人、收件人、主题和内容。最后,通过调用transporter.sendMail
方法来发送邮件,并在回调函数中处理发送结果。
请注意,以上示例代码仅为演示目的,实际使用时需要根据你的具体需求进行适当的修改和配置。
推荐的腾讯云相关产品:腾讯云邮件推送(https://cloud.tencent.com/product/ses)
领取专属 10元无门槛券
手把手带您无忧上云