首页 > 解决方案 > 使用颤振通过 Sendgrid API 发送 png 附件?

问题描述

我正在尝试使用颤振通过 SendGrid API 发送 png 附件。我遵循了特定于附件邮件的 SendGrid API 文档(https://docs.sendgrid.com/for-developers/sending-email/migrating-from-v2-to-v3-mail-send#attachments),但我不断收到错误的请求响应消息(状态 400)。我不知道为什么。希望有人可以提供帮助。


  static Future<int> sendEmail(
      {String subject,
      String content,
      LoggedInUserNameLogin senderNameLogin,
      String attachmentBase64,
      String fileName}) async {
    String supportEmail = 'test@email.com';
    String fromEmail = 'test@email.com';
    Map<String, String> headers = new Map();
    headers["Authorization"] =
        "Bearer SG.key";
    headers["Content-Type"] = "application/json";

    Uri url = Uri.parse('https://api.sendgrid.com/v3/mail/send');
    content =
        '$content<b> Sender User ID:<\/b> ${senderNameLogin.userID} <br><br> <b>Sender Name: <\/b>${senderNameLogin.name} <\/p>';
   
    String emailBody = "{\n          \"personalizations\":"
        "         [\n"
        "            {\n"
        "              \"to\": [\n"
        "                {\n"
        "                  \"email\": \"$supportEmail\"\n"
        "                },\n"
        "                       ]\n"
        "             }\n"
        "          ],\n"
        "          \"from\": {\n"
        "            \"email\": \"$fromEmail\"\n"
        "          },\n"
        "          \"subject\": \"$subject\",\n"
        "          \"content\":"
        "          [\n"
        "            {\n"
        "              \"type\": \"text\/html\",\n"
        "              \"value\": \"$content\"\n"
        "            }\n"
        "          ],\n"
        "          \"attachments\":"
        "           [\n"
        "                 { \n"
        "                   \"content\": \"$attachmentBase64\", \n"
        "                   \"type\": \"jpg\",\n"
        "                   \"name\": \"example_file\",\n"
        "                   \"filename\": \"$fileName\", \n"
        "                   \"disposition\": \"inline\", \n"
        "                   \"content_id\": \"feedback_screenshot\" \n"
        "                 } \n"
        "                ],\n"
        "        }";
   

    return response.statusCode;
  } 

标签: flutterpngsendgridattachment

解决方案


推荐阅读