# `email_feedback`

| Package | Kind | Provides the role | Needs | Choose with |
| --- | --- | --- | --- | --- |
| [`smf_email_feedback`](https://pub.dev/packages/smf_email_feedback) | infrastructure | Feedback | values in `.env`, such as [`dart_define`](https://doc.saymyframe.com/modules/dart-define.md) | `-m email_feedback` |

`email_feedback` lets the users of the app write to you: the row "Write to us" of the settings screen, or a button of yours that calls `appFeedback.open(context)`, opens a new mail to your address in the mail app of the device, with [url_launcher](https://pub.dev/packages/url_launcher). The code of the app uses `appFeedback`, which the feedback role generates, and knows nothing of the mail; see [Store reviews and feedback](https://doc.saymyframe.com/guides/reviews-and-feedback.md#feedback).

The mail goes to the address in `EMAIL_FEEDBACK_ADDRESS` of `.env`, so the module requires the values of `.env` and brings `dart_define` while that module is the only one that provides them. The role uses the settings screen and the localization: in an app with a [settings screen](https://doc.saymyframe.com/modules/settings.md), the row "Write to us" is there, and in an app with [`gen_l10n`](https://doc.saymyframe.com/modules/gen-l10n.md) the texts of the module are texts of the app.

## What it adds to the app

| In the app | What it is |
| --- | --- |
| `url_launcher: any`, `package_info_plus: any` and `device_info_plus: any` in the dependencies | url_launcher opens the mail, package_info_plus reads the version of the app, and device_info_plus the system and the model of the device. The constraint `any` leaves their versions to the other modules of the app that take them. |
| `lib/core/feedback/email_feedback_service.dart` | `EmailFeedbackService`, which implements `FeedbackService` of the role, and `createEmailFeedbackService()`, which creates it with the app. |
| `lib/core/feedback/email_feedback_mail.dart` | `emailFeedbackAppName`, the name of the app in the mail, `isEmailFeedbackAddress()`, and `emailFeedbackLink()`, which builds the `mailto:` link. It needs only Dart. |
| `lib/core/feedback/email_feedback_sheet.dart` | `showEmailFeedbackSheet()`, the sheet that shows the address where no mail app opens the mail. |
| The key `EMAIL_FEEDBACK_ADDRESS` in `.env` | The address that the mails go to, such as `support@example.com`. |
| Six texts, in English and in Ukrainian | The word of the subject, the header of what the mail tells about the app and the device, and the title, the text and the two labels of the button of the sheet. |
| The section "Feedback by email" of the README | Where the mail goes, what it holds, what shows where no mail app opens it, where its texts are, and how to see it. |

Whichever module provides it, the feedback role adds these files:

| File | What it holds |
| --- | --- |
| `lib/core/feedback/app_feedback.dart` | `appFeedback`, through which the code of the app opens the way to write. |
| `lib/core/feedback/feedback_service.dart` | `FeedbackService`, the interface that this module implements. |
| `lib/core/feedback/feedback_setting.dart`, in an app with a settings screen | `FeedbackSetting`, the row "Write to us", and `feedbackSettingShows()`, which hides it while `appFeedback.available` is false. |

The service is created with the app, so the role adds nothing to `bootstrap()`. The module needs nothing in the native projects: the app asks the device nothing before it opens the mail, so it needs no `<queries>` in the manifest of Android and no `LSApplicationQueriesSchemes` in `Info.plist`.

In the [guide for coding agents](https://doc.saymyframe.com/guides/coding-agents.md) of the app, the role describes `appFeedback` under "Feedback". The module adds which file opens the mail and with what, how to build a `mailto:` link, why the app never calls `canLaunchUrl` first, and what shows where no mail app opens the mail.

## The address

Put your address into `.env`:

```bash title=".env"
EMAIL_FEEDBACK_ADDRESS=support@example.com
```

Until it has one, the app does not offer the way to write: `appFeedback.available` is false, the row "Write to us" hides from the settings screen, and a debug build prints which key to fill in. In an app without a settings screen, hide your own button while `appFeedback.available` is false. A value that is not one email address, such as two addresses or an address with a query, counts as none.

The address goes into the app, where anyone can read it, so use an address for support with a filter of spam rather than your own.

## What the mail holds

- The subject is "Feedback: " in the language of the app and `emailFeedbackAppName`, the name of the app, such as "Feedback: My App". The name is the same in every flavor, so a filter of your mail finds every such mail. Change it in `lib/core/feedback/email_feedback_mail.dart` if you rename the app.
- The body leaves two empty lines for the message. Below them, a header and two lines tell the app and the device, such as `My App 1.0.0 (1), dev` and `Android 14 (SDK 34), Google Pixel 7`, or `iOS 17.4, iPhone 15 Pro`: the name, the version and the build number of the app and the flavor of the build, and the system with its version and the model of the device. The mail of the flavor for a release, `prod`, names no flavor.
- The app reads the app and the device at once, each for 1.5 seconds at most. A read that fails or takes longer leaves its part out, the line of the device then names the platform alone, and the mail still opens.

The user sees all of it and may change it before sending. What the mail tells is read on the device, and leaves it only in a mail that the user sends from their own mail app: the app itself sends nothing.

`emailFeedbackLink()` encodes each part as RFC 6068 asks: a space is `%20` and never `+`, which a mail app would show as a plus, a line break is `%0D%0A`, and a `+` of the address is `%2B`. So build a `mailto:` link of your own with it, never with `Uri(queryParameters: ...)`, which writes a space as `+`.

## Where no mail app opens the mail

The service never asks the device first whether a mail app is there, as `canLaunchUrl` would: Android 11 and later answer no to an app whose manifest names no other app. It opens the mail, and learns from the answer. When nothing opened it, such as on the iOS simulator, which has no Mail, or on an Android emulator without a mail app, a sheet shows the address on the navigator at the root of the app, with a button that copies it and says so. `appFeedback.open(context)` then completes with `false`.

To see the mail, run the app on an iPhone, or on an Android device or emulator with a mail app and an account in it, such as Gmail, and tap "Write to us", or your own button.

## The texts

The texts of the mail and of the sheet are texts of the module, in English and in Ukrainian:

| Text | English | Ukrainian |
| --- | --- | --- |
| The word of the subject | Feedback | Відгук |
| The header of the lines about the app and the device | About the app and the device: | Про застосунок і пристрій: |
| The title of the sheet | Write to us by email | Напишіть нам листом |
| The text of the sheet | Couldn't open a mail app. Copy the address and write to us from your email. | Не вдалося відкрити поштовий застосунок. Скопіюйте адресу й напишіть нам зі своєї пошти. |
| The button of the sheet | Copy the address | Скопіювати адресу |
| What the sheet says once the address is copied | Address copied | Адресу скопійовано |

In an app with localization, they are texts of the app: change them where the provider of the localization keeps the texts of the app, in each language. In an app without it, they are the English texts in the files of the service and of the sheet.

## Finish the setup

The README lists one step in its section "Finish the setup":

| Step | What you do |
| --- | --- |
| [`email_feedback.address`](https://doc.saymyframe.com/setup/email-feedback-address.md) | Put the email address that the mails go to into `EMAIL_FEEDBACK_ADDRESS` in `.env`. |

## In tests

A test of a screen with a button that opens the way to write gives `appFeedback.use()` a service of its own; see [Store reviews and feedback](https://doc.saymyframe.com/guides/reviews-and-feedback.md#in-tests).

Whichever module provides the role, `appFeedback.open(context)` opens the way once, runs one call at a time, and completes with `false` and never throws when the provider fails. SMF's CI checks this in running apps with the role. For this module, it checks, over a stand-in for a device with a mail app that says no when it is asked whether it has one, as Android 11 and later say: on Android and on iOS and in each language of the app, that one mail reaches the mail app and goes to the address of `.env`, with `%20` and no `+` in its link, with its subject and with the lines of the app and of the device; that a read of the device that fails or waits too long leaves its part out and the mail still opens; that without an address the app offers nothing; and that where nothing opens the mail, the sheet shows with the address, which its button copies, also on a small phone with a large text size. On a device, it checks that the mail would tell a version, a build number, a system and a model, and that `appFeedback.available` agrees with whether `.env` has an address. See [Contributing](https://doc.saymyframe.com/contributing.md#the-roles-at-run-time).

## Choosing it

`smf create` asks "Feedback: which module provides it?" and offers this module first, so Enter takes it. The answer may be none. To choose it without the question, with the settings screen that shows the row "Write to us":

```bash
smf create my_app -m email_feedback,settings
```

`smf create` then adds `dart_define`, the only provider of the values in `.env`, which the module requires, and `go_router`, which the settings screen requires.
