Dart
Dart integration examples for Keplers Mail Service
Send emails using Dart with Keplers Mail Service REST API.
Installation
Add to your pubspec.yaml:
dependencies:
http: ^1.1.0Basic Setup
# .env
KEPLERS_API_KEY=kms_your_api_key.live_...REST API Integration
Basic Client
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
class KeplersEmailClient {
final String apiKey;
final String baseUrl;
KeplersEmailClient({
required this.apiKey,
this.baseUrl = 'https://api.keplars.com',
});
Future<Map<String, dynamic>> sendEmail({
required String to,
required String subject,
required String body,
bool isHtml = false,
}) async {
return await _makeRequest('/api/v1/send-email/queue', {
'to': [to],
'subject': subject,
'body': body,
'is_html': isHtml,
});
}
Future<Map<String, dynamic>> sendInstantEmail({
required String to,
required String subject,
required String body,
bool isHtml = false,
}) async {
return await _makeRequest('/api/v1/send-email/instant', {
'to': [to],
'subject': subject,
'body': body,
'is_html': isHtml,
});
}
Future<Map<String, dynamic>> _makeRequest(String endpoint, Map<String, dynamic> data) async {
try {
final response = await http.post(
Uri.parse('$baseUrl$endpoint'),
headers: {
'Authorization': 'Bearer $apiKey',
'Content-Type': 'application/json',
},
body: jsonEncode(data),
);
if (response.statusCode == 200) {
return jsonDecode(response.body);
} else {
throw Exception('Failed to send email: ${response.body}');
}
} catch (e) {
throw Exception('Failed to send email: $e');
}
}
}Usage Examples
void main() async {
final client = KeplersEmailClient(
apiKey: Platform.environment['KEPLERS_API_KEY'] ?? '',
);
await sendWelcomeEmail(client, '[email protected]', 'John');
await sendVerificationEmail(client, '[email protected]', '123456');
}
Future<void> sendWelcomeEmail(KeplersEmailClient client, String userEmail, String userName) async {
try {
final result = await client.sendEmail(
to: userEmail,
subject: 'Welcome $userName!',
body: '<h1>Welcome $userName!</h1><p>Thank you for joining our platform.</p>',
isHtml: true,
);
print('Email sent: $result');
} catch (e) {
print('Failed to send email: $e');
}
}
Future<void> sendVerificationEmail(KeplersEmailClient client, String userEmail, String code) async {
try {
final result = await client.sendInstantEmail(
to: userEmail,
subject: 'Verify Your Email',
body: 'Your verification code: $code',
isHtml: false,
);
print('Verification email sent: $result');
} catch (e) {
print('Failed to send verification email: $e');
}
}Flutter Integration
class EmailService {
static final _client = KeplersEmailClient(
apiKey: const String.fromEnvironment('KEPLERS_API_KEY'),
);
static Future<bool> sendWelcomeEmail(String email, String name) async {
try {
await _client.sendEmail(
to: email,
subject: 'Welcome $name!',
body: '<h1>Welcome $name!</h1>',
isHtml: true,
);
return true;
} catch (e) {
print('Failed to send email: $e');
return false;
}
}
}
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State<LoginScreen> {
final _emailController = TextEditingController();
bool _isLoading = false;
Future<void> _sendEmail() async {
setState(() => _isLoading = true);
final success = await EmailService.sendWelcomeEmail(
_emailController.text,
'User',
);
setState(() => _isLoading = false);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(success ? 'Email sent!' : 'Failed to send email'),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
TextField(
controller: _emailController,
decoration: InputDecoration(labelText: 'Email'),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _isLoading ? null : _sendEmail,
child: _isLoading
? CircularProgressIndicator()
: Text('Send Email'),
),
],
),
),
);
}
}SMTP Integration
SMTP Installation
dependencies:
mailer: ^6.0.1SMTP Client
import 'package:mailer/mailer.dart';
import 'package:mailer/smtp_server.dart';
class KeplersEmailSMTPClient {
final String username;
final String password;
late final SmtpServer _smtpServer;
KeplersEmailSMTPClient({
required this.username,
required this.password,
}) {
_smtpServer = SmtpServer(
'smtp.keplars.com',
port: 587,
username: username,
password: password,
);
}
Future<bool> sendEmail({
required String to,
required String subject,
required String body,
bool isHtml = false,
}) async {
try {
final message = Message()
..from = Address('[email protected]', 'Keplers Mail Service')
..recipients.add(to)
..subject = subject;
if (isHtml) {
message.html = body;
} else {
message.text = body;
}
final sendReport = await send(message, _smtpServer);
return sendReport.sent.isNotEmpty;
} catch (e) {
print('SMTP Error: $e');
return false;
}
}
}SMTP Usage
void main() async {
final smtpClient = KeplersEmailSMTPClient(
username: Platform.environment['KEPLERS_SMTP_USERNAME'] ?? '',
password: Platform.environment['KEPLERS_SMTP_PASSWORD'] ?? '',
);
await sendEmailViaSMTP(smtpClient, '[email protected]', 'Test Email', '<p>Hello World!</p>');
}
Future<void> sendEmailViaSMTP(
KeplersEmailSMTPClient smtpClient,
String userEmail,
String subject,
String body,
) async {
final success = await smtpClient.sendEmail(
to: userEmail,
subject: subject,
body: body,
isHtml: true,
);
if (success) {
print('SMTP email sent successfully');
} else {
print('SMTP send failed');
}
}Choose REST API for advanced features or SMTP for traditional email client integration.