【发布时间】: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