facebook twitter hatena line email

Flutter/外部ライブラリ/メール送信/flutter email sender

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

flutter email senderとは

直でflutterからメール送信できるライブラリ

公式

https://pub.dev/packages/flutter_email_sender

インストール

pubspec.yaml

dependencies:
  flutter_email_sender: ^2.2.1

サンプル

import 'package:flutter_email_sender/flutter_email_sender.dart';
final Email email = Email(
  body: 'Email body',
  subject: 'Email subject',
  recipients: ['example@example.com'],
  cc: ['cc@example.com'],
  bcc: ['bcc@example.com'],
  attachmentPath: '/path/to/attachment.zip',
  isHTML: false,
);
await FlutterEmailSender.send(email);

成功するとandroidだとgmailのページがアプリ内で開く。iosだと内製メーラーが開く。

エラーが出てしまう

androidx対応してるライブラリっぽいので、導入側プロジェクトにandroidxが対応しておらずエラーになってた。androidx対応したところ直った。

~/android/app/src/debug/AndroidManifest.xml:22:18-91 Error:
	Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
	is also present at [androidx.core:core:1.0.2] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
	Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:11:5-34:19 to override.

メール送信エラー

エラー詳細

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(not_available, No email clients found!, null, null)

対応方法

android/app/src/main/AndroidManifest.xml

<intent-filter>
                <action android:name="android.intent.action.SENDTO" />
                <data android:scheme="mailto" />
</intent-filter>

参考:https://stackoverflow.com/questions/73234732/flutter-email-sender-doesnt-send-email