【问题标题】:Emailjs working in development but not in productionEmailjs 在开发中工作但不在生产中
【发布时间】:2022-01-03 22:21:21
【问题描述】:

我有一个带有联系表单的 Next js 应用程序。我正在使用 Emailjs 将电子邮件发送到我的 gmail 帐户。这在开发中非常有效,但是一旦我部署它就停止工作并且它给了我这个消息:

未捕获 用户 ID 是必需的。访问https://dashboard.emailjs.com/admin/integration

这是我的代码(与emailjs web中的示例基本相同)

    const [result, setResult] = useState(false);
    
      const form = useRef();
    
      const sendEmail = (e) => {
        e.preventDefault();
    
        emailjs
          .sendForm(
            process.env.EMAIL_SERVICE,
            process.env.EMAIL_TEMPLATE,
            form.current,
            process.env.EMAIL_USER
          )
          .then(
            (result) => {
              console.log(result.text);
            },
            (error) => {
              console.log(error.text);
            }
          );
        e.target.reset();
        setResult(true);
      };
    
//THIS CODE IS JUST TO ERASE THE "SENT" MESSAGE AFTER 5 secs
      setTimeout(() => {
        setResult(false);
      }, 5000);

我正在使用 .env 文件来获取实际值,并且正如我所说,在开发中完美运行。

我是否缺少使其在生产中工作的东西?

【问题讨论】:

    标签: reactjs email next.js development-environment


    【解决方案1】:

    注销环境变量。 它们很可能在生产中未定义。 NextJS environment variables documentation

    来自文档的示例 - 内部 next.config.js:

    const {
      PHASE_DEVELOPMENT_SERVER,
      PHASE_PRODUCTION_BUILD,
    } = require('next/constants')
    
    // This uses phases as outlined here: https://nextjs.org/docs/#custom-configuration
    module.exports = (phase) => {
      // when started in development mode `next dev` or `npm run dev` regardless of the value of STAGING environmental variable
      const isDev = phase === PHASE_DEVELOPMENT_SERVER
      // when `next build` or `npm run build` is used
      const isProd = phase === PHASE_PRODUCTION_BUILD && process.env.STAGING !== '1'
      // when `next build` or `npm run build` is used
      const isStaging =
        phase === PHASE_PRODUCTION_BUILD && process.env.STAGING === '1'
    
      console.log(`isDev:${isDev}  isProd:${isProd}   isStaging:${isStaging}`)
    
      const env = {
        RESTURL_SPEAKERS: (() => {
          if (isDev) return 'http://localhost:4000/speakers'
          if (isProd) {
            return 'https://www.siliconvalley-codecamp.com/rest/speakers/ps'
          }
          if (isStaging) return 'http://localhost:11639'
          return 'RESTURL_SPEAKERS:not (isDev,isProd && !isStaging,isProd && isStaging)'
        })(),
        RESTURL_SESSIONS: (() => {
          if (isDev) return 'http://localhost:4000/sessions'
          if (isProd) return 'https://www.siliconvalley-codecamp.com/rest/sessions'
          if (isStaging) return 'http://localhost:11639'
          return 'RESTURL_SESSIONS:not (isDev,isProd && !isStaging,isProd && isStaging)'
        })(),
      }
    
      // next.config.js object
      return {
        env,
      }
    }
    

    【讨论】:

    • 我觉得这很奇怪。我还为我的 api 数据使用了一个变量,并且页面显示良好,即使已部署。变量和值在 Verces 的设置中设置,这是我用于部署的页面。你可以在这里查看ikarulus.com
    【解决方案2】:

    我目前在使用 EmailJS 的键时遇到类似问题,我找到了Emile 的答案,这有助于我了解更多信息。

    【讨论】:

    • longstoryshort 用户 ID 和其他键可以公开,因为它们应该显示在浏览器中?
    • 看起来如此@AfxCrush。不过,我向 EmailJS 的支持部门发送了一封电子邮件。如果与此不同,我会告诉你。
    • 同样...昨天我向 Emailjs 联系人发送了一条消息。让我们看看他们要说什么。谢谢你的帮助:)
    • 好的。已经收到回复:“这是因为您的 .env 值没有传递给 SDK。请注意,EmailJS 发送端点不需要身份验证,因此您可以调用我们的 API 而无需担心安全性。用户 ID 是公钥。如果要更改服务配置,只需更改服务 ID 或模板 ID。"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 2011-10-29
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    相关资源
    最近更新 更多