sendOTP method

Future<bool> sendOTP({
  1. required dynamic email,
  2. String subject = '',
  3. String body = '',
  4. int otpLength = 6,
})

Send an OTP tp the email.

subject is the email subject, default value is Verify Email, this value can be changed only when modifiedSubject for this appName on your server is set to true.

body default is Use this OTP to verify your email for the <b>{appName}</b>, please do not share to anyone: {otp}. this value can be changed only when you set modifiedBody on your server for this appName to true.

otpLength is length of the OTP, this value only works when you set modifiedOtpLength on your server for this appName to true.

Implementation

Future<bool> sendOTP({
  required email,
  String subject = '',
  String body = '',
  int otpLength = 6,
}) async {
  if (!isValidEmail(email)) return false;

  _finalEmail = email;

  final Map<String, String> uriBody = {
    'appName': appName,
    'toMail': email,
    'serverKey': serverKey,
    'subject': subject,
    'body': body,
    'otpLength': otpLength.toString(),
  };
  String uriBodyParams = '';
  uriBody.forEach((key, value) => uriBodyParams += '$key=$value&');
  final url = Uri.parse('$server?$uriBodyParams');
  _printDebug('URL: $url');

  http.Response response = await http.get(url);

  if (response.statusCode != 200) {
    _printDebug('HTTP RESPONSE CODE = ${response.statusCode} != 200');
    _finalEmail = '';
    _finalOTP = '';
    return false;
  }

  final result = jsonDecode(response.body);
  _printDebug('HTTP RESPONSE: $result');

  if (result['status'] == 'ok') {
    _finalOTP = result['message'];
    return true;
  } else {
    _finalEmail = '';
    _finalOTP = '';
    return false;
  }
}