Jon
Jon

Reputation: 6275

Nodemailer multiple recipients

I'm using Nodemailer with mailgun to send emails to an array of users. I'm generating the HTML to send for the email with React. I want to pass in the current "to" in my emails array to the React component so I can use it within the React component.

I need to know which user I'm dealing with before the callback since I need it to generate the HTML

NODE

const emails = ['[email protected]', '[email protected]'];

nodemailerMailgun.sendMail({
  from: '[email protected]',
  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="[email protected]" 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>
     )
 }
})

Upvotes: 4

Views: 5378

Answers (2)

Mouradif
Mouradif

Reputation: 2724

While @Potier97's answer is correct, I strongly recommend you instead send each recipient a separate e-mail (aka loop through your list of recipients and call sendMail for each). This is because sending an e-mail to multiple recipients means each recipient get the exact same e-mail. They can also see all the other recipients and can Reply to all. I don't know about your specific use case but it's very unlikely that you actually want to do this

const emails = ['[email protected]', '[email protected]'];

for (const email of emails) {
  nodemailerMailgun.sendMail({
    from: '[email protected]',
    to: email,
    subject: 'Event Invitation',
    html: renderToString(<InvitationEmail from="[email protected]" to={email} eventId={eventId} />)
  })

}

Upvotes: 1

Potier97
Potier97

Reputation: 105

To send the mail to multiple users you have to place them in a data type String, separated by commas, an example

const arrayUsersMail = ['[email protected]', '[email protected]', '[email protected]', '[email protected]' ]

const stringUsersMail = arrayUsersMail.join(', ')

nodemailerMailgun.sendMail({
from: '[email protected]',
to: stringUsersMail,
subject: 'Event Invitation',
html: renderToString(<InvitationEmail from="[email protected]" to="WHAT HERE" eventId={eventId} />)

})

Upvotes: 1

Related Questions