我正在使用 smtp.js 将邮件发送到我使用 smtp.js 制作的凭据的电子邮件 ID。我制作了一个联系表单,希望用户输入他的信息和电子邮件,并将其发送到我的电子邮件。所以根据这个逻辑,我的邮件应该在 to 属性中,发件人的邮件应该在 From 属性中。但是,当我运行代码时,它会抛出一个错误:
不允许使用邮箱名称。服务器响应是:不允许使用来自“[电子邮件受保护]”电子邮件地址的信封。
如果不可能,你能告诉我使用 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.')
}
}
)
}
您遇到的此错误是一项安全功能。如果您考虑一下,您不应该能够使其看起来像是其他人发送了电子邮件(其中存在很多恶意潜力)。
因此,我不确定其他 SMTP 提供商是否能让您走得更远。我个人在我的项目中使用 SmtpJS,这是我如何使其满足我的需求的代码片段:
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)
);
祝你好运!
我不知道您使用哪个库来建立 SMTP 连接,但首先请确保 SMTP 服务器接受您需要的所有域。
我建议你使用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',
});