gpt4 book ai didi

flutter - 从Flutter/dart发送html格式的文本

转载 作者:行者123 更新时间:2023-12-03 04:24:30 26 4
gpt4 key购买 nike

我设法从Flutter发送电子邮件,但电子邮件中仅允许<br>
如果我什至尝试使用<b>标记,它也会返回状态码400。

Future<Null> testingEmail(String userId, Map header) async {
header['Accept'] = 'application/json';
header['Content-type'] = 'application/json';

var from = userId;
var to = userId;
var subject = 'test send email';
var message = "Hi<br/>Html <b>Email</b><br><br>gfdgdfgdfg";
var content = '''
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: ${to}
from: ${from}
subject: ${subject}

${message}''';

var bytes = utf8.encode(content);
var base64 = base64Encode(bytes);
var body = json.encode({'raw': base64});

String url =
'https://www.googleapis.com/gmail/v1/users/' + userId + '/messages/send';

final http.Response response =
await http.post(url, headers: header, body: body);
}

最佳答案

构建字符串时,请注意换行符。最好使用\n以避免问题,而不是使用base64Encode,而不是base64Url。这是一个显示编码主体的小例子:

import 'dart:convert';

void main() {
var content = '''Content-Type: text/html; charset=utf-8\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nto: user2@domain\nsender: user@domain\nsubject: API test Dart\n\nHi<br/>Html <b>Email</b><br><br>gfdgdfgdfg''';

String encoded = base64Url.encode(utf8.encode(content));
print(encoded);

}

另外,我建议您检查 Google API Client Libraries with Dart,这是Google提供的使用其API的官方Dart库。

关于flutter - 从Flutter/dart发送html格式的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60420898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com