[英]Nodemailer multiple recipients
I'm using Nodemailer with mailgun to send emails to an array of users.我正在使用带有 mailgun 的 Nodemailer 向一组用户发送电子邮件。 I'm generating the HTML to send for the email with React.
我正在生成 HTML 以使用 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.
我想将电子邮件数组中的当前“收件人”传递给 React 组件,以便我可以在 React 组件中使用它。
I need to know which user I'm dealing with before the callback since I need it to generate the HTML我需要在回调之前知道我正在处理哪个用户,因为我需要它来生成 HTML
NODE节点
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>
)
}
})
To send the mail to multiple users you have to place them in a data type String, separated by commas, an example要将邮件发送给多个用户,您必须将它们放在数据类型字符串中,用逗号分隔,例如
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} />)
}) })
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).虽然@Potier97 的答案是正确的,但我强烈建议您改为向每个收件人发送一封单独的电子邮件(也就是遍历您的收件人列表并为每个收件人调用
sendMail
)。 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 = ['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} />)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.