Generate html code from any email mime message for displaying it.
The enough_mail_html
package defines the transformToHtml()
extension method on MimeMessage
of the enough_mail package.
This method will always generate HTML, specifically also for plain text or empty messages. You can define your custom processors
- for converting a plain text message into HTML,
- for adapting the HTML message,
- for handling inline attachments.
A simple usage example:
import 'package:enough_mail/enough_mail.dart';
import 'package:enough_mail_html/enough_mail_html.dart';
String generateHtml(MimeMessage mimeMessage) {
return mimeMessage.transformToHtml(
blockExternalImages: false,
emptyMessageText: 'Nothing here, move on!',
);
}
String generatePlainText(String htmlText) {
return HtmlToPlainTextConverter.convert(htmlText);
}
More examples:
import 'package:enough_mail/enough_mail.dart';
import 'package:enough_mail_html/enough_mail_html.dart';
import 'package:html/dom.dart';
String simpleTransformExample(MimeMessage mimeMessage) {
return mimeMessage.transformToHtml();
}
String configureImageBlockingOrEmptyMessage(MimeMessage mimeMessage) {
return mimeMessage.transformToHtml(
blockExternalImages: true, emptyMessageText: 'Nothing here, move on!');
}
String playYourself(MimeMessage mimeMessage) {
final cfg = TransformConfiguration.create(
blockExternalImages: true,
emptyMessageText: 'Nothing here, move on!',
customDomTransformers: [StyleTextDomTransformer()],
customValues: {'textStyle': 'font-size:10px;font-family:verdana;'},
);
return mimeMessage.transformToHtml(transformConfiguration: cfg);
}
class StyleTextDomTransformer extends DomTransformer {
@override
void process(Document document, MimeMessage message,
TransformConfiguration configuration) {
final paragraphs = document.getElementsByTagName('p');
for (final paragraph in paragraphs) {
paragraph.attributes['style'] = configuration.customValues['textStyle'];
}
}
}
Add this dependency your pubspec.yaml
file:
dependencies:
enough_mail_html: ^2.0.0
The latest version or enough_mail_html
is .
Note: you might need to set a dependency override for the xml package:
dependency_overrides:
xml: ^6.0.1
Please file feature requests and bugs at the issue tracker.
Licensed under the commercial friendly Mozilla Public License 2.0.