简体   繁体   English

在 flutter/dart 开发中发送验证邮件

[英]send verification mail in flutter/dart development

I'm New to flutter and dart mobile app development.我是 flutter 和 dart 移动应用程序开发的新手。 how to implement forgot password and send verification mail in flutter/dart development or is there any way to implement to send mail.如何在 flutter/dart 开发中实现忘记密码和发送验证邮件,或者有什么方法可以实现发送邮件。

I don't think there is any way to send an email from your flutter application.我认为没有任何方法可以从您的 Flutter 应用程序发送电子邮件。 This is something I would definitely implement on a backend server.这是我肯定会在后端服务器上实现的东西。

I would implement a 'forgot password' button in flutter, which triggers a http call to the backend which then triggers the password generation and email sending.我会在flutter中实现一个“忘记密码”按钮,它会触发对后端的http调用,然后触发密码生成和电子邮件发送。

Read more about making calls over the internet here: [ https://flutter.dev/docs/cookbook/networking/fetch-data][1] 在此处阅读有关通过互联网拨打电话的更多信息:[ https://flutter.dev/docs/cookbook/networking/fetch-data] [1 ]

Edited: This links relates into how you can make requests and accept responses from the internet(to make requests in the server when the password is forgotten as in this case). 编辑:该链接与您如何可以从Internet发出请求和接受响应(在这种情况下忘记密码时在服务器中发出请求)相关。

So you have to create a back-end service to accept the calls from your clients in this case your client is flutter app, I can't provide the way to do it here with code, you should learn more to do that. 因此,您必须创建一个后端服务来接受来自客户的呼叫,在这种情况下,您的客户是扑朔迷离的应用程序,在这里我无法提供使用代码的方法,您应该了解更多有关这样做的信息。 After you have created your server and implemented the logic to accept and validate requests, then the server can return a request with a link in it to reset the password, and you can render the response in the flutter app, so you can make another request with new credentials. 在创建服务器并实现了接受和验证请求的逻辑之后,服务器可以返回带有链接的请求以重置密码,然后您可以在flutter应用程序中呈现响应,因此可以发出另一个请求新的凭据。 The other way is to send the link(request) via email to the client(the email user provided when he/she registered) and the client then can change the password accordingly. 另一种方法是通过电子邮件将链接(请求)发送给客户端(他/她注册时提供的电子邮件用户),然后客户端可以相应地更改密码。

Hope this can help. 希望这会有所帮助。

Yes there are some ways.是的,有一些方法。 The most common would be to use firebase as a backend server handling these requests.最常见的是使用 firebase 作为处理这些请求的后端服务器。

You could do it like this你可以这样做

Add these packages to your flutter apps pubspec.yaml file将这些包添加到您的 flutter 应用程序 pubspec.yaml 文件中

// latest version
firebase_core: ^1.17.0
firebase_auth: ^3.3.18 

On forgot password button click在忘记密码按钮上单击

once you've completed the logic necessary before making the request, call this function一旦您在发出请求之前完成了必要的逻辑,请调用此 function

sendResetEmail(String email, BuildContext context) async {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  try {
    await _auth.sendPasswordResetEmail(email: email);
    Timer(
      const Duration(seconds: 3),
      () => CustomWidgets().moveToPage(
        page: const Home(), context: context, replacement: true),
  );
 } catch (e) {
  // error handling here
}

} }

This will send an email from firebase to the selected email to reset password.这将从 firebase 发送一个 email 到选定的 email 以重置密码。

On email verification request关于 email 验证请求

Once the logic is over, call this function.逻辑结束后,请拨打此电话 function。

bool _isEmailVerified = false;
Timer? timer;
final FirebaseAuth _auth = FirebaseAuth.instance;

the initstate method初始化方法

@override
void initState() {
_isEmailVerified = _auth.currentUser!.emailVerified;

if (!_isEmailVerified) {
  sendEmailVerificationForUser();
  timer = Timer.periodic(const Duration(seconds: 5), (timer) {
    emailVerificationStatus(context);
  });
}

email verification check function email 验证校验 function

emailVerificationStatus(BuildContext context) async {
try {
  await _auth.currentUser!.reload();
  setState(() {
    _isEmailVerified = _auth.currentUser!.emailVerified;
  });
} catch (e) {
  // handle the error here 
}
setState(() {
  _isEmailVerified = _auth.currentUser!.emailVerified;
});

if (_isEmailVerified) {
  timer?.cancel();
  
// and move to next page
  
}}

send email verification function发送 email 验证 function

Future sendEmailVerificationForUser() async {
try {
  await FirebaseAuth.instance.currentUser!.sendEmailVerification();
} catch (e) {
  // error handling}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM