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}`);
},
);
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745623641a4636671.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