Skip to content

Commit d80851a

Browse files
send email
1 parent fbbabac commit d80851a

File tree

1 file changed

+56
-80
lines changed

1 file changed

+56
-80
lines changed

src/components/feedback-form/feedback-form.tsx

Lines changed: 56 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -323,95 +323,71 @@ fileListUpdated(event: CustomEvent) {
323323
}
324324
//Before performing any operations- GET or SET- ensure that the this.editor instance is available
325325
//send email also without backend
326-
async sendEmail() {
327-
if (this.editor) {
328-
// Access properties or methods of the TinyMCE editor instance
329-
this.editorContent = this.editor.getContent();
330-
console.log('Content from TinyMCE editor:', this.editorContent);
331-
try {
332-
const response = await fetch('https://reqres.in/api/users', {
333-
method: 'POST',
334-
headers: {
335-
'Content-Type': 'application/json',
336-
},
337-
body: JSON.stringify({ name: this.userName,email:this.userEmail,file:this.fileUploaded,content: this.editorContent }),
338-
});
339-
340-
if (!response.ok) {
341-
throw new Error('Failed to submit content');
342-
}
343-
344-
const result = await response.json();
345-
this.responseMessage = 'Content submitted successfully!';
346-
console.log('Response:', result);
347-
} catch (error) {
348-
console.error('Error:', error);
349-
this.responseMessage = 'Submission failed!';
350-
}
351-
352-
// Ensure all required fields are filled
353-
if (!this.userName || !this.userEmail || !this.editorContent) {
354-
alert('Please fill out first 3 fields before submitting.');
326+
async sendEmail() {
327+
if (this.editor) {
328+
this.editorContent = this.editor.getContent().trim(); // Trim to remove extra spaces
329+
console.log('Editor Content:', this.editorContent);
330+
} else {
331+
console.error('TinyMCE editor instance not available.');
355332
return;
356333
}
357-
if (!this.uploadedFile) {
358-
console.error("No file uploaded.");
334+
335+
// Debugging logs
336+
console.log('User Name:', this.userName);
337+
console.log('User Email:', this.userEmail);
338+
console.log('Editor Content:', this.editorContent);
339+
console.log('Uploaded File:', this.uploadedFile);
340+
341+
// Ensure required fields are filled
342+
if (!this.userName?.trim() || !this.userEmail?.trim() || !this.editorContent?.trim()) {
343+
alert('Please fill out the first 3 fields before submitting.');
359344
return;
360345
}
361-
362-
const formData = new FormData();
363-
formData.append('file', this.uploadedFile);
364-
formData.append('name', this.userName);
365-
formData.append('email', this.userEmail);
366-
formData.append('message', this.editorContent);
367-
// Call EmailJS to send the email (Browser based or client based)
368-
//Emailjs dashboard: https://dashboard.emailjs.com/sign-up
369-
//templateParams is this.editorContent
370-
//Template ID not found: https://dashboard.emailjs.com/admin/templates
371-
emailjs.send(
372-
'service_2q5gm3h', // Email service ID from EmailJS dashboard
373-
'template_szeawas', // Template ID from EmailJS dashboard
374-
{ user_name: this.userName,
375-
user_email: this.userEmail,
376-
content: this.editorContent,
377-
file: this.uploadedFile }, // Template parameters (the content of the email)
378-
{
379-
publicKey: 'IRGsyXDXq7ZJHMbzF',
380-
}// Your user ID from EmailJS: YOUR_PUBLIC_KEY
381-
)
382-
.then((response) => {
383-
console.log('Email sent successfully', response.status, response.text);
384-
alert('Email sent successfully!');
385-
386-
// Sending auto-reply email
387-
emailjs
388-
.send(
389-
'service_2q5gm3h', // Your EmailJS service ID
390-
'template_57gcu0o', // Auto-reply template ID
346+
347+
try {
348+
let base64File = null;
349+
let filename = null;
350+
351+
// Convert file to Base64 only if a file is uploaded
352+
if (this.uploadedFile) {
353+
base64File = await this.convertFileToBase64(this.uploadedFile);
354+
filename = this.uploadedFile.name;
355+
}
356+
357+
const emailResponse = await emailjs.send(
358+
'service_2q5gm3h',
359+
'template_szeawas',
391360
{
392-
user_name: this.userName,
393-
user_email: this.userEmail,
361+
user_name: this.userName.trim(),
362+
user_email: this.userEmail.trim(),
363+
content: this.editorContent.trim(),
364+
attachment: base64File, // Will be null if no file is uploaded
365+
filename: filename, // Will be null if no file is uploaded
394366
},
395-
'IRGsyXDXq7ZJHMbzF' // Your EmailJS user ID
396-
)
397-
.then((response) => {
398-
console.log('Auto-reply email sent successfully', response);
399-
alert('Feedback sent, and an auto-reply email was sent to the user!');
400-
})
401-
.catch((error) => {
402-
console.error('Error sending auto-reply email', error);
403-
});
404-
405-
}) //this is first feedback then endinng parantheses, auto-reply is placed inside this above
406-
.catch((error) => {
367+
{ publicKey: 'IRGsyXDXq7ZJHMbzF' }
368+
);
369+
370+
console.log('Email sent successfully', emailResponse.status, emailResponse.text);
371+
alert('Email sent successfully!');
372+
} catch (error) {
407373
console.error('Error sending email', error);
408374
alert('Failed to send email');
375+
}
376+
}
377+
378+
379+
/**
380+
* Converts a File to Base64 format for EmailJS
381+
*/
382+
convertFileToBase64(file: File): Promise<string> {
383+
return new Promise((resolve, reject) => {
384+
const reader = new FileReader();
385+
reader.readAsDataURL(file);
386+
reader.onload = () => resolve(reader.result as string);
387+
reader.onerror = (error) => reject(error);
409388
});
410-
} else {
411-
console.error('TinyMCE editor instance not available.');
412-
return null;
413-
}
414-
}
389+
}
390+
415391

416392
setContentInEditor(newContent: string) {
417393
if (this.editor) {

0 commit comments

Comments
 (0)