代码之家  ›  专栏  ›  技术社区  ›  sbrass

有没有办法通过SendGrid在node.js中获取空灵邮件ID?

  •  0
  • sbrass  · 技术社区  · 5 年前

    我目前正在使用SendGrid作为web应用程序中的电子邮件服务,我想设置我的开发环境以将电子邮件发送到 https://ethereal.email/ 这样我就能确保它们看起来是正确的。我目前的设置方式是使用 nodemailer.createTestAccount() 启动时,然后记录凭据,然后如果我需要查看电子邮件,我必须手动转到以太坊网站登录并查看所有电子邮件。

    由于我使用SendGrid发送消息,所以我不能仅使用 nodemailer.getTestMessageUrl(info) ,但我真的希望每次在dev中发送电子邮件时都能记录URL,然后我可以直接转到消息。

    我已经尝试过打印SendGrid响应,以及通过IMAP连接和加载最新消息,但这两种消息都不包含我能看到的任何消息ID。

    有没有办法获得发送到ethereal.email的电子邮件的消息ID,并直接构建消息的URL?

    我的环境是node.js,我使用@sendgrid/mail发送消息。

    谢谢

    0 回复  |  直到 5 年前
        1
  •  0
  •   philnash    5 年前

    这里是Twilio SendGrid开发人员的传道者。

    这个 Nodemailer documentation here 建议您在开发中使用ethereal.email作为SMTP设置。然后你可以使用 nodemailer.getTestMessageUrl(info) 以获得正确的URL。

    你说你正在使用 @sendgrid/mail 以发送消息,该消息不会以URL响应,因为它不是通过ethereal.email SMTP服务器发送的。如果您确实想要那个URL,最好的办法是用ethereal.email替换开发/测试环境中的SendGrid。

    您可以通过使用相同的界面创建自己的电子邮件发送对象来实现这一点,其中一个用于 @发送网格/邮件 一个用于具有ethereal.email凭据的Nodemailer,并根据环境加载您想要的对象。

    例如

    class SendGridEmail {
      constructor(sgApiKey) {
        // construct SG mail object
      }
    
      sendEmail(options) {
        // use SG mail object to send email
      }
    }
    
    class NodemailerEmail {
      constructor() {
      }
    
      sendEmail(options) {
      }
    }
    
    function getMailer() {
      if (process.env.NODE_ENV === "production") {
        return new SendGridEmail(process.env.SG_API_KEY);
      } else {
        return new NodemailerEmail();
      }
    }
    
    // later
    
    getMailer().sendEmail(options);