|
| 1 | +# ht_email_sendgrid |
| 2 | + |
| 3 | + |
| 4 | +[](https://pub.dev/packages/very_good_analysis) |
| 5 | +[](https://polyformproject.org/licenses/free-trial/1.0.0) |
| 6 | + |
| 7 | +A concrete implementation of the `HtEmailClient` interface that uses the SendGrid API to send transactional emails. |
| 8 | + |
| 9 | +## Description |
| 10 | + |
| 11 | +This package provides `HtEmailSendGrid`, a class that connects to the SendGrid v3 API to send emails. It is designed to be used with the `ht_email_repository` and `ht_http_client` packages, leveraging the standardized `HtHttpClient` for making HTTP requests and handling errors. |
| 12 | + |
| 13 | +## Getting Started |
| 14 | + |
| 15 | +Add the dependency to your `pubspec.yaml`: |
| 16 | + |
| 17 | +```yaml |
| 18 | +dependencies: |
| 19 | + ht_email_sendgrid: |
| 20 | + git: |
| 21 | + url: https://github.com/headlines-toolkit/ht-email-sendgrid.git |
| 22 | + # ref: <specific_tag_or_commit> |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +To use this client, you must first configure an `HtHttpClient` instance with the SendGrid API base URL and an `AuthInterceptor` that provides your SendGrid API key. |
| 28 | + |
| 29 | +```dart |
| 30 | +import 'package:dio/dio.dart'; |
| 31 | +import 'package:ht_email_client/ht_email_client.dart'; |
| 32 | +import 'package:ht_email_sendgrid/ht_email_sendgrid.dart'; |
| 33 | +import 'package:ht_http_client/ht_http_client.dart'; |
| 34 | +import 'package:logging/logging.dart'; |
| 35 | +
|
| 36 | +void main() async { |
| 37 | + // 1. Configure the HtHttpClient for SendGrid |
| 38 | + final sendGridHttpClient = HtHttpClient( |
| 39 | + baseUrl: 'https://api.sendgrid.com/v3', |
| 40 | + tokenProvider: () async => 'YOUR_SENDGRID_API_KEY', // Provide your key here |
| 41 | + isWeb: false, // Use false for server-side applications |
| 42 | + ); |
| 43 | +
|
| 44 | + // 2. Instantiate the SendGrid email client |
| 45 | + final HtEmailClient emailClient = HtEmailSendGrid( |
| 46 | + httpClient: sendGridHttpClient, |
| 47 | + log: Logger('SendGridClient'), |
| 48 | + ); |
| 49 | +
|
| 50 | + // 3. Use the client to send an email |
| 51 | + try { |
| 52 | + await emailClient.sendTransactionalEmail( |
| 53 | + recipientEmail: 'recipient@example.com', |
| 54 | + templateId: 'd-your-template-id', |
| 55 | + templateData: {'name': 'World'}, |
| 56 | + ); |
| 57 | + print('Email sent successfully!'); |
| 58 | + } on HtHttpException catch (e) { |
| 59 | + print('Failed to send email: $e'); |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## License |
| 65 | + |
| 66 | +This package is licensed under the [PolyForm Free Trial](LICENSE). Please review the terms before use. |
0 commit comments