Masamune Mail
[GitHub](https://github.com/mathrunet) | [YouTube](https://www.youtube.com/c/mathrunetchannel) | [Packages](https://pub.dev/publishers/mathru.net/packages) | [X](https://x.com/mathru) | [LinkedIn](https://www.linkedin.com/in/mathrunet/) | [mathru.net](https://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, Gmail, 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");
}
}
Gmail Example:
await functions.execute(
SendGmailFunctionsAction(
from: "noreply@example.com",
to: "recipient@example.com",
title: "Monthly Report",
content: "Please check the attached report for this month.",
),
);
Backend Implementation
Your Masamune Functions backend must handle the send_grid and gmail 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 };
}
Gmail Backend Example:
// Cloud Functions
if (action === "gmail") {
const { from, to, title, content } = data;
// Use Gmail API
await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: createMimeMessage(from, to, title, content),
},
});
return { success: true };
}
Tips
- Store API keys (SendGrid, Gmail OAuth) 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!
Libraries
- masamune_mail
- Plug-in for sending emails via Sendgrid, Gmail, etc. from servers, etc. using Functions.