Skip to content

Commit

Permalink
Improved gmail integration utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasfj committed Sep 3, 2020
1 parent 332ece0 commit 6505b57
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1.0
* Improve gmail integration utilities.
* Discourage use of username/password authentication through deprecation.

## 3.0.4
* fix null pointer when server doesn't support EHLO (#121)

Expand Down
67 changes: 67 additions & 0 deletions lib/smtp_server/gmail.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,74 @@
import 'dart:convert' show base64, ascii;
import '../smtp_server.dart';

/// Send through gmail with username / password authentication.
///
/// **WARNING** Authentication with username / password is a Less-Secure-App
/// feature, which has been [scheduled for deprecation][1] once.
///
/// [1]: https://gsuiteupdates.googleblog.com/2019/12/less-secure-apps-oauth-google-username-password-incorrect.html
@Deprecated(
'Favor gmailUserXoauth2 as username/password is considered a Less-Secure-Apps',
)
SmtpServer gmail(String username, String password) =>
SmtpServer('smtp.gmail.com', username: username, password: password);

@Deprecated('Favor gmailUserXoauth2 or gmailRelayXoauth2')
SmtpServer gmailXoauth2(String token) =>
SmtpServer('smtp.gmail.com', xoauth2Token: token);

/// Send through gmail with [SASL XOAUTH2][1] authentication.
///
/// This requires an [accessToken] for [userEmail] with the OAuth2 scope:
/// `https://mail.google.com/`.
///
/// [1]: https://developers.google.com/gmail/imap/xoauth2-protocol#the_sasl_xoauth2_mechanism
SmtpServer gmailSaslXoauth2(
String userEmail,
String accessToken,
) =>
SmtpServer(
'smtp.gmail.com',
xoauth2Token: _formatXoauth2Token(userEmail, accessToken),
ssl: true,
port: 465,
);

/// Send through GSuite gmail relay with [SASL XOAUTH2][1] authentication.
///
/// This requires that the _G Suite SMTP relay service_ is enabled by the
/// GSuite administrator. For more information see:
/// [Send email from a printer, scanner, or app][2].
///
/// This requires an [accessToken] for [userEmail] with the OAuth2 scope:
/// `https://mail.google.com/`. This can be obtained in many differnet ways,
/// one could add an application to the GSuite account and have users grant
/// access, or one could use [domain-wide delegation][3] to obtain a
/// service-account that can impersonate any GSuite user for the given domain
/// with the `https://mail.google.com/`, and then [use said service account][4]
/// to obtain an `accessToken` impersonating a GSuite user.
///
/// [1]: https://developers.google.com/gmail/imap/xoauth2-protocol#the_sasl_xoauth2_mechanism
/// [2]: https://support.google.com/a/answer/176600?hl=en
/// [3]: https://support.google.com/a/answer/162106?hl=en
/// [4]: https://developers.google.com/identity/protocols/oauth2/service-account#delegatingauthority
SmtpServer gmailRelaySaslXoauth2(
String userEmail,
String accessToken,
) =>
SmtpServer(
'smtp-relay.gmail.com',
xoauth2Token: _formatXoauth2Token(userEmail, accessToken),
ssl: true,
port: 465,
);

/// Format in compliance with:
/// https://developers.google.com/gmail/imap/xoauth2-protocol#the_sasl_xoauth2_mechanism
String _formatXoauth2Token(
String userEmail,
String accessToken,
) =>
ascii.fuse(base64).encode(
'user=$userEmail\u0001auth=Bearer $accessToken\u0001\u0001',
);
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mailer
version: 3.0.4
version: 3.1.0
description: >
Compose and send emails from Dart.
Supports file attachments and HTML emails
Expand Down

0 comments on commit 6505b57

Please sign in to comment.