mailto 패키지는 Flutter 개발자가 앱에서 직접 이메일을 보낼 수 있도록 하는 간단한 Dart 라이브러리입니다. 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:
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.awaitlaunch('$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
Clone the repository
Change directory to cd example/flutter
flutter run and wait for the app to start
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 = awaitHttpServer.bind(InternetAddress.loopbackIPv4, 3000);
StringrenderHtml(Mailto mailto) => '''<html><head><title>mailto example</title></head><body><a href="$mailto">Open mail client</a></body></html>''';
awaitfor (HttpRequest request in server) {
request.response
..statusCode = HttpStatus.ok
..headers.contentType = ContentType.html
..write(renderHtml(mailto));
await request.response.close();
}
}
Clone the repository
Change directory to cd example/http_server
Start HTTP server dart main.dart
Open your browser and visit localhost:3000
Click on the link
If you have an email client installed on your computer, this client will be opened when you click the link on the HTML page.