【问题标题】:NodeJS Sendgrid Issue in sending email to multiple recipients向多个收件人发送电子邮件时出现 NodeJS Sendgrid 问题
【发布时间】:2013-07-03 18:01:53
【问题描述】:

我在向多个收件人发送邮件时遇到问题。

我的脚本是

var SendGrid = require('sendgrid').SendGrid;
var sendgrid = new SendGrid('<<username>>', '<<password>>');      
    sendgrid.send({
    to: 'nabababa@gmail.com',   
from: 'sengupta.nabarun@gmail.com',
bcc: ["sengupta.nabarun@gmail.com","sengupta_nabarun@rediffmail.com"],

这里有两个问题

  1. 我可以列出一组收件人吗?
  2. 如何在密件抄送列表中获取收件人数组?

与以上两个查询相关的解决方案确实会有所帮助

谢谢 那巴伦

【问题讨论】:

    标签: node.js sendgrid


    【解决方案1】:

    这是我最终得到的解决方案,并认为它更简单,并且可能对人们有所帮助。

    注意个性化对象的形状差异。

    收件人可以互相看到:

    
    const sgMail = require('@sendgrid/mail')
    sgMail.setApiKey(process.env.SENDGRID_API_KEY)
    
    // Declare the content we'll use for the email
    const FROM_EMAIL = 'example@example.io' // <-- Replace with your email
    const subject = 'Test Email Subject'
    const body = '<p>Hello HTML world!</p>'
    const recipients = ['alice@example.com', 'bob@example.com'] // <-- Add your email(s) here to test
    
    // Create the personalizations object that will be passed to our message object
    let personalizations = [{
        to: [],
        subject
    }]
    
    // Iterate over our recipients and add them to the personalizations object
    for (let index in recipients) {
        personalizations[0].to[index] = { email: recipients[index] }
    }
    
    const msg = {
        personalizations,
        from: FROM_EMAIL,
        html: body,
    }
    
    // Log to see what our message object looks like
    console.log(msg)
    
    // Send the email, if success log it, else log the error message
    sgMail.send(msg)
        .then(() => console.log('Mail sent successfully'))
        .catch(error => console.error(error.toString()))
    

    个性化对象

    {
        personalizations: [{
            to: [
                {email: "alice@example.com"},
                {email: "bob@example.com"},
            ],
            subject: "Test Email Subject"
        }]
    }
    

    收件人不能看到对方:

    // Create the personalizations object that will be passed to our message object
    personalizations = []
    
    // Iterate over our recipients and add them to the personalizations object
    for (let index in recipients) {
        personalizations[index] = { to: recipients[index], subject}
    }
    

    个性化对象

    { 
        personalizations: [
            {
                to:  "alice@example.com",
                subject: "Test Email Subject"
            }, 
            { 
                to:  "bob@example.com",
                subject: "Test Email Subject"
            }
        ]
    }
    

    我创建了一个带有完整解决方案的RunKit,您可以在其中进行测试。

    【讨论】:

      【解决方案2】:

      您可以在tobcc 字段中使用收件人数组。

      例如:

      var SendGrid = require('sendgrid').SendGrid;
      var sendgrid = new SendGrid('{{sendgrid username}}', '{{sendgrid password}}');      
      sendgrid.send({
          to: ['one@example.com', 'two@example.com'],
          from: 'nick@sendgrid.com',
          bcc: ['three@example.com', 'four@example.com'],
          subject: 'This is a demonstration of SendGrid sending email to mulitple recipients.',
          html: '<img src="http://3.bp.blogspot.com/-P6jNF5dU_UI/TTgpp3K4vSI/AAAAAAAAD2I/V4JC33e6sPM/s1600/happy2.jpg" style="width: 100%" />'
      });
      

      如果这对您不起作用并且 Node 没有吐出任何错误,请登录 SendGrid 的网站并查看 Email Activity Log 以检查电子邮件是否正在发送。

      我在测试您的代码示例时遇到的一件事是,如果您将 tobcc 发送到同一个 gmail 地址,gmail 会将它们全部合并到一封电子邮件中(所以看起来它不起作用)。确保在测试时将电子邮件发送到完全不同的帐户。

      如果您需要一些电子邮件帐户来测试,Guerrilla Mail 是创建临时测试帐户的绝佳选择。

      【讨论】:

        【解决方案3】:

        对于 Sendgrid 的 v3 API,我发现他们的 "kitchen sink" example 很有帮助。这是其中的一个相关部分:

        var helper = require('sendgrid').mail
        
        mail = new helper.Mail()
        email = new helper.Email("test@example.com", "Example User")
        mail.setFrom(email)
        
        mail.setSubject("Hello World from the SendGrid Node.js Library")
        
        personalization = new helper.Personalization()
        email = new helper.Email("test1@example.com", "Example User")
        personalization.addTo(email)
        email = new helper.Email("test2@example.com", "Example User")
        personalization.addTo(email)
        
        // ...
        
        mail.addPersonalization(personalization)
        

        【讨论】:

        • reading more about Sendgrid's personalizations 之后,我认为您可以在一个个性化设置中执行多个操作。这将总共发送一封电子邮件,收件人字段中有 2 人,而不是 2 封电子邮件每封一封电子邮件。
        【解决方案4】:

        新的 sendgrid-nodejs 更新废弃了以前的实现方法,因此现在接受的答案对您没有帮助。

        所以...只是一个更新,以防有人以特定搜索结果登陆该线程。

            to: [
              {
                email: 'email1@email.com', 
              },
              {
                email: 'email2@email.com', 
              },
            ],
        

        【讨论】:

          【解决方案5】:

          TypeScript 的解决方案(用 ts 版本 3.4.3 和 sendGrid 7.1.1 编写),您不希望收件人能够看到彼此。

          import * as sendGrid from '@sendgrid/mail'
          
          type UserEmail = {
            to: string
            subject: string
          }
          // Add as many recipients as you want
          recipients = ['email1@global.com', 'email2@gmail.com']
          const personalizations: UserEmail[] = recipients.map(admin => ({
              to: admin,
              subject: 'Inject Subject Here',
          }))
          
          try {
              await sendGrid.send({
                  from, // Inject
                  personalizations,
                  html, // Inject
              })
          } catch (err) {
              console.log(err)    
          }
          

          const personalizations 看起来像这样

          [{ to: 'email1@global.com',
              subject: 'Inject Subject Here' },
            { to: 'email2@global.com',
              subject: 'Inject Subject Here' }]
          

          【讨论】:

            猜你喜欢
            • 2012-05-18
            • 2015-06-09
            • 1970-01-01
            • 1970-01-01
            • 2012-01-12
            • 2017-12-05
            • 2015-08-29
            相关资源
            最近更新 更多