【问题标题】:Send contact form data to email address using flutter and dart [duplicate]使用颤振和飞镖将联系表格数据发送到电子邮件地址[重复]
【发布时间】:2020-09-02 13:47:28
【问题描述】:

我正在构建一个颤振应用程序,它有一个联系表格。填写联系表格后,当用户在没有第三方的情况下按下提交时,我希望用户提供的数据从联系表格发送到我的电子邮件地址(或我将提供的电子邮件地址)。我的表单有预定义的字段,这些字段将由用户填写。提交后,我希望将表单发送到管理员的电子邮件地址。 我不是在谈论在我的应用中发送邮件

我已经完成了此处提出的附加问题,看起来很相似,但它没有回答我的问题。我曾尝试使用 url_launcher 包,但这也无济于事。

Flutter, Sending Form Data to Email

下面是我的表单截图。

**下面是我的代码摘录**

return Scaffold(
  backgroundColor: Color(0xFF56ccf2),
  body: SafeArea(
      top: false,
      bottom: false,
      child: SingleChildScrollView(
        child: Container(
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.only(
                    top: 10.0, bottom: 5.0, left: 15.0, right: 15.0),
                child: Card(
                  elevation: 6,
                  child: Form(
                      key: _formKey,
                      autovalidate: _autoValidate,
                      child: ListView(
                        shrinkWrap: true, 
                        padding: EdgeInsets.symmetric(horizontal: 20.0),
                        children: <Widget>[
                          //===> Student Number Text Input starts from here <===
                          Padding(
                            padding: EdgeInsets.only(
                          top: 10.0, bottom: 6.0, left: 1.0, right: 1.0),
                            child: TextFormField(
                              autofocus: false,
                              focusNode: myFocusNodeEmail,
                              controller: studentNumberController,
                              keyboardType: TextInputType.emailAddress,
                              style: TextStyle(
                                  fontSize: 16.0,
                                  color: Colors.black),
                              decoration: InputDecoration(
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10.0),
                                ),
                                labelText: 'Student Number',
                              ),
                              validator: validateStudentNumber,
                              onSaved: (String val) {
                                _stNumber = val;
                              },
                            ),
                          ),

                          //===> Email Address Text Input starts from here <===
                          Padding(
                            padding: EdgeInsets.only(
                                top: 1.0, bottom: 6.0, left: 1.0, right: 1.0),
                            child: TextFormField(
                              validator: validateStudentEmailAddress,
                              onSaved: (String val) {
                                _stEmail = val;
                              },
                              controller: studentEmailController,
                              decoration: InputDecoration(
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10.0),
                                ),
                                labelText: 'Student Email',
                              ),
                              keyboardType: TextInputType.emailAddress,
                              style: TextStyle(
                                  fontSize: 16.0,
                                  color: Colors.black),
                            ),
                          ),

                          //===> Phone Number Text Input starts from here <===
                          Padding(
                            padding: EdgeInsets.only(
                                top: 1.0, bottom: 6.0, left: 1.0, right: 1.0),
                            child: TextFormField(
                              validator: validateStudentPhoneNumber,
                              onSaved: (String val) {
                                _stPhone = val;
                              },
                              controller: studentPhoneNumberController,
                              decoration: InputDecoration(
                                border: OutlineInputBorder(
                                  borderRadius: BorderRadius.circular(10.0),
                                ),
                                labelText: 'Phone Number',
                              ),
                              keyboardType: TextInputType.phone,
                              inputFormatters: [
                                FilteringTextInputFormatter.allow(
                                    RegExp(r'^[()\d -]{1,15}$')),
                              ],
                              style: TextStyle(
                                  fontSize: 16.0,
                                  color: Colors.black),
                            ),
                          ),

                          //===> Drop Down Menu starts from here <===
                          Padding(
                            padding: EdgeInsets.only(top: 1.0, bottom: 6.0, left: 1.0, right: 1.0),
                            child: FormField(
                              builder: (FormFieldState state) {
                                return InputDecorator(
                                  decoration: InputDecoration(
                                    border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(10.0),
                                    ),
                                    labelText: 'Semester',
                                    hintText: ("Semester"),
                                  ),
                                  isEmpty: _semester == '',
                                  child: Padding(
                                    padding: EdgeInsets.only(left: 1.0, right: 130 , ),
                                    child: Container(
                                      width: MediaQuery.of(context).size.width,
                                      child: DropdownButtonHideUnderline( 
                                        child: ButtonTheme(
                                          alignedDropdown: true, 
                                          child: DropdownButton(
                                            value: _semester,
                                            isDense: true,
                                            elevation: 5,
                                            isExpanded: true,
                                            onChanged: (String value) {
                                              setState(() {
                                                _semester = value; 
                                                state.didChange(value);
                                              });
                                            },
                                            items: _semesters.map((String value) {
                                              return DropdownMenuItem(
                                                value: value,  
                                                child: Text(value ?? '',
                                                    textAlign: TextAlign.left,
                                                    overflow: TextOverflow.ellipsis,
                                                    maxLines: 1,
                                                    softWrap: true,
                                                ),
                                              );
                                            }).toList(),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ),
                                );
                              },
                            ),
                          ),

                          //===> Query Text Input starts from here <===
                          TextFormField(
                            validator: validateStudentQuery,
                            onSaved: (String val) {
                              _query = val;
                            },
                            controller: queryController,
                            decoration: InputDecoration(
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(10.0),
                              ),
                              labelText: 'Your Query',
                            ),
                            keyboardType: TextInputType.text,
                            style: TextStyle(
                                fontSize: 16.0,
                                color: Colors.black),
                            maxLines: 3,
                          ),

                          Container(
                              margin: EdgeInsets.only(top: 6.0, bottom: 5),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.all(Radius.circular(10.0)),
                                boxShadow: <BoxShadow>[
                                  BoxShadow(
                                    color:  Color(0xFF008ECC),
                                    offset: Offset(0.0, 0.0),
                                    //blurRadius: 20.0,
                                  ),
                                  BoxShadow(
                                    color:  Color(0xFF008ECC),
                                    offset: Offset(0.0, 0.0),
                                    //blurRadius: 20.0,
                                  ),
                                ],
                                gradient: LinearGradient(
                                    colors: [
                                      Color(0xFF008ECC), //Colors is Olympic blue
                                      Color(0xFF008ECC),
                                    ],
                                    begin: FractionalOffset(0.2, 0.2),
                                    end: FractionalOffset(1.0, 1.0),
                                    stops: [0.0, 1.0],
                                    tileMode: TileMode.clamp),
                              ),
                              child: MaterialButton(
                                onPressed: validateAndSubmit,
                                child: Padding(
                                  padding: EdgeInsets.symmetric(
                                      vertical: 10.0, horizontal: 65.0),
                                  child: Text(
                                    "Submit",
                                    style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 25.0,
                                    ),
                                  ),
                                ),
                              )),
                        ],
                      )),
                ),
              ),
            ],
          ),
        ),
      )),
);

