与SMTP.JS发送电子邮件时可能的问题

发布于 2025-02-12 15:28:16 字数 1837 浏览 0 评论 0原文

我正在使用SMTP.JS将邮件发送到我的电子邮件ID,我使用SMTP.J.我制作了一个联系表格,希望用户输入他的信息和电子邮件,应该将其发送到我的电子邮件中。因此,根据此逻辑,我的邮件应该输入属性,发件人的邮件应在属性中。但是,当我运行代码时,它会引发错误:

不允许邮箱名称。服务器响应是:来自'允许。

如果不可能,您可以使用JavaScript告诉我其他替代方案。

代码:

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.')
            }      
        }       
    )
}

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.')
            }      
        }       
    )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2025-02-19 15:28:16

您遇到的此错误是安全功能。如果您考虑一下,您应该无法使其看起来像其他人发送电子邮件(其中有很多恶意潜力)。

因此,我不确定其他SMTP提供商是否会为您带来更大的进步。我个人正在为我的项目使用SMTPJ,这是我如何满足我的需求的代码片段:

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

祝你好运!

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!

紫竹語嫣☆ 2025-02-19 15:28:16

我不知道您正在使用哪个库来稳定SMTP连接,但是首先要确保SMTP Sever接受您需要的所有域。

我建议您使用 nodemailer 这很容易使用。

例子:

// 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',
    });

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',
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文