【问题标题】:Nodemailer multiple recipientsNodemailer 多个收件人
【发布时间】:2016-12-16 04:04:47
【问题描述】:

我正在使用带有 mailgun 的 Nodemailer 向一组用户发送电子邮件。我正在生成 HTML 以使用 React 发送电子邮件。我想将我的电子邮件数组中的当前“收件人”传递给 React 组件,以便我可以在 React 组件中使用它。

我需要在回调之前知道我正在处理哪个用户,因为我需要它来生成 HTML

节点

const emails = ['email1@gmail.com', 'email2@gmail.com'];

nodemailerMailgun.sendMail({
  from: 'myemail@example.com',
  to: emails,
  subject: 'Event Invitation',

  // how can i pass in the current "to" from my emails array into my react component below?
  html: renderToString(<InvitationEmail from="myemail@example.com" to="WHAT HERE" eventId={eventId} />)
})

反应

const InvitationEmail = React.createClass({
  getDefaultProps() {
    return {
      from: '',
      to: ''
    }
  },
  render() {
    let { to, from } = this.props

     return (
       <div style={{ 'textAlign': 'center' }} className="InvitationEmail">
         <h1>Yo {to}, You've Been Invited!</h1>
       </div>
     )
 }
})

【问题讨论】:

  • 该组件会呈现一个单独的电子邮件,对吧?如果是这样,您是否需要将emails 传递给组件?为什么不循环 emails,将当前电子邮件传递给组件(不是所有电子邮件的列表),然后使用呈现的 html 调用 sendMail
  • 我想我可以为每封电子邮件多次调用 sendMail,但这似乎很愚蠢,因为它们支持通过数组发送多封电子邮件。
  • 他们支持的似乎不是一次发送多封电子邮件,而是一次将一封电子邮件发送给多个收件人。要发送多封电子邮件,html 属性不能是字符串,它必须是函数,以便您可以参数化 to 属性。您可以通过在组件内循环电子邮件来连接所有电子邮件,但随后您会将所有电子邮件正文发送给各个收件人。 (更多地使用 API,我相信他们之前已经考虑过这个任务。)
  • 这种区别很有意义。谢谢!

标签: javascript express reactjs nodemailer


【解决方案1】:

要将邮件发送给多个用户,您必须将它们放在数据类型字符串中,以逗号分隔,示例

const arrayUsersMail = ['emailOne@example.com', 'emailTwo@example.com', 'emailThree@example.com', 'emailFour@example.com' ]

const stringUsersMail = arrayUsersMail.join(', ')

nodemailerMailgun.sendMail({
from: 'myemail@example.com',
to: stringUsersMail,
subject: 'Event Invitation',
html: renderToString(<InvitationEmail from="myemail@example.com" to="WHAT HERE" eventId={eventId} />)

})

【讨论】:

    【解决方案2】:

    虽然@Potier97's answer 是正确的,但我强烈建议您改为向每个收件人发送一封单独​​的电子邮件(也就是遍历您的收件人列表并为每个收件人致电sendMail)。这是因为向多个收件人发送电子邮件意味着每个收件人都会收到完全相同的电子邮件。还可以查看所有其他收件人并可以回复所有收件人。我不知道你的具体用例,但你不太可能真的想要这样做

    const emails = ['email1@gmail.com', 'email2@gmail.com'];
    
    for (const email of emails) {
      nodemailerMailgun.sendMail({
        from: 'myemail@example.com',
        to: email,
        subject: 'Event Invitation',
        html: renderToString(<InvitationEmail from="myemail@example.com" to={email} eventId={eventId} />)
      })
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2011-03-18
      • 2016-12-08
      相关资源
      最近更新 更多