using System.Collections; using System.Text; using TMPro; using UnityEngine; using UnityEngine.Networking; // EmailSender.cs // Branche EnvoyerEmail() (sans paramètre) au OnClick du bouton. // Les valeurs sont lues depuis _G au moment du clic. public class EmailSender : MonoBehaviour { [Header("Backend")] [Tooltip("URL de ton backend PHP (avec .php à la fin)")] public string endpoint = "https://ukitchenit.com/api/send-design.php"; [Header("Champs scène")] [Tooltip("Champ email placé sur la scène")] public TMP_InputField emailField; [Tooltip("Panneau à fermer après l'envoi")] public GameObject emailPanel; [Header("Langue")] [Tooltip("\"fr\" ou \"en\"")] public string language = "fr"; [System.Serializable] private class Payload { public string email; public string name; public string lang; public string softwareUrl; public string cartUrl; public string logo; public string pdf; } // ====== FONCTION À BRANCHER AU BOUTON (sans paramètre) ====== public void EnvoyerEmail() { string email = emailField != null ? emailField.text.Trim() : ""; if (string.IsNullOrEmpty(email)) email = _G.designerEmail; string name = _G.Form != null && _G.Form.Count > 0 ? _G.Form[0].text : ""; string softwareUrl = "https://ukitchenit.com/3d?" + _G.PATH+"?ai_"+_G.Form[2].text; string cartUrl = "https://www." + _G.PATH; string logo = "https://ukitchenit.com/api/logos/" + _G.PATH + "logo.png"; string pdf = "https://ukitchenit.com/api/pdfs/" + _G.PATH + ".pdf"; language = _G.L == 1 ? "en" : "fr"; if (string.IsNullOrEmpty(email)) { Debug.LogWarning("[EmailSender] Aucune adresse email disponible (emailField vide et _G.designerEmail vide)."); return; } StartCoroutine(SendCoroutine(email, name, language, softwareUrl, cartUrl, logo, pdf)); } // ============================================================ private IEnumerator SendCoroutine(string email, string name, string lang, string softwareUrl, string cartUrl, string logo, string pdf) { var payload = new Payload { email = email, name = name, lang = lang, softwareUrl = softwareUrl, cartUrl = cartUrl, logo = logo, pdf = pdf }; string json = JsonUtility.ToJson(payload); byte[] body = Encoding.UTF8.GetBytes(json); using (UnityWebRequest req = new(endpoint, "POST")) { req.uploadHandler = new UploadHandlerRaw(body); req.downloadHandler = new DownloadHandlerBuffer(); req.SetRequestHeader("Content-Type", "application/json"); yield return req.SendWebRequest(); if (req.result == UnityWebRequest.Result.Success) { Debug.Log("Email envoyé ! Réponse : " + req.downloadHandler.text); if (emailPanel != null) emailPanel.SetActive(false); _M.PH(18, -420f, "898787", 1, 1); } else Debug.LogError($"Erreur {req.responseCode} : {req.error}\n{req.downloadHandler.text}"); } } }