【问题标题】:How to add attachment using mailgun in dart with http?如何在带有http的飞镖中使用mailgun添加附件?
【发布时间】: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


    【解决方案1】:

    你链接的文档说

    重要提示:发送附件时必须使用 multipart/form-data 编码。

    所以你想做一个MultipartRequest,而不仅仅是一个普通的发帖请求。

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

    var request = foo.MultipartRequest(
      'POST',
      Uri.parse('$secureProtocol$un:$userApiKey@$host')
    );
    
    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');
    
    request.fields = body;
    
    request.headers["Content-Type"] = "multipart/form-data";
    request.files.add(await http.MultipartFile.fromPath('attachment', path));
    
    var response = await request.send();
    print('Response status: ${response.statusCode}');
    print('Response body: ${response.body}');
    

    添加 from、to、subject 和 text 字段都是一样的,只需将其添加到 MultipartRequestfields 参数即可。

    标题已更改以指示正确的类型。 MultipartFile 是从路径中创建的,并给定字段名称 attachment。这被添加到MultipartRequest 的文件部分。

    然后请求会被发送和处理,就像您已经拥有的一样。


    如果您想更轻松地做到这一点,您可以尝试mailgun package,它会为您完成所有这些工作。

    【讨论】:

    • 这是最礼貌的说法,“RTFM”。这家伙应该得到荣誉的金属。
    猜你喜欢
    • 2022-01-24
    • 2020-04-12
    • 1970-01-01
    • 2020-11-28
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2022-01-20
    相关资源
    最近更新 更多