Nodemailer多个收件人

时间:2016-08-09 23:39:50

标签: javascript express reactjs nodemailer

我使用带有mailgun的Nodemailer向一组用户发送电子邮件。我正在使用React生成要发送的电子邮件的HTML。我想把当前的"传递到"在我的电子邮件数组中到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} />)
})

REACT

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>
     )
 }
})

2 个答案:

答案 0 :(得分: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} />)

})

答案 1 :(得分:0)

虽然 @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} />)
  })

}