import axios, { AxiosError } from "axios";
type emailDataType = {
email: string,
header: string,
subject: string,
apiKey:string,
body: string,
};
export const SendCodedMail = async ({emailData}:{emailData:emailDataType})=>{
try {
const response = await axios.post<{ success: boolean; message: string }>(
"https://codedmail.codeddesign.org.za/api/index.php",
emailData
);
// Handle success response
if (response.status === 200) {
return { success:true,message: response.data }
}
} catch (error) {
if (error instanceof AxiosError) {
// Handle error response
if (error.response) {
return { success:false,message:error.response }
} else if (error.request) {
return { success:false,message:error.response }
} else {
return { success:false,message: error.message }
}
} else {
// Handle non-Axios errors
return { success:false,message:"Unexpected Error" }
}
}
}
// 1. Import the SendCodedMail first
import { SendCodedMail } from '@/components/mattcomponents/api/helpers';
// 2. Declare an async function
const SendEmail = async () => {
// 3. Declare an emailData object with all the keys as they are, email,header,subject,apiKey and body
const emailData = {
email: email,
header: "Coded Mail",
subject: "API Key",
apiKey: "4338d085-80b7-4ac2-9866-16942c47bf96",
body: `<h2><strong>Your email: is trying to register again, please use this API KEY instead :</strong></h2> <br>`,
};
// 4. Await the response
const isEmailSuccess = await SendCodedMail({ emailData });
// 5. Test if the email is successful and take action as you wish
if (!isEmailSuccess) {
console.log(isEmailSuccess.message);
} else {
console.log(isEmailSuccess.message);
}
};
// 6. Create a DIV or a BUTTON, as long as it can take an Onclick method to send the email.
<div onClick={SendEmail}> SEND EMAIL </div>