Email OTP is a Flutter package designed to simplify email authentication using one-time passwords (OTPs). With Email OTP, you can effortlessly generate OTPs and send them to users' email addresses, ensuring secure identity verification. This package offers an efficient and secure method for incorporating email-based OTP authentication into your Flutter applications.
- Configuration
- SMTP (Optional)
- Template (Optional)
- Sending OTP
- Verifying OTP
- Get OTP
- Example
- Report bugs or issues
- Contributors
The simplest way to 🛠️ configure the EmailOTP package is by calling the config
method without any parameters. This sets up the default configuration for generating OTPs.
void main() {
EmailOTP.config();
}
For more control over the OTP generation and email settings, you can pass various parameters to the config
method. Below is an example demonstrating how to customize the configuration.
void main() {
EmailOTP.config(
appName: 'App Name',
otpType: OTPType.numeric,
emailTheme: EmailTheme.v1,
appEmail: '[email protected]',
otpLength: 6,
);
}
Parameters
appName
: A string representing the name of your application.otpType
: Specifies the type of OTP to be generated. It supports three types:OTPType.numeric
: OTP will consist only of numbers.OTPType.alpha
: OTP will consist only of alphabetic characters.OTPType.alphaNumeric
: OTP will consist of both alphabetic characters and numbers.
emailTheme
: Defines the theme for the OTP email. The package currently supports five themes:appEmail
: The email address from which the OTP emails will be sent.otpLength
: An integer specifying the length of the OTP. In this example, the OTP will be 6 digits long.
By configuring these parameters, you can tailor the OTP generation and email sending process to suit your application's requirements.
To send OTPs from a custom email address, configure your SMTP settings using the setSMTP
method. Ensure that your SMTP credentials are accurate.
void main() {
EmailOTP.config();
EmailOTP.setSMTP(
host: 'mail.rohitchouhan.com',
emailPort: EmailPort.port25,
secureType: SecureType.tls,
username: '[email protected]',
password: 'm9eFxuBQ4hbD5XGP3TEdWN',
);
}
Parameters
host
: The hostname of your SMTP server.emailPort
: The port number used by the SMTP server. Supported values include:EmailPort.port25
EmailPort.port465
EmailPort.port587
secureType:
The type of security used by the SMTP server. Supported values include:SecureType.none
SecureType.tls
SecureType.ssl
username
: The username for your SMTP server account.password
: The password for your SMTP server account.
By configuring these parameters, you can ensure that your application is able to send OTP emails securely and efficiently.
You can also customize the email 🌐 template used to send the OTP. Use the setTemplate
method to provide your own HTML template.
void main() {
EmailOTP.config();
EmailOTP.setTemplate(
template: '''
<div style="background-color: #f4f4f4; padding: 20px; font-family: Arial, sans-serif;">
<div style="background-color: #fff; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);">
<h1 style="color: #333;">{{appName}}</h1>
<p style="color: #333;">Your OTP is <strong>{{otp}}</strong></p>
<p style="color: #333;">This OTP is valid for 5 minutes.</p>
<p style="color: #333;">Thank you for using our service.</p>
</div>
</div>
''',
);
}
Parameters
template
: A string containing the HTML content of your custom email template. Use placeholders such as{{appName}}
and{{otp}}
to dynamically insert the application name and OTP respectively.
By configuring these parameters and using a custom template, you can ensure that your OTP emails are tailored to match your application's branding and style.
To 📧 send an 🔐 OTP to a user's email address, use the sendOTP
method. This method takes the recipient's email address as a parameter.
EmailOTP.sendOTP(email: emailController.text)
Parameters
email
: The recipient's email address where the OTP will be sent.
To verify an 🔐 OTP entered by the user, use the verifyOTP
method. This method takes the OTP entered by the user as a parameter and returns a boolean indicating whether the OTP is correct.
EmailOTP.verifyOTP(otp: otpController.text)
Parameters
otp
: The OTP entered by the user.
This code will print the OTP generated by the getOTP
method.
print(EmailOTP.getOTP);
👉 For a complete example, refer to the Email OTP package documentation.
You are welcome to open a ticket on github if any 🐞 problems arise. New ideas are always welcome.
Copyright © 2024 Rohit Chouhan. Licensed under the MIT LICENSE