【问题讨论】:

  • 您需要澄清“我希望将数据发送到我的电子邮件地址”是什么意思,您是否想要: a) 通过意图打开默认邮件应用程序; b) 通过 SMTP 直接从您的应用程序发送电子邮件; c) 别的东西(请详细说明)。哦,顺便说一句:a) 和 b) 包含在您发布的链接问题中,您只需向下滚动一点。
  • 我想要的是在用户填写表格后,我希望将数据直接发送到我的电子邮件地址。 @Uroš
  • 没有@ChristopherMoore

标签: android flutter dart


【解决方案1】:

嗯,这是一件事。首先,您需要一个发件人电子邮件。 I want the data to be sent to my email address straight. 因此,您的意思是您不希望用户使用 url_launcher 打开它的电子邮件应用程序,您需要在提交表单后准确发送电子邮件。(在您的应用程序中发送电子邮件)。

使用this 库,您可以在应用程序中登录电子邮件(显然是您自己的电子邮件,因为您需要密码),然后使用该电子邮件发送数据。现在你可能会说我为什么要在我的应用程序中输入电子邮件地址和密码!答案是在您的应用程序中发送电子邮件是错误的,最好将这些数据发送到服务器(只有您可以查看和编辑代码),然后服务器使用电子邮件库(如@987654322)向您发送电子邮件@ 再次,但使用服务器自己的编程语言。 我建议你使用firebase函数来做这件事,你应该写一些JS代码,但你也可以在网上找到现成的代码。

【讨论】:

  • 你给了我一个良好的开端。我会将表单发送到 Firestore,然后从那里检索它。感谢您的建议@Parsa
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 2012-06-24
相关资源
最近更新 更多