Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add canSend function to check mail sending capability #107

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class EmailSender extends StatefulWidget {
class _EmailSenderState extends State<EmailSender> {
List<String> attachments = [];
bool isHTML = false;
final _canSend = FlutterEmailSender.canSend();

final _recipientController = TextEditingController(
text: 'example@example.com',
Expand Down Expand Up @@ -73,9 +74,18 @@ class _EmailSenderState extends State<EmailSender> {
appBar: AppBar(
title: Text('Plugin example app'),
actions: <Widget>[
IconButton(
onPressed: send,
icon: Icon(Icons.send),
FutureBuilder<bool>(
future: _canSend,
builder: (context, snapshot) {
if (!snapshot.hasData || snapshot.data == false) {
return const SizedBox();
}

return IconButton(
onPressed: send,
icon: Icon(Icons.send),
);
},
)
],
),
Expand Down
7 changes: 7 additions & 0 deletions ios/Classes/SwiftFlutterEmailSenderPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ public class SwiftFlutterEmailSenderPlugin: NSObject, FlutterPlugin {
switch call.method {
case "send":
sendMail(call, result: result)
case "canSend":
canSend(call, result: result)
default:
result(FlutterMethodNotImplemented)
}
}

private func canSend(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let canSend = MFMailComposeViewController.canSendMail();
result(canSend)
}

private func sendMail(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let email = parseArgs(call, result: result) else { return }
Expand Down
16 changes: 16 additions & 0 deletions lib/flutter_email_sender.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';

Expand All @@ -9,6 +10,14 @@ class FlutterEmailSender {
static Future<void> send(Email mail) {
return _channel.invokeMethod('send', mail.toJson());
}

static Future<bool> canSend() async {
if (Platform.isIOS) {
return await _channel.invokeMethod('canSend');
}

return true;
}
}

class Email {
Expand All @@ -19,6 +28,7 @@ class Email {
final String body;
final List<String>? attachmentPaths;
final bool isHTML;

Email({
this.subject = '',
this.recipients = const [],
Expand All @@ -41,3 +51,9 @@ class Email {
};
}
}

extension EmailExt on Email {
Future<void> send() {
return FlutterEmailSender.send(this);
}
}