Send email on flutter direct from a form

Viewed 3319

I want to send an email directly from a flutter form. Which is the best option? This can't be difficult. The plugins always have errors and no direct. There is no option to put a mailto and send the email?

Thanks

3 Answers
   import 'package:flutter/material.dart';
   import 'dart:async';
   import 'package:flutter_email_sender/flutter_email_sender.dart';

   void main() => runApp(MyApp());

  class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
  // TODO: implement build
  return MaterialApp(
  debugShowCheckedModeBanner: false,
  home: Home(),
   );
   }
  }
 class Home extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
  // TODO: implement createState
  return _HomeState();
  }
 }

class _HomeState extends State<Home> {
var _emailFormKey = GlobalKey<FormState>();
TextEditingController emailController = new TextEditingController();
TextEditingController messageController = new TextEditingController();

@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
  resizeToAvoidBottomPadding: false,
  appBar: AppBar(
    title: Text("Email sending App"),
  ),
  body: Form(
    key: _emailFormKey,
    child: Column(
      children: <Widget>[
        Container(
          margin: EdgeInsets.only(top: 50.0, left: 15.0, right: 15.0),
          child: TextFormField(
            controller: emailController,
            validator: (value){
              if(value.isEmpty) {
                return "please Enter email";
              }
            },
            decoration: InputDecoration(
                labelText: "Enter email",
                border: OutlineInputBorder(
                    borderSide: BorderSide(
                  color: Colors.red,
                  width: 2.0,
                ))),
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 15.0, left: 15.0, right: 15.0),
          child: TextFormField(
            controller: messageController,
            validator: (value){
              if(value.isEmpty) {
                return "please Enter message";
              }
            },
            decoration: InputDecoration(
                labelText: "Enter Message",
                border: OutlineInputBorder(
                    borderSide: BorderSide(
                  color: Colors.red,
                  width: 2.0,
                ))),
          ),
        ),
        Container(
          margin: EdgeInsets.only(top: 15.0),
          child: RaisedButton(child: Text("Send"), onPressed: (){
            if(_emailFormKey.currentState.validate()){
              sendMessage();
            }
          }),
        )
      ],
      ),
     ),
     );
    }
   void sendMessage(){
   String inputEmail ;
   String inputMessage;
   Email email ;
   setState(() {
   inputEmail= emailController.text;
   inputMessage = messageController.text;
   if(inputMessage.isNotEmpty&&inputEmail.isNotEmpty) {
       email = Email(
       body: inputMessage,
       subject: 'Email subject',
       recipients: [inputEmail],
     );
     send(email);
    }

   });
  debugPrint('email - > $inputEmail  message -> $inputMessage');

  }
  void send(Email email) async {
  await FlutterEmailSender.send(email);
 }

 }

// please add "flutter_email_sender:" plugin in pubspec.yaml file

Now I have this problem with the email sender:

  • flutter_email_sender does not specify a Swift version and none of the targets (Runner) integrating it have the SWIFT_VERSION attribute set. Please contact the author or set the SWIFT_VERSION attribute in at least one of the targets that integrate this pod.

    /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/lib/cocoapods/installer/xcode/target_validator.rb:122:in verify_swift_pods_swift_version' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/lib/cocoapods/installer/xcode/target_validator.rb:37:in validate!' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/lib/cocoapods/installer.rb:578:in validate_targets' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/lib/cocoapods/installer.rb:158:ininstall!' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/lib/cocoapods/command/install.rb:51:in run' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/claide-1.0.2/lib/claide/command.rb:334:inrun' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/lib/cocoapods/command.rb:52:in run' /usr/local/Cellar/cocoapods/1.7.2/libexec/gems/cocoapods-1.7.2/bin/pod:55:in' /usr/local/Cellar/cocoapods/1.7.2/libexec/bin/pod:22:in load' /usr/local/Cellar/cocoapods/1.7.2/libexec/bin/pod:22:in'

Error output from CocoaPods: ↳

[!] Automatically assigning platform `ios` with version `8.0` on target `Runner` because no platform was specified. Please
specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.
Related