package com.sygnalist.api; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.firebase.auth.FirebaseAuthException; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import com.sygnalist.api.models.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.mail.*; import javax.mail.internet.*; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import java.util.concurrent.ExecutionException; @RestController @RequestMapping(path = "/utils/", headers = {"content-type=application/json"}) public class EmailController { @GetMapping("/sendEmail") public void sendEmail(String hash) throws MessagingException, IOException, ExecutionException, InterruptedException, FirebaseAuthException { EmailConstructor emailConstructor = new EmailConstructor(); Report report = ReportFetcher.FetchReport(hash); Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "sygnalist.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("zgloszenie@sygnalist.com", "Dmu`Q@{UW&;vX{6Q"); } }); List emails = report.getRecipientList(); for (String email : emails) { Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("zgloszenie@sygnalist.com", false)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); msg.setSubject("Zgłoszenie z aplikacji sygnalista"); msg.setContent("Zgłoszenie z aplikacji sygnalista", "text/html; charset=UTF-8"); msg.setSentDate(new Date()); MimeBodyPart messageBodyPart = new MimeBodyPart(); MimeUtility.encodeText(emailConstructor.CreateSygnalistaEmail(report, email), "UTF-8", "B"); messageBodyPart.setContent(emailConstructor.CreateSygnalistaEmail(report, email), "text/html; charset=UTF-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); msg.setContent(multipart); Transport.send(msg); } } @GetMapping("/report") public void GetReport(String hash) throws ExecutionException, InterruptedException { ReportFetcher.FetchReport(hash); } @PostMapping("/sendEmailKids") public void sendEmail(@RequestBody KidsReportEmail kidsReportEmail) { ObjectMapper objectMapper = new ObjectMapper(); try { String objStr = ""; objStr = objectMapper.writeValueAsString(kidsReportEmail); System.out.println(objStr); } catch (JsonProcessingException e) { e.printStackTrace(); } try { System.out.println(kidsReportEmail.toString()); sendmailkids(kidsReportEmail); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @PostMapping("/sendEmailPaparazzi") public void sendEmailPaparazzi(@RequestBody PaparazziEmail paparazziEmail) { ObjectMapper objectMapper = new ObjectMapper(); try { String objStr = ""; objStr = objectMapper.writeValueAsString(paparazziEmail); System.out.println(objStr); } catch (JsonProcessingException e) { e.printStackTrace(); } try { System.out.println(paparazziEmail.toString()); sendMailPaparazzi(paparazziEmail); } catch (MessagingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void sendmailkids(KidsReportEmail reportEmail) throws AddressException, MessagingException, IOException { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "malysygnalista.pl"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("zgloszenie@malysygnalista.pl", "MalySygnalistaZgloszenie123*()"); } }); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("zgloszenie@malysygnalista.pl", false)); //tutaj jest do kogo wysyłąne? //ZMIANY 18.09.2019 InternetAddress[] emails = InternetAddress.parse(/*reportEmail.institution.email + "\n" + */(/*reportEmail.sendToMe ?*/ reportEmail.targetEmail/* : ""*/)); //GOTCH YA msg.setRecipients(Message.RecipientType.TO, emails); msg.setSubject("Zgłoszenie z aplikacji Mały Sygnalista"); msg.setContent("Zgłoszenie z aplikacji Mały Sygnalista", "text/html; charset=UTF-8"); msg.setSentDate(new Date()); MimeBodyPart messageBodyPart = new MimeBodyPart(); MimeUtility.encodeText(createEmailContentForKids(reportEmail), "UTF-8", "B"); messageBodyPart.setContent(createEmailContentForKids(reportEmail), "text/html; charset=UTF-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); //MimeBodyPart attachPart = new MimeBodyPart(); //attachPart.attachFile("/var/tmp/image19.png"); //multipart.addBodyPart(attachPart); msg.setContent(multipart); Transport.send(msg); } private void sendMailPaparazzi(PaparazziEmail paparazziEmail) throws AddressException, MessagingException, IOException { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "malysygnalista.pl"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("zgloszenie@malysygnalista.pl", "MalySygnalistaZgloszenie123*()"); } }); Message msg = new MimeMessage(session); msg.setFrom(new InternetAddress("zgloszenie@malysygnalista.pl", false)); InternetAddress[] emails = InternetAddress.parse(paparazziEmail.email); msg.setRecipients(Message.RecipientType.TO, emails); msg.setSubject("Zgłoszenie z aplikacji Paparazzi"); msg.setContent("Zgłoszenie z aplikacji Paparazzi", "text/html; charset=UTF-8"); msg.setSentDate(new Date()); MimeBodyPart messageBodyPart = new MimeBodyPart(); MimeUtility.encodeText(createEmailContentPaparazzi(paparazziEmail), "UTF-8", "B"); messageBodyPart.setContent(createEmailContentPaparazzi(paparazziEmail), "text/html; charset=UTF-8"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); //MimeBodyPart attachPart = new MimeBodyPart(); //attachPart.attachFile("/var/tmp/image19.png"); //multipart.addBodyPart(attachPart); msg.setContent(multipart); Transport.send(msg); } private String createEmailContent(ReportEmail reportEmail) { List urls = reportEmail.urls; String attachments = ""; System.out.println(reportEmail.urls.size()); int number = 1; for (String url : urls) { attachments = attachments + " " + url + "
"; number++; } System.out.println("attachments " + attachments); DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); Date date = new Date(); String email = "\n" + " \n" + " \n" + " \n" + " " + " \n" + " \n" + " \n" + " \n" + "
\n" + "
\n" + "
" + dateFormat.format(date) + ", Zgłoszenie nr " + reportEmail.reportNumber + "
\n" + "
\n" + " \n" + "
\n" + " Do:

\n" + "" + reportEmail.institution.getName() + "
" + ((reportEmail.email != null && !reportEmail.email.isEmpty()) ? ("" + reportEmail.institution.getAddress() + "
" + reportEmail.institution.getCity() + "
") : ("" + reportEmail.institution.getEmail() + "
")) + "
\n" + "
ZAWIADOMIENIE" + "
" + reportEmail.problem + "\n" + "
\n" + "
\n" + (!reportEmail.description.isEmpty() ? "Ja niżej podpisany zawiadamiam o treści:
\n" + " " + reportEmail.description + "\n" : "") + // "
Lokalizacja:" + "https://www.google.com/maps?q="+ reportEmail.lat +","+ reportEmail.lng + "

" + "
Lokalizacja:" + "https://www.google.com/maps/search/" + reportEmail.fetchedAddress + "/@" + reportEmail.lat + "," + reportEmail.lng + ",13.5z" + "

" + " Załączniki:
\n" + attachments + "
\n" + "
\n" + " Dane osoby składającej zawiadomienie:
\n" + ((reportEmail.firstName == null || reportEmail.anonymously) ? (" Anonimowy mały sygnalista
") : (" " + reportEmail.firstName + " " + reportEmail.lastName + "
\n" + " " + reportEmail.streetName + " " + reportEmail.flat + (!reportEmail.flatNumber.isEmpty() ? "/" + reportEmail.flatNumber : "") + " " + reportEmail.city + " " + reportEmail.postCode + "
\n" + " Tel: " + reportEmail.phoneNumber + "
\n" + "
\n")) + "
\n" + "
Zawiadomienie wysłane za pośrednictwem aplikacji mały sygnalista
\n" + "" + "
" + "
\n" + " \n" + "\n"; String encodedBody = null; try { encodedBody = new String(email.getBytes("UTF-8"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encodedBody; } private String createEmailContentForKids(KidsReportEmail reportEmail) { List urls = reportEmail.attachments; String attachments = ""; System.out.println(reportEmail.attachments.size()); int number = 1; for (String url : urls) { attachments = attachments + " " + url + "
"; number++; } System.out.println("attachments " + attachments); DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); Date date = new Date(); String email = "\n" + " \n" + " \n" + " \n" + " " + " \n" + " \n" + " \n" + " \n" + "
\n" + "
\n" + "
" + dateFormat.format(date) + ", Zgłoszenie nr " + reportEmail.reportNumber + "
\n" + "
\n" + " \n" + "
\n" + " Do:

\n" + "" + reportEmail.targetName + "
" + ((reportEmail.kidEmail != null && !reportEmail.kidEmail.isEmpty()) ? ("" + reportEmail.targetAddress + "
" + reportEmail.targetCity + "
") : ("" + reportEmail.targetEmail + "
")) + "
\n" + "
ZAWIADOMIENIE" + "
\n" + "
\n" + (!reportEmail.description.isEmpty() ? "Ja niżej podpisany zawiadamiam o:
\n" + " " + reportEmail.description + "\n" : "") + // "
Lokalizacja:" + "https://www.google.com/maps?q="+ reportEmail.lat +","+ reportEmail.lng + "

" + "

Lokalizacja:" + "https://www.google.com/maps/search/" + reportEmail.targetAddress + "/@" + reportEmail.reportLocationLat + "," + reportEmail.reportLocationLng + ",13.5z" + "

" + " Załączniki:
\n" + attachments + "
\n" + "
\n" + "Dane osoby składającej zawiadomienie:
\n" + ((reportEmail.anonymously) ? "Anonimowy sygnalista" : reportEmail.kidName) + "

" + " Dane opiekuna osoby składającej zawiadomienie:
\n" + ((reportEmail.parentName == null || reportEmail.parentName == " " || reportEmail.parentName == "" || reportEmail.anonymously) ? ("Dane ukryte
") : (" " + reportEmail.parentName + "
\n" + " " + reportEmail.parentAddress + "\n" + reportEmail.parentCity + "
\n" + " Tel: " + reportEmail.targetPhoneNumber + "
\n" + "
\n")) + "
\n" + "
Zawiadomienie wysłane za pośrednictwem aplikacji sygnalista
\n" + "" + "
" + "
\n" + " \n" + "\n"; String encodedBody = null; try { encodedBody = new String(email.getBytes("UTF-8"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encodedBody; } private String createEmailContentPaparazzi(PaparazziEmail paparazziEmail) { List urls = paparazziEmail.attachments; String attachments = ""; System.out.println(paparazziEmail.attachments.size()); int number = 1; for (String url : urls) { attachments = attachments + " " + url + "
"; number++; } System.out.println("attachments " + attachments); DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); Date date = new Date(); String email = "\n" + " \n" + " \n" + " \n" + " " + " \n" + " \n" + " \n" + " \n" + "
\n" + "
\n" + "
" + dateFormat.format(date) + ", Materiał nr " + paparazziEmail.reportNumber + "
\n" + "
\n" + "
\n" + " Do:

\n" + "" + paparazziEmail.name + "
" + "" + paparazziEmail.email + "
" + "
\n" + "
\n" + (!paparazziEmail.description.isEmpty() ? "Opis materiału:
\n" + " " + paparazziEmail.description + "\n" : "") + // "
Lokalizacja:" + "https://www.google.com/maps?q="+ reportEmail.lat +","+ reportEmail.lng + "

" + "
Lokalizacja:" + "https://www.google.com/maps/search/" + paparazziEmail.address + "/@" + paparazziEmail.reportLocationLat + "," + paparazziEmail.reportLocationLng + ",13.5z" + "

" + " Załączniki:
\n" + attachments + "
\n" + "
\n" + " Dane osoby składającej materiał:
\n" + ((paparazziEmail.firstName == null || paparazziEmail.anonymously) ? ("Użytkownik anonimowy
") : (" " + paparazziEmail.firstName + " " + paparazziEmail.lastName + "
\n" + " " + paparazziEmail.streetName + " " + paparazziEmail.homeNumber + (!paparazziEmail.flatNumber.isEmpty() ? "/" + paparazziEmail.flatNumber : "") + " " + paparazziEmail.city + " " + paparazziEmail.postCode + "
\n" + " Tel: " + paparazziEmail.phoneNumber + "
\n" + "

Numer konta: " + paparazziEmail.bankAccount + "

" + (paparazziEmail.publishing ? "Wyrażono zgodę na publikację materiału
" : "Nie wyrażono zgody na publikację materiału
") + "
\n")) + "
\n" + "
Materiał wysłany za pośrednictwem aplikacji Paparazzi
\n" + "" + "
" + "
\n" + " \n" + "\n"; String encodedBody = null; try { encodedBody = new String(email.getBytes("UTF-8"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return encodedBody; } private String determineReportType(int type) { if (type == 2) { return "Negatywne"; } else if (type == 1) { return "Pozytywne"; } return ""; } }