How to Send A Complicated Email with Flutter Mailer Package How to Send A Complicated Email with Flutter Mailer Package flutter flutter

How to Send A Complicated Email with Flutter Mailer Package


There are 2 aspects to this question.

  • How to set the stripo html.
  • How to adapt an html template. (Modify the content to change parts of the template)

To set the template simply do:

message.html ='''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[... copy the complete template here ...]</html>'''

Three quotes (''') instead of one quote (') allows text to span multiple lines.To keep your code clean, you might want to create a new dart file: mail-template.dart and assign the html-template to a variable. Then include this file and assign the variable.

To replace parts of the template I would use .replaceAll on the template.

var nameFromSomeInput = 'Jane Doe';var yourHtmlTemplate = '<html>Dear {{NAME}}</html>';message.html = yourHtmlTemplate.replaceAll('{{NAME}}', nameFromSomeInput);

Note that replaceAll is easy and doesn't depend on any additional libraries. It is however slow. Especially if you replace multiple values by calling replaceAll multiple times.

Consider a template engine like: jinja in such cases.