首页 > 解决方案 > 从 Flutter Web 发送邮件

问题描述

嗨,我想从 Flutter Web 向注册的电子邮件发送邮件。我希望将随机安全码发送给新注册用户,以便他们首次登录。收件人电子邮件取自 TextFormField。我已经提到了邮件程序 pub.dev 和示例代码。下面是我添加的代码和我得到的结果。我没有为此实现做任何其他配置。

                    final smtpServer = gmail('email address', 'password');
                    final message = Message()
                      ..from = Address('email address', 'Your Name')
                      ..recipients.add(_emailText.text)
                      ..subject = 'Test Dart Mailer library ::  :: ${DateTime.now()}'
                      ..text = 'This is the plain text.\nThis is line 2 of the text part.'
                      ..html = "<h1>Test</h1>\n<p>Hey! Here's some HTML content</p>";

                    try {
                      final sendReport = await send(message, smtpServer);
                      print('Message sent: ' + sendReport.toString());
                    } on MailerException catch (e) {
                      print('Message not sent.');
                      for (var p in e.problems) {
                        print('Problem: ${p.code}: ${p.msg}');
                      }
                    }

                    var connection = PersistentConnection(smtpServer);

                    // Send the first message
                    await connection.send(message);

                    // close the connection
                    await connection.close();

这是我得到的错误:

Error: Unsupported operation: Socket constructor
    at Object.throw_ [as throw] (http://localhost: )
    at Function._connect (http://localhost: )
    at Function.connect (http://localhost: )
    at connection.Connection.new.connect (http://localhost: )
    at connect.next (<anonymous>)
    at runBody (http://localhost: )
    at Object._async [as async] (http://localhost: )
    at connection.Connection.new.connect (http://localhost: )
    at connect (http://localhost: )
    at connect.next (<anonymous>)
    at runBody (http://localhost: )
    at Object._async [as async] (http://localhost: )
    at Object.connect (http://localhost: )
    at send (http://localhost: )
    at send.next (<anonymous>)
    at runBody (http://localhost: )
    at Object._async [as async] (http://localhost: )
    at Object.send (http://localhost: )
    at signUpForm._SignUpFormState.new.<anonymous> (http://localhost: )
    at Generator.next (<anonymous>)
    at http://localhost:
    at _RootZone.runUnary (http://localhost: )
    at _FutureListener.thenAwait.handleValue (http://localhost: )
    at handleValueCallback (http://localhost: )
    at Function._propagateToListeners (http://localhost: )
    at _Future.new.[_completeWithValue] (http://localhost: )
    at async._AsyncCallbackEntry.new.callback (http://localhost: )
    at Object._microtaskLoop (http://localhost: )
    at _startMicrotaskLoop (http://localhost: )
    at http://localhost: 

我找不到任何相关的解决方案。我错过了什么还是有更简单的方法来实现这个?希望有人可以提供帮助。非常感谢!

标签: firebaseemailflutter-web

解决方案


即使您在使用 Flutter 生成 Web 应用程序时使用 Flutter,所有移动事物的事情都会发生在 JavaScript 中。我强烈建议不要将任何电子邮件凭据放入 JavaScript 代码!!!

为了避免这种情况,只需创建一个可调用的 Firebase 函数,您可以从本机应用程序和 Web 应用程序调用该函数来发送电子邮件。您可以有一个带有电子邮件内容的有效负载,并且该函数还可以在context. 如果发送失败或成功,您也会收到响应。在可调用的云功能中,您可以使用普通的 JS 代码和包来发送电子邮件,如nodemailer.


推荐阅读