Home > Mobile >  Share a file into Email from externalStorage directory
Share a file into Email from externalStorage directory

Time:03-28

How can I send a file from external storage directory location like /storage/emulated/0/Android/data/com.abc.myApp/files/AppLog/Logs/28032022.txt' via email. When user clicked on Upload button it'll open email app with attached file. I've to upload single or multiple files from this Logs directory. How could I do something what I say here

ElevatedButton.icon(
onPressed: (() async {
  url = 'mailto:${''}?subject=Subject&body=$AttachedFile';
  launch(url);
}),
icon: Icon(Icons.restore_sharp),
label: Text("Upload Log")
)

In AttachedFile I have to pass the file/path

CodePudding user response:

Solved the issue by using flutter_email_sender package. Just have to call _sendMailWithAttachment() inside Upload button. Package: flutter_email_sender: ^5.1.0

  Future <void> _sendMailWithAttachment() async {
    // give your file path
    final directory = await getExternalStorageDirectory();
    var attachment = File(directory).path;
    final Email email = Email(
      body: '',
      subject: '',
      recipients: [''],
      attachmentPaths: [attachment],
      isHTML: false,
    );
    await FlutterEmailSender.send(email);
  }

CodePudding user response:

  • First Solation

This will add a line like this to your package's pubspec.yaml

mailto: ^2.0.0

url_launcher: ^6.0.3

define the email-send-mail.dart class and use this code.


import 'package:mailto/mailto.dart';
import 'package:url_launcher/url_launcher.dart';

launchMailto() async {
  final mailtoLink = Mailto(
    to: ['[email protected]'],
    cc: ['[email protected]', '[email protected]'],
    subject: 'mailto example subject',
    body: 'mailto example body',
  );
  // Convert the Mailto instance into a string.
  // Use either Dart's string interpolation
  // or the toString() method.
  await launch('$mailtoLink');
}

Note: you must convert your 28032022.txt to string and put it in the body.

  • Secend Solation

This will add a line like this to your package's pubspec.yaml

flutter_email_sender: ^5.1.0

define the email-send-mail.dart class and use this code.

import 'package:flutter_email_sender/flutter_email_sender.dart';

Future<void> sendemail(
    {required List<String> to,
    String subject = "",
    String body = "",
    required List<String> cc,
    required List<String> bcc,
    List<String>? attachmentPaths,
    bool isHTML = false}) async {
  final Email email = Email(
    recipients: to,
    cc: cc,
    bcc: bcc,
    subject: subject,
    body: body,
    attachmentPaths: attachmentPaths,
    isHTML: isHTML,
  );
  await FlutterEmailSender.send(email);
}
  • Related