简体   繁体   English

如何从 Nodejs 发送带有 html 内容的 SENDGRID email?

[英]How to send SENDGRID email with html contents from Nodejs?

I have designed a template from Sendgrid website.我从 Sendgrid 网站设计了一个模板。 Let's call it sendgrid.html我们称它为sendgrid.html

I am trying to send the email from Nodejs with the design from sendgrid.html .我正在尝试使用Nodejs的设计从 Nodejs 发送sendgrid.html Below is my Nodejs code:下面是我的 Nodejs 代码:

function sendVoucherCodeEmail (emailAddress, voucherCode){
    sgMail.setApiKey(process.env.SENDGRID_API_KEY);
    const msg = {
        to: emailAddress,
        from: 'example@example.com',
        subject: 'YourCode',
        text: ''
    };
}

I would like to pass emailAddress and voucherCode into the html content and send it as email to the user.我想将emailAddressvoucherCode传递到 html 内容中,并将其作为 email 发送给用户。 How can I do it?我该怎么做? Thank you in advance.先感谢您。

It's better to create a template on sendgrid and just put the ID of the template when sending using sendgrid API. 最好在sendgrid上创建一个模板,并在使用sendgrid API发送时仅放置模板的ID。 By doing this, you can easy change the content without deploy new app. 这样,您可以轻松更改内容,而无需部署新应用。 You can still inject the needed data into the template. 您仍然可以将所需的数据注入模板。

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
sgMail.setSubstitutionWrappers('{{', '}}');
const msg = {
  to: emailAddress,
  from: 'example@example.com',
  templateId: templateId,
  dynamic_template_data: {emailAddress, voucherCode},
};

!note: sendgrid change their API. 注意:sendgrid更改其API。 So substitutions in V2 is replaced by dynamic_template_data in V3: https://github.com/sendgrid/sendgrid-nodejs/issues/703 因此, substitutions在V2被替换dynamic_template_data在V3: https://github.com/sendgrid/sendgrid-nodejs/issues/703

To know how to create template, you can visit the official document here: https://sendgrid.com/docs/User_Guide/Transactional_Templates/create_and_edit_transactional_templates.html 要了解如何创建模板,您可以在此处访问官方文档: https : //sendgrid.com/docs/User_Guide/Transactional_Templates/create_and_edit_transactional_templates.html

Your placeholder in your template should be surrounded by {{ }} . 模板中的占位符应包含{{ }} for example: {{emailAddress}} 例如: {{emailAddress}}

You can do it with Notifire你可以用 Notifire 做到这一点

npm install @notifire/core @notifire/sendgrid

And then just然后只是

import { Notifire, ChannelTypeEnum } from '@notifire/core';
import { SendgridEmailProvider } from '@notifire/sendgrid';

const notifire = new Notifire();

await notifire.registerProvider(
  new SendgridEmailProvider({
    apiKey: process.env.SENDGRID_API_KEY,
    from: 'sender@mail.com'
  })
);

const passwordResetTemplate = await notifire.registerTemplate({
  id: 'password-reset',
  messages: [
    {
      subject: `You password reset request`,
      // Or for translation or custom logic you can use function syntax
      // subject: (payload: ITriggerPayload) => getTranslation('resetPasswordSubject', payload.language),
      channel: ChannelTypeEnum.EMAIL,
      template: `
          Hi {{firstName}}!
          
          To reset your password click <a href="{{resetLink}}">here.</a>
          
          {{#if organization}}
            <img src="{{organization.logo}}" />
          {{/if}}
      `
    },
  ]
});

await notifire.trigger('<REPLACE_WITH_EVENT_NAME>', {
  $user_id: "<USER IDENTIFIER>",
  $email: "test@email.com",
  firstName: "John",
  lastName: "Doe",
  language: "en",
  organization: {
    logo: 'https://evilcorp.com/logo.png'
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM