Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在带有http的dart中使用mailgun添加附件?_Http_Dart_Attachment_Mailgun - Fatal编程技术网

如何在带有http的dart中使用mailgun添加附件?

如何在带有http的dart中使用mailgun添加附件?,http,dart,attachment,mailgun,Http,Dart,Attachment,Mailgun,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

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}');
}
导入'dart:io';
将“package:http/http.dart”作为foo导入;
//基本身份验证(un+pw)必须为https
const secureProtocol='https://';
const host='api.mailgun.net/v3/m.givenap.com/messages';
//基本授权
const userApiKey='my api key here';//嗯
const un='api';
void main()异步{
//
const path='bin/services/foo.baz.txt';
var file=文件(路径);
打印(file.existsSync());//看起来不错
打印(file.readAsStringSync());//看起来不错
var列表=[];
add(file.readAsStringSync());
var文件=[];
文件。添加(文件);
//
var body={};
body.putIfAbsent('from',()=>'John Smith');
body.putIfAbsent('to',()=>'简。doe@somehost.com');
body.putIfAbsent('subject',()=>'testsubject'+DateTime.now().toIso8601String());
body.putIfAbsent('text',()=>'body text');
//修理工
body.putIfAbsent('attachment',()=>'@$path');//失败
body.putIfAbsent('attachment',()=>path);//失败
//body.putIfAbsent('附件',()=>文件);//失败
//body.putIfAbsent('attachment',()=>list);//失败
//body.putIfAbsent('attachment',()=>files);//失败
body.putIfAbsent('attachment',()=>file.readAsStringSync());//失败
//body.putIfAbsent('attachment',()=>file.readAsBytesSync());//失败
最终uri=“$secureProtocol$un:$userApiKey@$host”;
最终响应=等待foo.post(uri,body:body);
打印('Response status:${Response.statusCode}');
打印('Response body:${Response.body}');
}
我想我很接近了


您链接的文档显示

重要提示:发送附件时必须使用多部分/表单数据编码

因此,您需要执行一个
多部分请求
,而不仅仅是一个普通的post请求

这可以通过使用与您已经使用的相同http包的以下代码大致完成

var-request=foo.MultipartRequest(
"岗位",,
parse(“$secureProtocol$un:$userApiKey@$host”)
);
var body={};
body.putIfAbsent('from',()=>'John Smith');
body.putIfAbsent('to',()=>'简。doe@somehost.com');
body.putIfAbsent('subject',()=>'testsubject'+DateTime.now().toIso8601String());
body.putIfAbsent('text',()=>'body text');
request.fields=body;
请求.headers[“内容类型”]=“多部分/表单数据”;
add(等待http.MultipartFile.fromPath('attachment',path));
var response=wait request.send();
打印('Response status:${Response.statusCode}');
打印('Response body:${Response.body}');
添加from、to、subject和text字段时,所有操作都是相同的,只需将其添加到
MultipartRequest的
字段
参数中即可

标题已更改以指示正确的类型。从路径创建一个
多部分文件
,并指定
附件
的字段名。这将添加到
多部分请求的文件部分

然后发送请求,并以与您已有请求类似的方式进行处理



如果您想更轻松地执行此操作,可以尝试,它可以为您完成所有这些操作。

这是说“RTFM”最礼貌的方式。这家伙应该得到荣誉。