0

I managed sending email via gmail Api, but could you help me how to send attachment?

var encodedSubject = base64Url.encode(utf8.encode(subject));
subject = '=?utf-8?B?$encodedSubject?=';
var content =
        '''Content-Type: text/html; charset=utf-8\nMIME-Version: 1.0\nContent-Transfer-Encoding: 7bit\nto: ${recipients[0]}\nsender: $userId\nsubject: $subject\n\n$body''';
    var encodedJson =
        json.encode({'raw': base64Url.encode(utf8.encode(content))});

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

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

1 Answer 1

0

I would strongly encourage you to take a look at the API documentation and more specifically at the part of Creating messages with attachments:

Creating a message with an attachment is like creating any other message, but the process of uploading the file as a multi-part MIME message depends on the programming language.

Also there is a whole section just describing the necessary request (without libraries) to upload attachments, there are multiple ways you can do this depending on your file or expected output.

Also it may be useful to take a look at the mime repository for mime multipart handling in dart.

2
  • The main problem that I cannot find any relevant info about how to set the content. Jul 18, 2020 at 16:35
  • What do you mean by set the content? There is a link describing how you need to upload the attachment if that's what you mean.
    – Raserhin
    Jul 20, 2020 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.