【发布时间】:2020-09-22 16:43:50
【问题描述】:
Mailgun 官方支持http,但截至2020年9月还没有Dart官方包。邮件发送成功但附件丢失。注意所有失败的尝试。
import 'dart:io';
import 'package:http/http.dart' as foo;
// must be https for basic auth (un + pw)
const secureProtocol = 'https://';
const host = 'api.mailgun.net/v3/m.givenapp.com/messages';
// basic auth
const userApiKey = 'my api key here'; // pw
const un = 'api';
void main() async {
//
const path = 'bin/services/foo.baz.txt';
var file = File(path);
print(file.existsSync()); // looks good
print(file.readAsStringSync()); // looks good
var list = <String>[];
list.add(file.readAsStringSync());
var files = <File>[];
files.add(file);
//
var body = <String, dynamic>{};
body.putIfAbsent('from', () => 'John Smith <john.smith@example.com>');
body.putIfAbsent('to', () => 'jane.doe@somehost.com');
body.putIfAbsent('subject', () => 'test subject ' + DateTime.now().toIso8601String());
body.putIfAbsent('text', () => 'body text');
// fixme
body.putIfAbsent('attachment', () => '@$path'); // failed
body.putIfAbsent('attachment', () => path); // failed
//body.putIfAbsent('attachment', () => file); // failed
//body.putIfAbsent('attachment', () => list); // failed
//body.putIfAbsent('attachment', () => files); // failed
body.putIfAbsent('attachment', () => file.readAsStringSync()); // failed
//body.putIfAbsent('attachment', () => file.readAsBytesSync()); // failed
final uri = '$secureProtocol$un:$userApiKey@$host';
final response = await foo.post(uri, body: body);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
}
我想我很接近了。
https://documentation.mailgun.com/en/latest/api-sending.html#sending
【问题讨论】:
标签: http dart attachment mailgun