Search code examples
reactjssmtp

how to send email with smtp.js use react.js


this code inside fuction but not working 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) );

And Where should I put this?

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

I put it in jsx file but it gives this error. enter image description here


Solution

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