admin管理员组文章数量:1025457
When i am trying to send a message using React with above code:
import emailjs from '@emailjs/browser'
const sendEmail = (e) => {
e.preventDefault();
emailjs.sendForm('gmail', 'service_*', form.current, 'template_*','public key', )
.then(
() => {
alert('Message successfully sent!');
window.location.reload(false)
}, (error) => {
console.error('Failed to send the message:', error);
alert(`Failed to send the message: ${error.text}`);
});
};
I got the message "Failed to send the message: The Public Key is invalid.
Tying to refresh the Public key and edit the code just like this:
const sendEmail = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('SERVICE_ID', 'TEMPLATE_ID', sendEmail, {
publicKey: 'PUBLIC_KEY',
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
When i am trying to send a message using React with above code:
import emailjs from '@emailjs/browser'
const sendEmail = (e) => {
e.preventDefault();
emailjs.sendForm('gmail', 'service_*', form.current, 'template_*','public key', )
.then(
() => {
alert('Message successfully sent!');
window.location.reload(false)
}, (error) => {
console.error('Failed to send the message:', error);
alert(`Failed to send the message: ${error.text}`);
});
};
I got the message "Failed to send the message: The Public Key is invalid.
Tying to refresh the Public key and edit the code just like this:
const sendEmail = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('SERVICE_ID', 'TEMPLATE_ID', sendEmail, {
publicKey: 'PUBLIC_KEY',
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
Share
Improve this question
edited Nov 18, 2024 at 13:03
konstantinosiakovou
asked Nov 18, 2024 at 10:57
konstantinosiakovoukonstantinosiakovou
155 bronze badges
4
|
1 Answer
Reset to default 0Solved! Attached the sendEmail function to an event and used e.preventDefault() to prevent the default form submission behavior:
emailjs.init({ publicKey: '*' });
const sendEmail = (e) => { e.preventDefault();
var emailData = {
name: 'Konstantinos Iakovou | Web developer',
notes: 'Check this out!',
};
emailjs.send('service_*', 'template_*', emailData).then(
(response) => {
alert('Message successfully sent!');
},
(error) => {
console.error('Failed to send the message:', error);
alert(`Failed to send the message: ${error.text}`);
},
);
};
When i am trying to send a message using React with above code:
import emailjs from '@emailjs/browser'
const sendEmail = (e) => {
e.preventDefault();
emailjs.sendForm('gmail', 'service_*', form.current, 'template_*','public key', )
.then(
() => {
alert('Message successfully sent!');
window.location.reload(false)
}, (error) => {
console.error('Failed to send the message:', error);
alert(`Failed to send the message: ${error.text}`);
});
};
I got the message "Failed to send the message: The Public Key is invalid.
Tying to refresh the Public key and edit the code just like this:
const sendEmail = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('SERVICE_ID', 'TEMPLATE_ID', sendEmail, {
publicKey: 'PUBLIC_KEY',
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
When i am trying to send a message using React with above code:
import emailjs from '@emailjs/browser'
const sendEmail = (e) => {
e.preventDefault();
emailjs.sendForm('gmail', 'service_*', form.current, 'template_*','public key', )
.then(
() => {
alert('Message successfully sent!');
window.location.reload(false)
}, (error) => {
console.error('Failed to send the message:', error);
alert(`Failed to send the message: ${error.text}`);
});
};
I got the message "Failed to send the message: The Public Key is invalid.
Tying to refresh the Public key and edit the code just like this:
const sendEmail = {
name: 'James',
notes: 'Check this out!',
};
emailjs
.send('SERVICE_ID', 'TEMPLATE_ID', sendEmail, {
publicKey: 'PUBLIC_KEY',
})
.then(
(response) => {
console.log('SUCCESS!', response.status, response.text);
},
(err) => {
console.log('FAILED...', err);
},
);
Share
Improve this question
edited Nov 18, 2024 at 13:03
konstantinosiakovou
asked Nov 18, 2024 at 10:57
konstantinosiakovoukonstantinosiakovou
155 bronze badges
4
-
Looking at the docs of that provider, you have to call
emailjs.init({publicKey: "..."})
with your public key prior to be able to send emails. Have you done this? Alternatively, you can pass an options object like{publicKey: "..."}
toemailjs.sendForm(...)
You seem to try passing a string instead of an object ... – derpirscher Commented Nov 18, 2024 at 11:05 - @derpirscher Yes, I have tried also to call emailjs.init public key prior but still the same. – konstantinosiakovou Commented Nov 18, 2024 at 11:11
-
Well, then either your key is indeed invalid (something only the provider can clarify) or you have done it wrong (ie you don't show you you are using
init
) But in principle your second example should work, if we assume your pubic key is valid. – derpirscher Commented Nov 18, 2024 at 11:26 - The method can receive up to 4 arguments, and you have 5. – Sergey Khomushin Commented Nov 22, 2024 at 3:45
1 Answer
Reset to default 0Solved! Attached the sendEmail function to an event and used e.preventDefault() to prevent the default form submission behavior:
emailjs.init({ publicKey: '*' });
const sendEmail = (e) => { e.preventDefault();
var emailData = {
name: 'Konstantinos Iakovou | Web developer',
notes: 'Check this out!',
};
emailjs.send('service_*', 'template_*', emailData).then(
(response) => {
alert('Message successfully sent!');
},
(error) => {
console.error('Failed to send the message:', error);
alert(`Failed to send the message: ${error.text}`);
},
);
};
本文标签: reactjsEmailJS Can not send message SOLVEDStack Overflow
版权声明:本文标题:reactjs - EmailJS. Can not send message. SOLVED - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745623640a2159726.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
emailjs.init({publicKey: "..."})
with your public key prior to be able to send emails. Have you done this? Alternatively, you can pass an options object like{publicKey: "..."}
toemailjs.sendForm(...)
You seem to try passing a string instead of an object ... – derpirscher Commented Nov 18, 2024 at 11:05init
) But in principle your second example should work, if we assume your pubic key is valid. – derpirscher Commented Nov 18, 2024 at 11:26