如何使用smtp.js发送电子邮件使用react.js

问题描述 投票:0回答:1

此代码在功能内但不起作用

Email.send({ Host : "smtp.elasticemail.com", Username : "username", Password : "password", To : '[email protected]', From : "[email protected]", Subject : "This is the subject", Body : "And this is the body" }).then( message => alert(message) );

还有 我应该把这个放在哪里?

<script src="https://smtpjs.com/v3/smtp.js"> </script>

我把它放在 jsx 文件中,但它给出了这个错误。 enter image description here

reactjs smtp
1个回答
0
投票
Step 1:
npm install smtp.js

Step 2:
import smtp from 'smtp.js';
const credentials = {
  host: 'smtp.host.com',
  port: 465,
  secure: true, 
  auth: {
    user: 'username',
    pass: 'password'
  }
}

smtp.connect(credentials)
  .then(info => {
    // connected, credentials OK 
  })

Step 3:
const email = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'test subject',
  html: '<p>This is an <strong>HTML email</strong></p>'
}

smtp.connect(credentials)
  .then(() => smtp.sendMail(email))
  .then(info => console.log('Email sent!'))
  .catch(err => console.error(err))
© www.soinside.com 2019 - 2024. All rights reserved.