Flutter邮件发送插件mandrill的使用
Flutter邮件发送插件mandrill的使用
Mandrill API
这是一个Mandrill API的原生Dart实现客户端。
它不是其他mandrill_api
包的分支,因为其他库已经过时且未维护,并不适合移植到Dart 2.0。
使用
import 'package:mandrill/mandrill_server.dart';
// 或者如果你在浏览器中使用:
// import 'package:mandrill/mandrill_browser.dart';
final apiKey = 'your-key';
void main() async {
final mandrill = createMandrill(apiKey);
final recipients = [
new Recipient(email: 'customer1@example.com', name: 'Customer 1'),
new Recipient(email: 'customer2@example.com', name: 'Customer 2', type: RecipientType.bcc),
];
final message = new OutgoingMessage(
html: '<h1>Welcome to our website</h1>',
text: 'WELCOME TO OUR WEBSITE',
to: recipients,
/* 等等... */
);
final response = await mandrill.messages.send(message);
}
对于完整的示例,请参阅example/example.dart
。
稳定性
此库旨在稳定可靠,使用Codable
来序列化JSON消息,经过良好测试并在生产环境中使用。
完整性
我们只实现了部分完整的API调用,因为我们目前不需要其他调用。
要查看已实现的API调用,请检查 API文档。
如果您需要其他资源的实现,我们很乐意接受合并请求,但我们也会根据请求添加新的资源。
许可证
MIT许可证
示例代码
import 'package:logging/logging.dart';
import 'package:mandrill/mandrill_server.dart';
const apiKey = 'your-key';
void main() async {
Logger.root
..level = Level.ALL
..onRecord.listen(print);
final log = Logger('Mandrill Example');
final mandrill = createMandrill(apiKey);
final recipients = [
Recipient(email: 'customer1@example.com', name: 'Customer 1'),
Recipient(
email: 'customer2@example.com',
name: 'Customer 2',
type: RecipientType.bcc),
];
final message = OutgoingMessage(
html: '<h1>Welcome to our website</h1>',
text: 'WELCOME TO OUR WEBSITE',
fromEmail: 'good@website.com',
fromName: 'Greatest Website',
to: recipients,
important: true,
);
try {
final response = await mandrill.messages.send(message);
log.info('${response.sentMessages.length} messages have been sent.');
} on MandrillException catch (e) {
log.severe(e.toString());
}
}
更多关于Flutter邮件发送插件mandrill的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter邮件发送插件mandrill的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中集成Mandrill邮件发送功能通常需要借助其API。由于Flutter本身不直接提供邮件发送功能,我们需要使用网络请求库(如http
包)来与Mandrill的API进行交互。以下是一个基本的示例代码,展示如何在Flutter应用中通过Mandrill发送邮件。
首先,确保你已经注册了Mandrill账号,并获取了API密钥。
步骤 1: 添加依赖
在你的pubspec.yaml
文件中添加http
依赖:
dependencies:
flutter:
sdk: flutter
http: ^0.13.3 # 请检查最新版本号
然后运行flutter pub get
来安装依赖。
步骤 2: 编写邮件发送逻辑
创建一个Dart文件(例如mandrill_service.dart
),并在其中编写发送邮件的逻辑。
import 'dart:convert';
import 'package:http/http.dart' as http;
class MandrillService {
final String apiKey;
MandrillService({required this.apiKey});
Future<void> sendEmail({
required String toEmail,
required String subject,
required String text,
String? html,
List<Map<String, String>>? mergeVars,
Map<String, dynamic>? async,
Map<String, dynamic>? ip_pool,
String? send_at,
List<String>? tags,
Map<String, dynamic>? subaccount,
String? return_path_domain,
bool? signing_domain,
bool? auto_text,
bool? auto_html,
bool? merge,
bool? validate,
bool? track_opens,
bool? track_clicks,
bool? url_strip_qs,
bool? preserve_recipients,
bool? view_content_link,
String? bcc_address,
bool? tracking_domain,
bool? canonical_identifiers,
bool? inline_css,
bool? google_analytics_domains,
bool? google_analytics_campaign,
bool? metadata,
String? recipient_metadata,
bool? from_name,
bool? reply_to,
bool? to_name,
bool? headers,
bool? important,
bool? mirror,
bool? smtp_envelope,
bool? return_path,
bool? signing_domain_signature,
bool? merge_language,
}) async {
final url = Uri.parse('https://mandrillapp.com/api/1.0/messages/send.json');
final body = {
'key': apiKey,
'message': {
'html': html,
'text': text,
'subject': subject,
'from_email': 'your-email@example.com', // 发件人邮箱
'to': [
{'email': toEmail, 'name': '', 'type': 'to'},
],
'merge_vars': mergeVars ?? [],
'auto_text': auto_text ?? false,
'auto_html': auto_html ?? null,
'async': async ?? false,
'ip_pool': ip_pool ?? null,
'send_at': send_at,
'tags': tags ?? [],
'subaccount': subaccount ?? null,
'return_path_domain': return_path_domain,
'signing_domain': signing_domain ?? null,
'track_opens': track_opens ?? null,
'track_clicks': track_clicks ?? null,
'url_strip_qs': url_strip_qs ?? null,
'preserve_recipients': preserve_recipients ?? null,
'view_content_link': view_content_link ?? null,
'bcc_address': bcc_address,
'tracking_domain': tracking_domain ?? null,
'canonical_identifiers': canonical_identifiers ?? null,
'inline_css': inline_css ?? null,
'google_analytics_domains': google_analytics_domains ?? null,
'google_analytics_campaign': google_analytics_campaign ?? null,
'metadata': metadata ?? null,
'recipient_metadata': recipient_metadata,
'headers': headers ?? null,
'important': important ?? null,
'mirror': mirror ?? null,
'smtp_envelope': smtp_envelope ?? null,
'return_path': return_path ?? null,
'signing_domain_signature': signing_domain_signature ?? null,
'merge_language': merge_language ?? null,
},
'async': merge ?? false,
'validate': validate ?? false,
};
final response = await http.post(
url,
body: jsonEncode(body),
headers: {
'Content-Type': 'application/json',
},
);
if (response.statusCode != 200) {
throw Exception('Failed to send email: ${response.body}');
}
print('Email sent successfully: ${response.body}');
}
}
步骤 3: 使用邮件发送服务
在你的Flutter应用中,你可以这样使用MandrillService
来发送邮件:
import 'package:flutter/material.dart';
import 'mandrill_service.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Send Email with Mandrill'),
),
body: Center(
child: ElevatedButton(
onPressed: () async {
final mandrillService = MandrillService(apiKey: 'your-mandrill-api-key');
try {
await mandrillService.sendEmail(
toEmail: 'recipient@example.com',
subject: 'Test Email',
text: 'This is a test email sent from Flutter using Mandrill.',
// html: '<h1>Test Email</h1><p>This is a test email sent from Flutter using Mandrill.</p>',
);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Email sent successfully')));
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Failed to send email: $e')));
}
},
child: Text('Send Email'),
),
),
),
);
}
}
请注意,上面的代码示例仅用于演示目的。在实际应用中,你应该避免在客户端代码中硬编码API密钥,而是应该考虑使用更安全的方法(如后端服务)来处理敏感信息。同时,确保遵守所有相关的隐私和安全最佳实践。