Saad Ali
Saad Ali

Reputation: 1

Possible issue when sending email with SMTP.js

I am using smtp.js to send mails to my email id whose credentials I made using smtp.js. I made a contact form where I want the user to enter his info and email and that should be sent to my email. So according to this logic, my mail should be in to property and sender's mail should be in the From property.However, when I run the code, it throws an error:

Mailbox name not allowed. The server response was: Envelope FROM '[email protected]' email address not allowed.

If not possible, can u tell me some other alternative using JavaScript.

Code:

function sendmail()
{
    var name = $('#name').val();
    var email = $('#email').val();
    var phone = $('#phone').val();
    var address = $('#address').val();
    var postal = $('#postal').val();
    console.log(typeof(email))
    // var body = $('#body').val();

    var Body='Subject: '+"Estimated Quote"+'<br>Name: '+name+'<br>Email: '+email+'<br>Phone: '+phone+'<br>Address: '+address+'<br>Postal Code: '+postal+'<br>Quote exl VAT: '+vat_excl+'<br>Quote incl VAT: '+vat_incl;
    //console.log(name, phone, email, message);

    Email.send({
        SecureToken:"2d019ac3-046b-4c4f-94e2-f4f3bf08fb1f",
        To: '[email protected]',
        From: email,
        Subject: "New mail from "+name,
        Body: Body
    }).then(
        message =>{
            console.log (message);
            if(message=='OK'){
                alert('Thanks for submitting details.We will contact you soon.');
            }
            else{
                console.error (message);
                alert('Error submitting form.')
            }      
        }       
    )
}

Upvotes: -2

Views: 1866

Answers (2)

Pranai Rao
Pranai Rao

Reputation: 111

This error you are running into is a security feature. If you think about it, you shouldn't be able to make it appear as if someone else has sent an email (there is a lot of malicious potential in that).

Because of that, I'm not sure whether other SMTP providers would get you much further. I personally am using SmtpJS for my project and this is a code snippet of how I made it work for my needs:

Email.send({
        SecureToken : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX", // The secure token of the registered smtpJS email
        To : '[email protected]', // The email that the ticket will be sent to (it can be any email)
        From : "[email protected]", // Must be a registered smtpJS email
        Subject :  subject, // Should have a(n) (ideally unique) random ticket number appended to a common subject title
        Body : "Name: " + fullname.value // This would be derived from the form
            + "<br> Email: " + email.value // This would be derived from the form
            + "<br> Phone: " + phone.value // This would be derived from the form
            + "<br> Message: " + message.value // This would be derived from the form
    }).then(
        message => alert(message)
    );

Best of luck!

Upvotes: 0

SaC-SeBaS
SaC-SeBaS

Reputation: 172

I don't know which library are you using to stablish the SMTP connection, but first of all make sure that the SMTP sever accepts all the domains that you're requiring.

I'd suggest you to use nodemailer which is fairly easy to use.

Example:

// CONFIG SETUP
import { createTransport } from 'nodemailer';

const Transporter = createTransport({
  host: process.env.SMTP_HOST || 'localhost',
  port: Number(process.env.SMTP_PORT) || 25,
  secure: false,
  tls: { rejectUnauthorized: false }, // THIS WILL BE ACCORDING TO YOUR SMTP CONFIG
});

// EXAMPLE OF USAGE
const info = Transporter.sendMail({
      from: '[email protected]',
      to: '[email protected]',
      subject: 'TEST',
      text: 'TEST',
    });

Upvotes: -1

Related Questions