Flutter, Sending Form Data to Email

后端 未结 4 897
野的像风
野的像风 2021-01-01 16:01

I would like to create a contact form and would like to know: How to send data from a contact form to my email? I would like to see a working example. I wanted to submit a f

相关标签:
4条回答
  • 2021-01-01 16:32

    You can navigate to default Email app. You can also set the following attributes from your flutter app.

    1. to mail ID,
    2. subject and
    3. body

    using url_launcher plugin.

    Steps:

    1. Add this to your package's pubspec.yaml file: url_launcher: "^3.0.1"

    2. main.dart file

    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    void main() => runApp(new MaterialApp(home: new MyApp(), debugShowCheckedModeBanner: false,));
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new RaisedButton(onPressed: () => _launchURL('xxx@gmail.com', 'Flutter Email Test', 'Hello Flutter'), child: new Text('Send mail'),),
          ),
        );
      }
    
      _launchURL(String toMailId, String subject, String body) async {
        var url = 'mailto:$toMailId?subject=$subject&body=$body';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-01 16:33

    Please check "mailer" package from flutter. It will use smtp to send email from background without opening userinterface app. It has gmail, yahoo mail, mailgun options to send email.

    Reference link :

    https://pub.dartlang.org/packages/mailer

    0 讨论(0)
  • 2021-01-01 16:37

    If you wanna send it silence and without pop-up any email dialog, you may consider the way which using firebase extension and an SMTP provider.

    For my example, I'm using Firebase as our backend API, so we choose an extension of Firebase called "trigger email" to send emails silently.

    After you set up an SMTP provider and decided to choose the "Firebase trigger email extension", you can send emails through the form you made in Flutter code silently.

    I hope it helps.

    0 讨论(0)
  • 2021-01-01 16:40

    Try flutter_email_sender package. Here is an example taken from their github.

    import 'dart:async';
    import 'dart:io';
    
    import 'package:flutter/material.dart';
    import 'package:flutter_email_sender/flutter_email_sender.dart';
    import 'package:image_picker/image_picker.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String attachment;
    
      final _recipientController = TextEditingController(
        text: 'example@example.com',
      );
    
      final _subjectController = TextEditingController(text: 'The subject');
    
      final _bodyController = TextEditingController(
        text: 'Mail body.',
      );
    
      final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
    
      Future<void> send() async {
        final Email email = Email(
          body: _bodyController.text,
          subject: _subjectController.text,
          recipients: [_recipientController.text],
          attachmentPath: attachment,
        );
    
        String platformResponse;
    
        try {
          await FlutterEmailSender.send(email);
          platformResponse = 'success';
        } catch (error) {
          platformResponse = error.toString();
        }
    
        if (!mounted) return;
    
        _scaffoldKey.currentState.showSnackBar(SnackBar(
          content: Text(platformResponse),
        ));
      }
    
      @override
      Widget build(BuildContext context) {
        final Widget imagePath = Text(attachment ?? '');
    
        return MaterialApp(
          theme: ThemeData(primaryColor: Colors.red),
          home: Scaffold(
            key: _scaffoldKey,
            appBar: AppBar(
              title: Text('Plugin example app'),
              actions: <Widget>[
                IconButton(
                  onPressed: send,
                  icon: Icon(Icons.send),
                )
              ],
            ),
            body: SingleChildScrollView(
              child: Center(
                child: Padding(
                  padding: EdgeInsets.all(8.0),
                  child: Column(
                    mainAxisSize: MainAxisSize.max,
                    // mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.all(8.0),
                        child: TextField(
                          controller: _recipientController,
                          decoration: InputDecoration(
                            border: OutlineInputBorder(),
                            labelText: 'Recipient',
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(8.0),
                        child: TextField(
                          controller: _subjectController,
                          decoration: InputDecoration(
                            border: OutlineInputBorder(),
                            labelText: 'Subject',
                          ),
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.all(8.0),
                        child: TextField(
                          controller: _bodyController,
                          maxLines: 10,
                          decoration: InputDecoration(
                              labelText: 'Body', border: OutlineInputBorder()),
                        ),
                      ),
                      imagePath,
                    ],
                  ),
                ),
              ),
            ),
            floatingActionButton: FloatingActionButton.extended(
              icon: Icon(Icons.camera),
              label: Text('Add Image'),
              onPressed: _openImagePicker,
            ),
          ),
        );
      }
    
      void _openImagePicker() async {
        File pick = await ImagePicker.pickImage(source: ImageSource.gallery);
        setState(() {
          attachment = pick.path;
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题