skip to Main Content

I’m working on a flutter project and I’m using email_otp package to send a verification code to the email

myAuth.setConfig(
        appEmail: "[email protected]",// used a real email but for email hiding purpose I put this
        appName: "Spectrum Speak",
        userEmail: _emailController.text,
        otpLength: 4,
        otpType: OTPType.digitsOnly,
      );
      bool r = await myAuth.sendOTP();
      if (r == true) {
        print("done send");
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('OTP has been sent'),
          ),
        );
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => OTPScreen(
              myAuth: myAuth,
              email: _emailController.text,
              comeFromSignUp: true,
            ),
          ),
        );
      } else {
        print("error");
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('Oops, OTP send failed'),
          ),
        );
      }

So it always returns a false valuem even though it was functioning properly two weeks ago

I have the latest version of email_otp in pubscpec.yaml, it’s supposed to return a true value and send the OTP to the email in the _emailController.text
I’d appreciate it if someone would tell me if they faced this and how did they fix it, thank you

2

Answers


  1. Chosen as BEST ANSWER

    Update: Gave up on this package and used a third party api called EmailJS to send OTP verification

    Random random = Random();
    // Generate a random number between 0 and 9999
    int randomNumber = random.nextInt(10000);
    // Format the number to have leading zeros if needed
    String formattedNumber =randomNumber.toString().padLeft(4, '0');
    try {
         final url = Uri.parse("https://api.emailjs.com/api/v1.0/email/send");
         final response = await http.post(url,
                                headers: {'Content-Type': 'application/json'},
                                body: jsonEncode({
                                'service_id': 'spectrumspeak',
                                'template_id': 'spectrumspeaktemplate',
                                'user_id': 'OBNbTFzznmRQQuw3G',
                                'template_params': {
                                  'user_name': _userNameController.text,
                                  'otp_code': formattedNumber,
                                  'user_email': _emailController.text
                                }
                              }));
                          print("done send");
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                              content: Text('OTP has been sent'),
                            ),
                          );
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => OTPScreen(
                                otpCode: formattedNumber,
                                email: _emailController.text,
                                comeFromSignUp: false,
                              ),
                            ),
                          );
                        } catch (e) {
                          print("error $e");
                          ScaffoldMessenger.of(context).showSnackBar(
                            const SnackBar(
                              content: Text('Oops, OTP send failed'),
                            ),
                          );
                        }
    

    Watch this video for more details Video. Make sure to turn on API the 'Allow EmailJS API for non-browser applications.' from the security section in the account tab in Email JS, also you can use API public key for the user_id


  2. Something very similar happened to me, I have the following code that worked perfectly a couple of months ago to send an email with a 4-digit code and today that I was debugging, I realized that it no longer sends the email.
    If you find the solution, please share it

      void sendEmail()
      {
        print(widget.email);
        OTPAuth.setConfig
        (
          appEmail: '***********@gmail.com',
          appName: "****",
          userEmail: widget.email,
          otpLength: 4,
          otpType: OTPType.digitsOnly
        );
        OTPAuth.setTemplate
        (
          render: 'Hi ${widget.name}! your {{app_name}} verification code is:<br><br>{{otp}}'
        );
        OTPAuth.sendOTP();
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search