diff --git a/src/main/java/com/gymtracker/email/acceptance.html b/src/main/java/com/gymtracker/email/acceptance.html new file mode 100644 index 0000000000000000000000000000000000000000..542f19da408fd1cdfb9c910249fa122dd188e27b --- /dev/null +++ b/src/main/java/com/gymtracker/email/acceptance.html @@ -0,0 +1,12 @@ +<html> +<body> + <div style='text-align:center; padding: 20px;'> + <h1>Gym Tracker</h1> + <div style='border: 1px solid #ccc; padding: 20px; margin-top: 20px;'> + <h2>Congratulations, {name}!</h2> + <p>You have been accepted for the application in <strong>{gymName}</strong>.</p> + <p>Click <a href="{website}">here</a> to visit our website.</p> + </div> + </div> +</body> +</html> \ No newline at end of file diff --git a/src/main/java/com/gymtracker/email/rejection.html b/src/main/java/com/gymtracker/email/rejection.html new file mode 100644 index 0000000000000000000000000000000000000000..d473e191340f25940443f774a599ebd141513744 --- /dev/null +++ b/src/main/java/com/gymtracker/email/rejection.html @@ -0,0 +1,12 @@ +<html> +<body> + <div style='text-align:center; padding: 20px;'> + <h1>Gym Tracker</h1> + <div style='border: 1px solid #ccc; padding: 20px; margin-top: 20px;'> + <h2>Sorry, {name}!</h2> + <p>We regret to inform you that your application for {gymName} has been rejected.</p> + <p>Click <a href='{website}'>here</a> to visit our website for more information.</p> + </div> + </div> +</body> +</html> \ No newline at end of file diff --git a/src/main/java/com/gymtracker/handler/LogAuthHandler.java b/src/main/java/com/gymtracker/handler/LogAuthHandler.java index 5b7750f923e8308eec3076be3d2564a10de10d82..851c0cc0985aa9ec392e8861e28eece11bed16ad 100644 --- a/src/main/java/com/gymtracker/handler/LogAuthHandler.java +++ b/src/main/java/com/gymtracker/handler/LogAuthHandler.java @@ -2,7 +2,7 @@ package com.gymtracker.handler; import com.gymtracker.util.HibernateUtil; import com.gymtracker.util.Util; -import com.gymtracker.model.Logging; +import com.gymtracker.service.Logging; import io.github.cdimascio.dotenv.Dotenv; import org.hibernate.Session; import org.hibernate.SessionFactory; diff --git a/src/main/java/com/gymtracker/service/Email.java b/src/main/java/com/gymtracker/service/Email.java new file mode 100644 index 0000000000000000000000000000000000000000..6bcce2c01186d84aaf9da6612598f62d6d8fdfe1 --- /dev/null +++ b/src/main/java/com/gymtracker/service/Email.java @@ -0,0 +1,93 @@ +package com.gymtracker.service; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import javax.mail.*; +import javax.mail.internet.*; + +import io.github.cdimascio.dotenv.Dotenv; + +public class Email { + + private static final Dotenv dotenv = Dotenv.configure().directory("/").load(); + private static final String username_gt = dotenv.get("USERNAME_GYM_TRACKER"); + private static final String password_gt = dotenv.get("PASSWORD_GYM_TRACKER"); + private static final String host = dotenv.get("SMTP_HOST"); + private static final String port = dotenv.get("SMTP_PORT"); + private static final String website = dotenv.get("WEBSITE_URL"); + + public static void sendMail(String recipientEmail, Boolean isAccepted, String gymName, String username, String name) { + System.out.println("Sending email..."); + System.out.println("username_gt: " + Email.username_gt); + Properties props = new Properties(); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + props.put("mail.smtp.host", Email.host); + props.put("mail.smtp.port", Email.port); + props.put("mail.smtp.ssl.protocols", "TLSv1.2"); + + Session session = Session.getInstance(props, new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(Email.username_gt, Email.password_gt); + } + }); + + try { + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress(Email.username_gt)); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail)); + message.setSubject("Gym Tracker Application Status"); + + // Read the HTML content from files + String htmlContent; + if (isAccepted) { + htmlContent = readFile("acceptance.html"); + } else { + htmlContent = readFile("rejection.html"); + } + + // Replace inside the HTML content with the correct values + htmlContent = htmlContent.replace("{gymName}", gymName); + htmlContent = htmlContent.replace("{name}", name); + htmlContent = htmlContent.replace("{website}", Email.website); + + // set the actual HTML content + message.setContent(htmlContent, "text/html"); + + Transport.send(message); + System.out.println(htmlContent); + + System.out.println("Email sent successfully."); + } catch (MessagingException e) { + e.printStackTrace(); + System.out.println("Failed to send email."); + } + } + + private static String readFile(String filename) { + String content = ""; + + try { + Path path = Paths.get("src/main/java/com/gymtracker/email/" + filename); + byte[] bytes = Files.readAllBytes(path); + content = new String(bytes, StandardCharsets.UTF_8); + } catch (IOException e) { + e.printStackTrace(); + } + return content; + } + + public static void main(String[] args) { + String recipientEmail = "trainer1@gmail.com"; + String gymName = "Example Gym"; + String username = "exampleuser"; + String name = "Nigel Sahl"; + Boolean isAccepted = true; + + Email.sendMail(recipientEmail, isAccepted, gymName, username, name); + } +} \ No newline at end of file diff --git a/src/main/resources/.env.example b/src/main/resources/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..716fe77599f2846fc093e909f7dcf0a75fabfbff --- /dev/null +++ b/src/main/resources/.env.example @@ -0,0 +1,13 @@ +# Taruh di dalam foldersrc/main/resources +MYSQL_DATABASE=<database_name> +MYSQL_USER=<user_mysql> +MYSQL_ROOT_PASSWORD=<password_root_mysql> +MYSQL_PASSWORD=<password_mysql> +MYSQL_TCP_PORT=<port_mysql> +PHP_KEY=<api_key_php> +NODE_KEY=<api_key_node> +EMAIL_GYM_TRACKER=<email_gym_tracker> +PASSWORD_GYM_TRACKER=<password_gym_tracker> +SMTP_HOST=<smtp_host> +SMTP_PORT=<smtp_port> ex: 587 +WEBSITE_URL=<website_url> ex: http://localhost:8080/gym-tracker \ No newline at end of file