搜索插件、代码仓与 APP 分类

插件详情v2.0.0

mailto

用于在您的 Flutter 应用中创建 mailto 链接的简单 Dart 包

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2.9万
喜欢数
139
Pub 分数
160

安装插件

flutter pub add mailto

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

mailto 包是一个简单的 Dart 库,可让 Flutter 开发者生成 mailto 链接,直接从其应用中发送电子邮件。它支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。该包提供了一个易于使用的 API,可用于构建带有预定义收件人、主题、正文和其他参数的电子邮件链接。非常适合用于联系表单、支持按钮或任何需要邮件集成而无需外部依赖的功能。通过自动处理 URL 编码和格式化,确保跨平台行为一致。

适合场景

  • 创建联系表单
  • 添加支持按钮
  • 生成电子邮件邀请
  • 与用户反馈系统集成
  • 简化电子邮件共享

主要特点

  • 轻松生成 mailto 链接
  • 跨平台兼容性
  • 支持 subject 和 body 参数
  • 自动URL编码
  • 最小依赖开销

适合对象

  • 简单的电子邮件操作
  • 移动和网页应用
  • 轻量级邮件集成
  • 寻求最小化设置的开发者

使用建议

  • 在某些平台上不处理电子邮件客户端选择
  • 仅限基本电子邮件功能
  • 电子邮件客户端没有内置的错误处理
  • 需要用户配置电子邮件应用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

mailto

Simple Dart package for creating mailto links in your Flutter and Dart apps

The mailto package helps you build mailto links and provides you with an idiomatic Dart interface that:

  • supports one or many to, cc, and bcc fields
  • supports custom body and subject for the emails
  • encodes every value for your correctly
  • is blazingly fast ⚡️😜

smaho-engineering/mailto

Build Status Code coverage

mailto GitHub Stars Count

Important links

Usage

You may want to launch the email client on your user's phone with certain fields pre-filled. For Flutter apps, it's recommended to use the url_launcher package for launching the links you create with the mailto package.

import 'package:mailto/mailto.dart';
// For Flutter applications, you'll most likely want to use
// the url_launcher package.
import 'package:url_launcher/url_launcher.dart';

// ...somewhere in your Flutter app...
launchMailto() async {
  final mailtoLink = Mailto(
    to: ['to@example.com'],
    cc: ['cc1@example.com', 'cc2@example.com'],
    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');
}
Validation

The package provides a simple validation function. You could use this function in an assert to catch issues in development mode.

The package doesn't validate automatically, so either use the validation function or make sure that the parameters you use are correct.

Mailto.validateParameters(
  // New lines are NOT supported in subject lines
  subject: 'new lines in subject \n FTW',
  // What does this even mean?
  cc: ['\n\n\n', ''],
);

Known limitations of mailto URIs

I tested the package manually in Flutter apps on iOS and Android (Gmail, FastMail, Yahoo email client), and in the browser on macOS, and the package has an extensive test suite that incorporates many examples from the RFC 6068 - The 'mailto' URI Scheme document.

Unfortunately, each client handle mailto links differently: Gmail does not add line-breaks in the message body, FastMail skips the bcc, Yahoo is not able to handle encoded values in subject and body, and these are only the three clients I tested. The iOS email client seems to handle everything well, so 🎸🤘🎉.

The package might also not work if the resulting mailto links are extremely long. I don't know the exact character count where the links fail, but I'd try to keep things under 1000 characters.

Important: Make sure you understand these limitations before you decide to incorporate mailto links in your app: letting users open their email clients with pre-filled values is a quick and easy way to let your users get in touch with you with extremely little development effort. At the same time, you need to keep in mind that it's very unlikely that these links are going to work consistently for all of your users.

If you need something bullet-proof, this package is not the right tool for solving your problem, so please consider alternative solutions (e.g. Flutter forms and a working backend).

In case you find potential improvements to the package, please create a pull request or let's discuss it in an issue. I might not merge all pull requests, especially changes that improve things for one client, but makes it worse for others. We consider the iOS mail app and Gmail on Android the two most important mail clients.

Examples

You'll find runnable, great examples on the project's GitHub repository in the /example folder.

Flutter example app
  1. Clone the repository
  2. Change directory to cd example/flutter
  3. flutter run and wait for the app to start
  4. You can fill out the forms with your own input or click the "Surprise me" button to see how your mail client handles tricky input
HTTP server serving an HTML web page with a mailto link

The mailto package works in any Dart program: be it Flutter, AngularDart, or on the server.

import 'dart:io';

import 'package:mailto/mailto.dart';

Future<void> main() async { 
  final mailto = Mailto(
    to: [
      'example@example.com',
      'ejemplo@ejemplo.com',
    ],
    cc: [
      'percentage%100@example.com',
      'QuestionMark?address@example.com',
    ],
    bcc: [
      'Mike&family@example.org',
    ],
    subject: 'Let\'s drink a "café"! ☕️ 2+2=4 #coffeeAndMath',
    body:
        'Hello this if the first line!\n\nNew line with some special characters őúóüűáéèßáñ\nEmoji: 🤪💙👍',
  );

  final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 3000);
  String renderHtml(Mailto mailto) => '''<html><head><title>mailto example</title></head><body><a href="$mailto">Open mail client</a></body></html>''';
  await for (HttpRequest request in server) {
    request.response
      ..statusCode = HttpStatus.ok
      ..headers.contentType = ContentType.html
      ..write(renderHtml(mailto));
    await request.response.close();
  }
}
  1. Clone the repository
  2. Change directory to cd example/http_server
  3. Start HTTP server dart main.dart
  4. Open your browser and visit localhost:3000
  5. Click on the link
  6. If you have an email client installed on your computer, this client will be opened when you click the link on the HTML page.
Screenshots
mailto demo: Dart server HTML mailto demo: MacOS email client

所属分类

源码仓库

使用此插件的项目