Build and Release FLUX (Multi-Platform) / build-web (push) Successful in 2m7s
Build and Release FLUX (Multi-Platform) / build-macos (push) Successful in 2m30s
Build and Release FLUX (Multi-Platform) / build-linux (push) Successful in 1m56s
Build and Release FLUX (Multi-Platform) / build-windows (push) Successful in 8m26s
137 lines
3.8 KiB
Dart
137 lines
3.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flux/core/services/email/email_response_model.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
/// Account mittente configurato sulla Edge Function `send-email`.
|
|
enum EmailSenderAccount {
|
|
ordini('ordini'),
|
|
assistenza('assistenza');
|
|
|
|
final String value;
|
|
const EmailSenderAccount(this.value);
|
|
}
|
|
|
|
class EmailService {
|
|
static const String _functionName = 'send-email';
|
|
static final RegExp _emailRegex = RegExp(r'^[^@\s]+@[^@\s]+\.[^@\s]+$');
|
|
|
|
final SupabaseClient _supabase;
|
|
|
|
EmailService(this._supabase);
|
|
|
|
/// Invia un'email tramite la Edge Function `send-email` (SMTP Stalwart).
|
|
Future<EmailResponse> sendEmail({
|
|
required String to,
|
|
required String subject,
|
|
required String text,
|
|
String? html,
|
|
String? replyTo,
|
|
EmailSenderAccount account = EmailSenderAccount.ordini,
|
|
}) async {
|
|
final cleanedTo = to.trim();
|
|
final cleanedSubject = subject.trim();
|
|
final cleanedText = text.trim();
|
|
|
|
if (cleanedTo.isEmpty) {
|
|
return const EmailResponse(
|
|
success: false,
|
|
error: 'Indirizzo email del cliente mancante.',
|
|
);
|
|
}
|
|
|
|
if (!_emailRegex.hasMatch(cleanedTo)) {
|
|
return EmailResponse(
|
|
success: false,
|
|
to: cleanedTo,
|
|
error: 'Indirizzo email non valido: $cleanedTo',
|
|
);
|
|
}
|
|
|
|
if (cleanedSubject.isEmpty || (cleanedText.isEmpty && (html?.trim().isEmpty ?? true))) {
|
|
return EmailResponse(
|
|
success: false,
|
|
to: cleanedTo,
|
|
error: 'Oggetto o contenuto email mancanti.',
|
|
);
|
|
}
|
|
|
|
try {
|
|
final response = await _supabase.functions.invoke(
|
|
_functionName,
|
|
body: {
|
|
'to': cleanedTo,
|
|
'subject': cleanedSubject,
|
|
'text': cleanedText,
|
|
'account': account.value,
|
|
if (html != null && html.trim().isNotEmpty) 'html': html.trim(),
|
|
if (replyTo != null && replyTo.trim().isNotEmpty)
|
|
'replyTo': replyTo.trim(),
|
|
},
|
|
);
|
|
|
|
final data = _decodeData(response.data);
|
|
if (data != null) {
|
|
return EmailResponse.fromMap(data);
|
|
}
|
|
|
|
return EmailResponse(
|
|
success: response.status >= 200 && response.status < 300,
|
|
to: cleanedTo,
|
|
error: response.status >= 200 && response.status < 300
|
|
? null
|
|
: 'Risposta non valida dal server email (HTTP ${response.status}).',
|
|
);
|
|
} on FunctionException catch (e) {
|
|
debugPrint('EmailService FunctionException (${e.status}): ${e.details}');
|
|
final details = _decodeData(e.details);
|
|
if (details != null) {
|
|
final parsed = EmailResponse.fromMap(details);
|
|
return parsed.copyWith(
|
|
success: false,
|
|
to: parsed.to ?? cleanedTo,
|
|
error: parsed.error ?? e.reasonPhrase ?? 'Errore invio email.',
|
|
);
|
|
}
|
|
return EmailResponse(
|
|
success: false,
|
|
to: cleanedTo,
|
|
error:
|
|
e.reasonPhrase ??
|
|
'Errore di comunicazione con il server email (${e.status}).',
|
|
);
|
|
} catch (e) {
|
|
debugPrint('EmailService error: $e');
|
|
return EmailResponse(
|
|
success: false,
|
|
to: cleanedTo,
|
|
error: 'Impossibile contattare il servizio email: $e',
|
|
);
|
|
}
|
|
}
|
|
|
|
Map<String, dynamic>? _decodeData(dynamic raw) {
|
|
if (raw is Map<String, dynamic>) {
|
|
return raw;
|
|
}
|
|
if (raw is Map) {
|
|
return Map<String, dynamic>.from(raw);
|
|
}
|
|
if (raw is String && raw.trim().isNotEmpty) {
|
|
try {
|
|
final decoded = jsonDecode(raw);
|
|
if (decoded is Map<String, dynamic>) {
|
|
return decoded;
|
|
}
|
|
if (decoded is Map) {
|
|
return Map<String, dynamic>.from(decoded);
|
|
}
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|