I am developing a project with Vue.js, so to manage sending email through the contact form instead of the backend, I am using Email.js. I followed steps as below:
- Add an email services
- Create an email template
- Install EmailJS
- Create contact form
I hosted it on Netlify. Problem is, the sending email works on localhost and in the permalink on Netlify, but not in the production deploy:
Anyone have any idea why the EmailJS works properly in the localhost and in the permalink but not in the production deploy?
This is my contact form:
<template>
<form class="contact-form" @submit.prevent="submitForm">
<div class="form-row">
<input type="text" placeholder="Emri" v-model="form.name" required />
<input type="text" placeholder="Mbiemri" v-model="form.surname" required />
</div>
<input type="email" placeholder="Emaili" v-model="form.email" required />
<input type="tel" placeholder="Numri i telefonit" v-model="form.phoneNumber" required />
<textarea placeholder="Mesazhi juaj..." v-model="form.message" required></textarea>
<label class="form-checkbox">
<input type="checkbox" id="agree" v-model="form.agree" required />
<span class="checkmark"></span>
Unë pajtohem me politikën e privatësisë
</label>
<button type="submit">Dërgo mesazhin</button>
</form>
</template>
<script>
import emailjs from 'emailjs-com';
export default {
data() {
return {
form: {
name: '',
surname: '',
email: '',
phoneNumber: '',
message: '',
agree: false
}
};
},
methods: {
submitForm() {
if (!this.form.agree) {
alert('Ju duhet të pajtoheni me politikën e privatësisë.');
return;
}
emailjs.init('PUBLIC_KEY');
const templateParams = {
from_name: `${this.form.name} ${this.form.surname}`,
from_email: this.form.email,
phone: this.form.phoneNumber,
message: this.form.message
};
emailjs.send(
'SERVICE_ID',
'TEMPLATE_ID',
templateParams,
'PUBLIC_KEY'
).then(() => {
alert('Mesazhi u dërgua me sukses!');
this.resetForm();
}).catch((error) => {
console.error('Gabim gjatë dërgimit:', error);
alert('Ndodhi një gabim gjatë dërgimit.');
});
},
resetForm() {
this.form = {
name: '',
surname: '',
email: '',
phoneNumber: '',
message: '',
agree: false
};
}
}
};
</script>
I tried to check if the email service, email template, account is OK or maybe has any mistake, but their configuration is ok.
emailjs.send()has wrong 4th parameter, instead of your public key the documentation explains this parameter is an optional object of type "Options"