You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为nuxt-mail设置可变收件邮箱,实现双邮件发送?

解决nuxt-mail动态设置收件人并同时发送多邮箱的问题

你之前的写法错误在于对nuxt-mail的$mail.send参数结构理解有误——不需要嵌套message对象,直接在顶层传递to等参数即可;要实现同时发送给固定邮箱和用户邮箱,可采用以下两种方案:

方案1:分两次发送(推荐,适配管理员通知+用户确认的不同需求)

假设表单用户邮箱字段绑定在this.form.userEmail,代码示例:

<script>
export default {
  data() {
    return {
      form: {
        userEmail: '',
        // 其他表单字段如姓名、留言等
      }
    }
  },
  methods: {
    async sendEmail() {
      try {
        // 发送给管理员:包含完整表单信息
        await this.$mail.send({
          to: 'abc@abc.com',
          from: '你的站点名称 <no-reply@yourdomain.com>',
          subject: '新的表单提交通知',
          text: `用户邮箱:${this.form.userEmail}\n姓名:${this.form.name}\n留言:${this.form.message}`
        })

        // 发送给用户:提交确认邮件
        await this.$mail.send({
          to: this.form.userEmail,
          from: '你的站点名称 <no-reply@yourdomain.com>',
          subject: '表单提交成功确认',
          text: '感谢你的提交,我们会尽快与你取得联系!'
        })

        alert('提交成功!')
      } catch (error) {
        console.error('邮件发送失败:', error)
        alert('提交失败,请稍后重试')
      }
    }
  }
}
</script>

方案2:一次发送给多个邮箱(内容相同的场景)

如果不需要区分邮件内容,直接给to参数传包含两个邮箱的数组即可:

<script>
export default {
  data() {
    return {
      form: {
        userEmail: ''
      }
    }
  },
  methods: {
    async sendEmail() {
      try {
        await this.$mail.send({
          to: ['abc@abc.com', this.form.userEmail],
          from: '你的站点名称 <no-reply@yourdomain.com>',
          subject: '表单提交通知',
          text: '这是同时发送给管理员和用户的统一内容'
        })
        alert('提交成功!')
      } catch (error) {
        console.error('邮件发送失败:', error)
        alert('提交失败,请稍后重试')
      }
    }
  }
}
</script>

注意事项

  • nuxt.config.js中配置的message.to是默认收件人,调用$mail.send时传递的to会直接覆盖该默认值,无需修改原有配置。
  • 使用async/await处理异步发送,可有效捕获错误并避免页面卡顿。
  • 发件人from建议采用「名称 <邮箱>」的规范格式,降低被判定为垃圾邮件的概率。

内容的提问来源于stack exchange,提问作者NekoErikk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 10:07:44