Search code examples
htmlfluttermailer

How can I pass an HTML file/template to the Mailer package "...html" parameter in Flutter?


So Im using the Mailer Package in flutter https://pub.dev/packages/mailer, and its great and everything but one problem im facing is that i want to pass a complex HTML template/file to the Mailer's package "...html" parameter instead of a simple one. The way i wanna approach this problem is through creating a separate file (heard it could be a dart file and write HTML in it P.S. please show me how), and then pass this file to the "...html" parameter in the Mailer package. Please tell me how is this possible in flutter. My Code


Solution

  • In this case, using the raw String definition it's recommended:

    final String html = '''
    very long html here 
    '''
    
    final Message = Message()
    ..html = html;
    

    An even better approach would be to define the HTML template in a separate file:

    //html_template.dart
    
    library html_template;
    
    final String html = '''
    <html></html>
    '''; // very long html template definition
    

    And import it when required:

    import './html_template.dart' as Template;
    
    void main() {
      final Message = Message()..html = Template.html;
    }