masamune_mail 3.2.0
masamune_mail: ^3.2.0 copied to clipboard
Plug-in for sending emails via Sendgrid, etc. from servers, etc. using Functions.
Masamune Mail
[GitHub] | [YouTube] | [Packages] | [X] | [LinkedIn] | [mathru.net]
Masamune Mail #
Usage #
Installation #
Add the package to your project.
flutter pub add masamune_mail
Run flutter pub get when editing pubspec.yaml manually.
Register the Adapter #
Register MailMasamuneAdapter before running the app. Combine it with a Functions adapter that can call your backend email endpoints.
// lib/adapter.dart
/// Masamune adapters used by the application.
final masamuneAdapters = <MasamuneAdapter>[
const UniversalMasamuneAdapter(),
const MailMasamuneAdapter(),
];
MailMasamuneAdapter.primary exposes the adapter instance when needed.
Send Email via Cloud Functions #
This package provides FunctionsAction classes to send emails through your backend. Your backend must implement the actual email sending logic using SendGrid, or other email service providers.
SendGrid Example:
import 'package:masamune_functions/masamune_functions.dart';
import 'package:masamune_mail/masamune_mail.dart';
// In your controller or page
final functions = ref.app.functions();
Future<void> sendWelcomeEmail(String userEmail) async {
try {
await functions.execute(
SendGridFunctionsAction(
from: "support@example.com",
to: userEmail,
title: "Welcome!",
content: "Thank you for signing up. We're excited to have you!",
),
);
print("Email sent successfully");
} catch (e) {
print("Failed to send email: $e");
}
}
Backend Implementation #
Your Masamune Functions backend must handle the send_grid actions:
SendGrid Backend Example:
// Cloud Functions
if (action === "send_grid") {
const { from, to, title, content } = data;
// Use SendGrid SDK
await sendgrid.send({
to: to,
from: from,
subject: title,
text: content,
});
return { success: true };
}
Tips #
- Store API keys (SendGrid) securely using environment variables or secret managers.
- Add validation and rate limiting to your backend endpoints to prevent abuse.
- Log email sends for audit purposes and monitor for delivery issues.
GitHub Sponsors #
Sponsors are always welcome. Thank you for your support!